Thursday, August 14, 2014

Easy way to determine the better XPath for Selenium Webdriver scripts

Determine the best XPath for selenium webdriver is sometimes difficult. We use different tools like Fire Bug, XPath checker, XPath Hepler for Chrome, developer tools for different browser etc. But determine the accurate XPath which will work every time when scripts runs, is sometimes difficult. It is found that proper XPath has constructed using a tool and implemented in the script to find the element and run the script multiple times at the time of development so that no “element not found” found but at the time of test execution it cannot detect the element. In that case we need to change the XPath again and run again to check that it is working.
But if we use the XPath that is  nearly better and developed by some of the Selenium tools like Selenium IDE, is more better than any other tools and nearly well known for Selenium webdriver API.
Here I will show you how to determine XPath for Selenium Webdriver scripts to locate element in the web page by using our well known Selenium IDE tools.

Installing the IDE tools:
You may know How to install Selenium IDE Plug in on your Firefox Browser. If not then click here.

Determining the XPath:
Step 1: Open Firefox browser.
Step 2: Click on the Selenium IDE icon on Plug in tool bar.

image

Step 3 : Selenium IDE window is displayed. Now click the red color record button to make the record on.

image

Step 4 : Now minimize the IDE window and enter the application URL : https://www.google.com.bd/ at the Firefox browser
Step 5 : Enter the text “Selenium IDE” at Google search text box and click on Search button.
Step 6 : Now Restore the Selenium IDE window and click red color button again to Stop the recording. You can play the script by clicking on green color play button.
Step 7 : Click on a Step. Command, Target and value is displayed at the bottom of the window.

 image

Step 8 : Here we see “id” as a locator in target combo box. If we click on the combo box we see a drop down list of other locators like Name, CSS, XPath etc.

image

Step 9 : There are three types of XPath here – Attribute, relative and Position. Most of the cases position is working fine. We select the XPath : Position at the target combo box. Now click on “Find” button to verify that it can locate the actual element.

image

Step 10 : Copy the XPath from Target box and paste to the selenium Webdriver code as Locator.
C# :
driver.FindElement(By.XPath("//td[2]/div/input")).SendKeys("Selenium IDE");
Java :
driver.findElement(By.xpath("//td[2]/div/input")).sendKeys("Selenium IDE");
Note that we will only use XPath if and only if id, Name or CSS are not available as locator. 
Thanks!!!

Wednesday, August 13, 2014

Running Selenium Webdriver script in different browsers

In cross browser testing, we need to run selenium webdriver script on different browser like Firefox, Chrome and Internet Explorer etc. In order to do that we need to declare the webdriver for different browsers.

Firefox:
There is a Firefox driver class at Selenium webdriver package.
import org.openqa.selenium.firefox.FirefoxDriver;
The driver declaration will be :
In Java,
Webdriver driver=new FirefoxDriver();
Internet Explorer:
In case  of internet Explorer, the implementation is different. You have to download the internet explorer driver server from http://docs.seleniumhq.org/download/ page at the section “The Internet Explorer Driver Server”. There are two version present for windows 32 bit and 64 bit version. Download your appropriate version and Unzip the folder and rename it and place it in a suitable place of your project. There is a single executable file on the folder in named “IEDriverServer.exe”.
We need to add a package for this IE.
(In Java)
import org.openqa.selenium.ie.InternetExplorerDriver;

In driver declaration section, at first we have to define the path where this file is located using setProperty() function that takes the browser specific driver name and downloaded driver file path as the argument.

System.setProperty(DriverName, FilePath);

Here DriverName is in format like "webdriver.ie.driver". FilePath is like “C:\\Development\\…….\\IE Driver\\IEDriverServer.exe"

So the implementation will be (In Java),
System.setProperty("webdriver.ie.driver", "C:\\Development\\IE Driver\\IEDriverServer.exe");

The driver declaration will be (In Java),
Webdriver driver=new InternetExplorerDriver();

