Wednesday, 15 April 2020

SE - 47 - All about Exceptions !

Throwable 

-> Throwable is parent class(not interface) of all the exceptions and Errors.

-> We can use Throwable in catch block to catch all the exceptions :
     catch(Throwable t)

->The mostly used methods of Throwable class are :

public String getMessage()  - It returns a detailed message string of the Throwable instance (which may be NULL).

public StackTraceElement[] getStackTrace()  - An array of StackTraceElement as given by printStackTrace

Sample Code : 
   try{  
       int i=10/0;  
   }catch(Exception e){  
       StackTraceElement[] trace = e.getStackTrace();  
       System.err.println(trace[0].toString());  
       System.out.println(trace[0].getClass());  
       System.out.println(trace[0].getMethodName());  
       System.out.println(trace[0].getFileName());  
       System.out.println(trace[0].getLineNumber());  

}}

public Throwable getCause()  - It is used to fetch the cause of the Throwable or null if cause can't be determined. This function fetches the cause that was supplied by one of the constructors or that was set after creation with the initCause(Throwable) method. All the PrintStackTrace methods invoke getCause() method to determine the cause of the Throwable.


public void printStackTrace()  - The printStackTrace() method of Java Throwble class is used to print the Throwable along with other details like classname and line number where the exception occurred.


As I mentioned earlier throwable has its 2 kids : Error and Exception

An Error is some problem in the program which can't be handled by try catch block. The execution has to be stopped in case of error. Some common error examples are : 


  • InternalError
  • OutOfMemoryError
  • StackOverflowError
  • UnknownError
  • NoClassDefFoundError
  • UnsatisfiedLinkError
  • AbstractMethodError
  • IllegalAccessError
  • InstantiationError
  • NoSuchFieldError
  • NoSuchMethodError



Exceptions are reasonable and logical errors which can be handled by using try catch block. We can continue the execution . The methods of Exception class are inherited by Throwable. Some common exceptions in java are : 


  • InterruptedException
  • IOException
  • FileNotFoundException
  • ConnectException
  • UnknownHostException
  • ClassNotFoundException
  • IllegalAccessException
  • InstantiationException
  • NoSuchFieldException
  • NoSuchMethodException
  • RuntimeException
  • ArithmeticException
  • ArrayStoreException
  • ClassCastException
  • ConcurrentModificationException
  • IllegalArgumentException
  • IllegalThreadStateException
  • NumberFormatException
  • IllegalStateException
  • IndexOutOfBoundsException
  • ArrayIndexOutOfBoundsException
  • StringIndexOutOfBoundsException
  • NegativeArraySizeException
  • NullPointerException




Tuesday, 10 March 2020

SE - 46 - isVisible() Vs isDisplayed() Vs isEnabled()

isVisible() - It is a legacy method of selenium RC. If an element is made invisible by setting the CSS "visibility" property to "hidden", or the "display" property to "none", either for the element itself or one if it's ancestors then this method will fail.

IisDisplayed - It is a method of selenium 2 and further versions. This method ignores the CSS style attribute and looks for the presence/absence of an element on webpage.

isEnabled() - It is used to check if any element is enabled or not

Saturday, 4 January 2020

SE - 45 - Execution from batch file

We can do batch execution in selenium by giving the execution path of testng.xml in batch file. The execution can be triggered through command too (CMD)

At the first step we need to include the classes in the testng.xml file which we want to execute via batch :

<suite name="Main Test Suite" verbose="2">
    <test name="TestNG Test Group">
        <classes>
        <class name="com.test.Test1"/>
        <class name="com.test.Test2"/>
        </classes>
    </test>
</suite>

After that we need to call the testng.xml file by giving the whole location and creating a .bat file . After giving the following commands in a text file , save it as .bat and execute it :
cd %pathofProject%
set classpath=%projectLocation%\bin;%projectLocation%\lib\*
java org.testng.TestNG %pathofProject%\testng.xml
pause


Friday, 3 January 2020

SE - 44 - How to open a new tab in a pre opened browser from selenium

There are 2 ways to open a new tab in browser which is previously opened.

1) Using Sendkeys method

String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t");
driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);

2) Using Robot class to send keyboard keys : Ctrl + T

//Launch the first URL
driver.get("http://www.google.com");
 
//Use robot class to press Ctrl+t keys     
Robot robot = new Robot();                          
robot.keyPress(KeyEvent.VK_CONTROL); 
robot.keyPress(KeyEvent.VK_T); 
robot.keyRelease(KeyEvent.VK_CONTROL); 
robot.keyRelease(KeyEvent.VK_T);
 
//Switch focus to new tab
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
 
//Launch URL in the new tab
driver.get("http://google.com");


3) Using JavaScrptExecutor

String link = "window.open('https://www.google.com','_blank');";
((JavascriptExecutor)driver).executeScript(link);
OR

((JavascriptExecutor) driver).executeScript("window.open();");

SE - 43 - Methods of Object class


Object class is the parent of all the classes in Java. This class is at the top of all the classes in JAVA. This class is inherited by many classes. Some of the important methods of Object class are :

public final Class getClass() -> returns the Class class object of this object. The Class class can further be used to get the metadata of this class.

public int hashCode() -> returns the hashcode number for this object.

public boolean equals(Object obj) -> compares the given object to this object.

protected Object clone() throws CloneNotSupportedException -> creates and returns the exact copy (clone) of this object.

public String toString() -> returns the string representation of this object.

public final void notify() -> wakes up single thread, waiting on this object's monitor.

public final void notifyAll() -> wakes up all the threads, waiting on this object's monitor.

public final void wait()throws InterruptedException -> causes the current thread to wait, until another thread notifies (invokes notify() or notifyAll() method).

protected void finalize()throws Throwable -> is invoked by the garbage collector before object is being garbage collected.

Saturday, 23 November 2019

SE - 42 - Major interfaces used in Selenium

Lets see few of the interfaces which are used in Selenium. The list also contains few of the JAVA interfaces which may be used in terms of Selenium :

1) WebDriver
2) WebElement
3) Alert
4) TakesScreenshoot
5) All the Listners (Eg : WebdriverEventListener , ItestListener ,etc)
6) SearchContext
7) Iterable , Collection,List,Queue, Set , etc
8) Wait
9) Comparator
10)JavaScriptExecutor
11) Row , Cell (Apache POI)

SE - 41 - Island of isolation in JAVA

Island of isolation is a term given to a condition where 2 objects are ready for garbage collection when the following conditions are met :

1) Object 1 references to Object 2
2) Object 2 references to Object 1

      Test i;
      public static void main(String[] args) 
      {
          Test t1 = new Test();
          Test t2 = new Test();
          t1.i = t2;
          t2.i = t1;
          t1 = null;
          t2 = null;
          System.gc();
      }