09 September, 2015

Selenium exceptions and their possible reasons

  • ElementNotSelectableException 
Thrown when trying to select an unselectable element.
  • ElementNotVisibleException
Thrown when an element is present on the DOM, but it is not visible, and so is not able to be interacted with.
  • ErrorInResponseException 
Thrown when an error has occurred on the server side. 
  • NoAlertPresentException 
Thrown when switching to no presented alert. 
  • NoSuchElementException 
Thrown when element could not be found. 
  • NoSuchFrameException 
Thrown when frame target to be switched doesn’t exist. 
  • NoSuchWindowException 
Thrown when window target to be switched doesn’t exist. 
  • StaleElementReferenceException 
Stale means the element no longer appears on the DOM of the page. 
Thrown when a reference to an element is now “stale”. 
  • TimeoutException 
Thrown when a command does not complete in enough time. 
  • UnexpectedAlertPresentException 
Thrown when an unexpected alert is appeared. 
  • InvalidCookieDomainException 
Thrown when attempting to add a cookie under a different domain than the current URL. 
  • InvalidSelectorException 
Thrown when the selector which is used to find an element does not return a WebElement. 
  • InvalidSwitchToTargetException 
Thrown when frame or window target to be switched doesn’t exist.
  • NoSuchAttributeException
Thrown when the attribute of element could not be found. 
  • WebDriverException 
Base webdriver exception.

02 September, 2015

Using DataProvider in TestNG

DataProvider in TestNG
TestNG supports two different ways of passing parameter values.
1. Parametrization through .xml file
2. DataProvider
  • Parametrization through .xml file is one of the methods of passing parameter values. This method is useful if the data set is limited to just a few rows.
  • To provide data from DataProvider we need to declare a method that returns the data set in the form of two dimensional object array Object[][].
  • The first array represents a data set whereas the second array contains the parameter values.
  • The DataProvider method can be in the same test class or one of its super classes.
  • It is also possible to provide DataProvider in another class but then the method has to be static.
                                     Calling DataProvider from Same Class
package qa_simple_data_provider;

import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;

public class Simple_Data_Provider {

    @Test(dataProvider = "ProvideData")
    public  void getData(int age, String name){
        System.out.println("Running DataProvider from Same class");
        System.out.println("Mr "+ name + " your age is "+ age);
    }

    @DataProvider
    public Object[][] ProvideData(){
        return new Object[][]{{24,"Adam"},{23,"Erwin"}};
    }
}
                                        Calling DataProvider from Other Class
//DataProvider Class    
package qa1;

import org.testng.annotations.DataProvider;


import org.testng.annotations.Test;

public class Simple_Data_Provider {

    @DataProvider
    public static Object[][] ProvideData(){
        return new Object[][]{{24,"Adam"},{23,"Erwin"}};
    }
}

// Method which will use DataProvider

package qa;

import org.testng.annotations.Test;

public class Get_Data_From_Other_Class {

    @Test(dataProvider = "ProvideData", dataProviderClass=qa1.Simple_Data_Provider.class)
    public void getData(int age, String name){
        System.out.println("Running Data Provider from other class");
        System.out.println("Mr "+ name + " your age is "+ age+ " on 02-Aug-2015");
    }
}
                                                   Sample Testng.xml file
<?xml version="1.0" encoding="UTF-8"?>
<suite name="StaticDataProviderExampleSuite" parallel="false">
  <test name="StaticDataProviderTest">
    <classes>
      <class name="qa.Get_Data_From_Other_Class"/>
    </classes>
  </test>
</suite>
While accessing the DataProvider method from other class we will make only 2 changes.
1. The DataProvider method should be declared as static.
2. The method in other class which will use the DataProvider class should specify both DataProvider name and its class in the @Test attributes.

28 August, 2015

Different Preferences available in Firefox for Selenium

In Selenium we usually came across a situations where really don't want Browser Dialog box to pop-up or say we need to change the path of Downloads only for our Automation script and more say we want to set some URL to be opened on starting up a new tab/page every time, or Disable to tool bar tips or disable the warn messages for Close.

Selenium cant automate the objects which is outside of web page scope so to really make the automation go through we can use below provided methods with Preferences of Firefox profile.

