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

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