Wednesday, September 17, 2014

How to run Selenium IDE script in different browser

We can easily create selenium script through Selenium IDE and run those scripts on Firefox browser.If you have no idea about selenium script, please visit my other post on IDE (How to record and run scripts with Selenium IDE).
But in order to do cross browser testing we need to run the script on other browsers like chrome, Internet Explorer etc.
Selenium IDE has given a feature (that is still experimental stage) where we can run our recorded IDE scripts through Chrome, Internet Explorer etc.
Step 1 : Download Selenium Standalone server and Browser driver server
Download the selenium Standalone server from the location (http://www.seleniumhq.org/download/).
image
Download the Browser Driver from the following location.
  • Internet Explorer(IE) driver :
    • Go to the location (http://www.seleniumhq.org/download/).
    • Note that there are two different version for 32 bit and 64 bit windows. Click on the appropriate link (32 bit windows IE or 64 bit windows IE) according to your windows OS version (32 or 64 bit).
image
image
image
  • Click to download the latest chrome driver. Here I have downloaded version 2.10. But it might be more latest version in future release. Clicking on this link will go to the following page.
image
  • Click on the above selected link and download the zip file. Unzip the file and copy the EXE file to a single location where Selenium Standalone server and IE driver are located.
Step 2 : Configure Selenium IDE
  • Open Selenium IDE
  • Go to Options
  • Click on Options menu
image
  • Selenium IDE options window is displayed and click on Webdriver tab.
image
  • Now check Enable Webdriver Playback options
  • Enter the full name of the browser you want to run your script in the text box below the checkbox. For example, in case of IE, we need to type “internet explorer”. In case of other browser, we can enter chrome, android, firefox, htmlunit, iPhone, iPad, opera.
image
  • Click ok button
  • Now restart the Selenium IDE. A restart is required to enable and disable webdriver. By default this option is disabled at selenium IDE.
Step 3 : Run Selenium Standalone server and Browser driver server
  • Before running this server ensure that java runtime environment is already installed on that machine as this is required to run JAR file. If not then download it from specific location.
  • Open the command prompt by using admin privilege (Run as administrator).
image
  • Go to the location where the Selenium Standalone server JAR file located. In my case, this is C:\Run location.
image
  • If you want to run your script on Internet explorer then type following command where both Selenium Standalone server and IE driver will start running.
java -jar selenium-server-standalone-2.42.2.jar -Dwebdriver.ie.driver=IEDriverServer.exe
image

image
  • In case of chrome driver the command will be
java -jar selenium-server-standalone-2.42.2.jar -Dwebdriver.chrome.driver=chromedriver.exe
All of the cases the command syntax will be
java -jar seleniumStandaloneServerFileName.jar -Dwebdriver.BrowserName.driver=.BrowserDriverServerFileName.exe

Note that every time we run the selenium standalone server with the browser driver server.

Step 4 : Run Selenium IDE scripts
  • Open Selenium IDE.
  • Load the recorded script file.
  • Run the script.
  • Note that the browser will be pop up and start executing the script.
Selenium IDE script recorded through Firefox browser will now run through other browsers like Chrome, IE etc.

Please comment me on any error you found to execute these steps.
Happy Cross Browser Testing!!!

Wednesday, September 10, 2014

How to continue refreshing a page until the element is loaded

Sometimes we need to continue refreshing the page until an specific element is loaded. In that case we need to write some extra code beside selenium webdriver command.

We can use do…While() loop using the specific element as locator and the function “isDisplayed()” to detect that the element is appeared. We run the loop until we get the element loaded on the page. Every time we execute the refresh() method under the loop.

do{
            // Reload the page

}while(the specific element is not displayed);

In Java,

do{
driver.navigate().refresh();
}while(!driver.findElement(By.id("gbqfq")).isDisplayed());
I think this will help those who are fighting with element loading problem.
Happy Selenium Webdrivering!!!

Tuesday, September 9, 2014

How to refresh or reload a page in selenium webdriver

Sometimes we need to refresh the web page to ensure that all the elements are loaded. In some of the cases we see that for the first attempt all elements are not loaded but if we load the page for the second time we see that all the components are loaded. Some of the cases we need to load pages more than one times. In that case we use some conditional loops until the components are loaded.

We can refresh the page in many ways. Here we will discuss some of the ways.

Using refresh() method:
Selenium webdriver has a method called refresh(). This is widely used command. It refreshes the current page after executing.

In Java,
driver.navigate().refresh();

Using sendkeys() method:
sendkeys() method is used like we do page refresh pressing F5 key through our keyboard. This manual task is executed by code using the sendkeys() command over an element.

The command will be like
driver.findElement(By.id(locator)).sendKeys(F5 key);

In Java,
driver.findElement(By.id("gbqfq")).sendKeys(Keys.F5);

Using navigate().to() method:
Actually browsing the same URL using navigate().to() function. We call getCurrentUrl() function to get the current URL of the page.
In Java,
driver.navigate().to(driver.getCurrentUrl());  

Using get() method:
If we know the URL then we can load the same URL again to reload the page. In Java,
driver.get("https://www.google.com.bd/");
We can also use getCurrentUrl() function to know the current URL of the page. The command would be,
driver.get(driver.getCurrentUrl());

Using sendkeys() method with ASCII code:
We can use ASCII code as argument on sendkeys() method that is equivalent to F5 key command of the keyboard.
driver.findElement(By.id("gbqfq")).sendKeys("\uE035");
Here \uE035 is the ASCII code of F5 key.
Using executeScript() method:
Using this command we can execute any JavaScript for our need. If we execute location.reload() JavaScript function then current page will be reloaded which meets our purpose. 

The command is,
driver.executeScript("location.reload()"); 
There are many other ways but these are the generally used ways. I think this will help us. 
Happy Selenium Webdrivering!!!

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...