browser.download.folderList 
The  browser.download.folderList Preference in Firefox's about:config interface allows the user to choose between one of three pre specified locations in which to store file downloads. Its Default Value is 1.
The value of browser.download.folderList can be set to either 0, 1, or 2.
  • When set to 0, Firefox will save all files downloaded via the browser    on the user's desktop.
  • When set to 1, these downloads are stored in the Downloads folder.
  • When set to 2, the location specified for the most recent download is utilized again.

browser.download.manager.showWhenStarting
The browser.download.manager.showWhenStarting Preference in Firefox's about:config interface allows the user to specify whether or not the Download Manager window is displayed when a file download is initiated.
The value of browser.download.manager.showWhenStarting can be set to either true or false.
  • When set to true, which is the default value, Firefox's Download Manager window will appear each time a download begins.
  • When set to false, this window will be hidden.

browser.startup.page
The value of browser.startup.page can be set to one of four integers: 0, 1, 2, or 3.
  • When this Preference is set to 0, a blank page (about:blank) is opened upon launch.
  • The default value, which is set to 1, causes Firefox to open whatever page(s) is set as the browser's home page.
  • When the value is set to 2, the Web page that the user last visited is opened.
  • Finally, when the value is set to 3, the user's previous browsing session is restored.

browser.chrome.toolbar_tips
The value of browser.chrome.toolbar_tips can be set to either true or false.
  • When set to true, which is the default setting, Firefox will display tool-tips containing helpful text whenever the mouse cursor hovers over specific elements. Some of these elements include bookmarks and  toolbar buttons as well as certain items within Web pages.
  • When set to false, these tool-tips are never displayed.

browser.tabs.warnOnClose
The value of browser.tabs.warnOnClose can be set to either true or false.
  • When set to true, Firefox will display a warning message (which requires user confirmation) if the browser is shut down while more than one tab is open.
  • When set to false, this warning message will never be displayed upon close.

15 July, 2015

How to run TestNG from the Command Prompt?

There are so many articles about running TestNG from Command Prompt but very few explains it in an easy manner.I am trying to put some efforts to make the same thing more simpler to beginners. 
When it comes to running a TestNG file from Command Prompt we may have two situations
  • Project containing both Selenium jar and TestNG jar files 
  • Project containing only TestNG jar files.
First Step is to download the TestNG Jar file from the link below if you havent downloaded it.
Download the Zip file, Extract it and save it to any fixed Location and set the same path of the Testng jar file in CLASSPATH of environment variables.
Lets first run the Project containing only TestNG jar file
Open the Eclipse  and create one new project 'FirstProject' and add the same TestNG jar file to the Project whose path is set in the CLASSPATH.
Right-Click on the Project, Create one new file name and name the file as 'testng.xml'
Add all the neccessary information in the testng.xml file such that if we right click on the testng.xml and run it as testng then we are able to execute the file successfully.
Now Open the Command Prompt and go to the project folder created above. It should contain the complete path where your Eclipse stores the Project.
Verify that we are under the same project folder which we are trying to execute from the command prompt and most important it contains 'testng.xml'.
Now the first step is to set the ProjectPath in the command Prompt in below manner
set ProjectPath=D:\TestNG_Workspace\FirstProject
(Note: The Project Path will differ in your case
Verify the Path by typing below command
echo %ProjectPath%
Now the second Step is to set the classpath from the command prompt.
set classpath=%ProjectPath%\bin;C:\Program Files\testng-6.8\testng-6.8.jar
(Note: The testng jar location path may differ in your case)
 Verify the Path by typing below command
echo %classpath%
Now the final Step is the run the testng.xml file in below manner
java org.testng.TestNG %ProjectPath%\testng.xml
(Note: The name of the testng.xml may differ in your case)
And yes you're now successfully runnnig testng script from command prompt.
Now Run the Project containing both Selenium Jar and TestNG jar file.
First Step is to locate the path of the Selenium jar files, Its Simply the path where from you're adding the Selenium jar files in your Eclipse project. Set the same path of Selenium jar files in CLASSPATH of environment variables.
The Process of running the project will remain same except ClassPath.
set classpath=%ProjectPath%\bin;C:\Program Files\testng-6.8\testng-6.8.jar;S:\selenium-java-2.44.0;S:\selenium-java-2.44.0\*
(Note: The testng jar and Selenium jar file path may differ in your case)
Verfiy the Path by typing below command
echo %classpath%
Run the testng.xml file in below manner
java org.testng.TestNG %ProjectPath%\testng.xml
I hope you're able to run the xml file successfully.