Chrome:
In case  of Chrome, the implementation is different but almost same IE driver described before. You have to download the Chrome driver server from https://code.google.com/p/selenium/wiki/ChromeDriver page. Click on “Download Driver” link will go to download page where there are different versions of drivers are displayed. Download the latest version. Unzip the folder and rename it and place it in a suitable place of your project. There is a single executable file on the folder in named “chromedriver.exe”.
We need to add a package for Chrome.
(In Java)
import org.openqa.selenium.chrome.ChromeDriver;
In driver declaration section, at first we have to define the path where this file is located using setProperty() function that takes the browser specific driver name and downloaded driver file path as the argument. So the implementation will be
(In Java),
System.setProperty("webdriver.chrome.driver", "C:\\Development\\chromedriver_win32\\chromedriver.exe");
The driver declaration will be (In Java),
 Webdriver driver = new ChromeDriver();
After driver declaration, your webdriver script will be same for all the three browsers.

In future post, I will describe on other browsers like opera and safari. Thanks !!


Tuesday, August 12, 2014

How to handle multiple windows in Selenium Webdriver

Now a days multiple windows are used common in most of the web applications. In some cases clicking on a link to open the page in a new windows. Selenium webdriver object needs to move to that window and do some operation and back to the previous window. So it is important to switch to the specific window recognizing its Title, Element, Content etc.
Selenium webdriver has given some specific features to handle multiple windows in a browser. For example, we have WindowA. Clicking on a link of WindowA will open a WindowB and clicking on a link of WindowB will open a new WindowC.
Now we want to move from WindowA to WindowC, the code (Java) will be,
String Parent_Window=driver.getWindowHandle();

 for (String Child_Window : driver.getWindowHandles())
         {
             driver.switchTo().window(Child_Window); 
         }

driver.getWindowHandle() :: returns the current window position.
driver.getWindowHandles() :: Rturns all the open windows referrence.

Using For statement driver will hover all window and stop to the last WindowC. If we want to move back to WindowA, we need another statement,
driver.switchTo().window(Parent_Window);

After moving to a window, we can do any other operation on that window and then moves to other windows.

Monday, August 11, 2014

How to use implicit and explicit wait in Selenium Webdriver

In Selenium webdriver, we need to wait for sometimes to load the page or any element of a page. In that cases we can use wait.
There are two types of wait used in selenium webdriver.
  1. Implicit Wait
  2. Explicit wait
How to implement implicit wait
We can instruct the browser to wait up to a certain amount of time before throwing a not element found exception. When the page or a component of a page is loaded, it waits up to the time that is declared at the implicit wait command. If the page or component is not loaded with in this time period, then selenium Webdriver throws an exception that the specific element is not found. Declaring wait command for a webdriver object will stay up to its life time until the driver is closed.

driver.manage().timeouts().implicitlyWait(time period, TimeUnit.SECONDS);

time period : Here time value is given as input. How many seconds the driver has to wait is given here.
TimeUnit.SECONDS : Time period is measured as second here. You can use other time unit like day, hour etc.

Example (Java) :
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

Implicit wait can make executing scripts slower.

How to implement Explicit wait
Explicit wait is used to define wait command for a single component load or single condition execution. It only waits for the single component load or single lines execution. There are two classes WebDriverWait and ExpectedConditions for this purpose.

WebDriverWait wait = new WebDriverWait(driver, time period);
wait.until(ExpectedConditions.textToBePresentInElement(By.xpath(getTxtVerifyTitle()), Text Title));

time period : Here time value is given as input. How many seconds the driver has to wait is given here.

Example (Java) :
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.textToBePresentInElement(By.xpath(getTxtVerifyTitle()), "Verify Email"));

ExpectedConditions class has some conditions.

alertIsPresent() : Is Alert Present?
elementSelectionStateToBe: Is the element selected?
elementToBeClickable: Is the element clickable?
elementToBeSelected: Element is selected
frameToBeAvailableAndSwitchToIt: Is frame available and selected?
invisibilityOfElementLocated: Is the element invisible?
presenceOfAllElementsLocatedBy: All elements presence location.
refreshed: Wait for a page refresh.
textToBePresentInElement: Is the text present for a particular element?
textToBePresentInElementValue: Is the element value present for a particular element?
visibilityOf: Is the element visible?
titleContains: Is that title contain?

Cypress: How to handle browser-based authentication pop up dialog in Cypress

Five years ago I have written a blog on how to handle browser-based authentication for selenium webdriver.   Now it is for cypress. Cypress...