Thursday, January 9, 2014

Running WebDriver test in multiple browsers


My next logical thing to do was to figure out how would I run the same tests in multiple browsers.

The first Step to do so is get the drivers for each of the desired browsers to test. Here are the links to read about them on the wiki  or get to the download section.


  • FirefoxDriver by default is include with Selenium so I didn't have to worry about getting the driver for it.
  • ChromeDriver I already cover this one up in previous post.
  • InternetExplorerDriver There are 2 versions of the driver a 32 bit and a 64 bit.
  • OperaDriver
  • Safari by default the safari driver is also include just like Firefox.


All the downloaded drivers (exe) will have to be located in a folder of your preference, in my case is just c:\Selenium\ with all the rest of files.
The only exception is for Internet Explorer Drivers just because the filename of the 32 and 64 bit are the same you will have to put them in different directories; unless a rename will do the trick (I haven't tried this yet).

Alright lets get this done:
First of all you always have to set System.setProperty to point the location of the drivers so I just created a small class that I will use to set them all from now on.

public class BrowserSetUp {

public static void IEConfiguration32() throws Exception {
System.setProperty("webdriver.ie.driver", "c:/Selenium/IEDriverServer.exe"); //should match your location
}

public static void IEConfiguration64() throws Exception {
System.setProperty("webdriver.ie.driver", "c:/Selenium/IE64/IEDriverServer.exe");
}

public static void FFConfiguration() throws Exception {
//this one is empty on purpose there is no property to set
}
        
        public static void SafariConfiguration() throws Exception {
               //ditto
        }

public static void ChromeConfiguration() throws Exception {
System.setProperty("webdriver.chrome.driver", "c:/Selenium/chromedriver.exe");
}
}

Now that we have this class we won't have to type this over and over again so we can move on to the next step.
I this sample I'll be reusing the code one of the test from the previous post (signInTest) but instead of a test I'll create a  method call signIn () that I'll call in my test method for each browser.

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class MyMultiBrowserTest {
private WebDriver _driver; 
@Before
public void setUp() throws Exception {
                //Call each of the methods to set the System property
BrowserSetUp.ChromeConfiguration();
BrowserSetUp.FFConfiguration();
}

@Test
public void Chrometest () throws Exception {
_driver = new ChromeDriver();
System.out.println(_driver);
_driver.get("https://plus.google.com");
signIn();
}
@Test
public void FireFoxTest() throws Exception {
_driver = new FirefoxDriver();
_driver.get("https://plus.google.com");
signIn(); 
}
@After
public void tearDown() throws Exception {
_driver.quit();
}
public void signIn () throws Exception {
if (_driver.findElement(By.id("Email")).isEnabled()) /* search bar */
{
System.out.println("Google Email text box is editable");
_driver.findElement(By.id("Email")).sendKeys("XXXX@gmail.com"); //replace with valid ID
_driver.findElement(By.id("Passwd")).sendKeys("XXXXXXX"); //replace with valid password
_driver.findElement(By.id("signIn")).click();  
System.out.println("Log in Has been Completed Succesfully");

} else {
System.out.println("Google Email text box is not editable");
}

}
}

The results:

Notes:
  • For some reason IE is giving problems that I believe is because the IE running on my machine is Version 11 but I'll keep looking for a solution.
  • In my other posts for the tearDown () I always use _driver.close() that works perfectly with Chrome but with Firefox the browser never close so had to change it to _driver.quit().
  • The signIn() method can be extracted and put in a separate class with other methods. Now the question that pops if we have asserts in these methods will we be able to see the error easily or not.
My next projects will be to running parallel test and remote webdriver