Cross Browser Testing with selenium web driver
Lets say we want to test our UI on multiple browsers when automating the UI flow. Cross browser testing is what is required. Discussed below are how we can achieve that with selenium web driver.
In this example, we are going to run the same selenium code on both Chrome and Firefox browsers
First in your project path create an XML file as below. What I have highlighted below are the package path and the class name.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" thread-count="2" parallel="tests" >
<test name="ChromeTest">
<parameter name="browser" value="Chrome" />
<classes>
<class name="project.CrossBrowserScript">
</class>
</classes>
</test>
<test name="FirefoxTest">
<parameter name="browser" value="Firefox" />
<classes>
<class name="project.CrossBrowserScript">
</class>
</classes>
</test>
</suite>
Before we write the below code, we need to download the relevant drivers. In this case I have downloaded the Chrome driver and the Firefox driver(geko driver).
Next, we need to write the selenium code. For this we need to use the @Parameters("browser") provided by TestNG. This enables us inform the
code what browsers we need to call.
Next within the setup method I call the relevant browser based on the identification of the browser name we call the driver type.
Within the testBrowsers() method we call the driver.get method and also call the relevant web elements to submit the details to be tested
package project;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.annotations.Optional;
public class CrossBrowserScript {
WebDriver driver;
String browser;
@BeforeTest(alwaysRun = true)
@Parameters("browser")
public void setup(@Optional String browser) throws Exception{
if(browser.equalsIgnoreCase("firefox")){
System.setProperty("webdriver.gecko.driver", "/home/shavantha/Documents/geckodriver");
driver = new FirefoxDriver();
}//Check if parameter passed as 'chrome'
else if(browser.equalsIgnoreCase("chrome")){
//set path to chromedriver.exe
System.setProperty("webdriver.chrome.driver", "/home/shavantha/Documents/chromedriver");
//create chrome instance
driver = new ChromeDriver();
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void testBrowsers() throws InterruptedException{
driver.get("https://localhost:9443/publisher/");
//Find user name
WebElement userName = driver.findElement(By.id("username"));
//Fill user name
userName.sendKeys("my username");
//Find password
WebElement password = driver.findElement(By.id("pass"));
//Fill password
password.sendKeys("mypass");
}
}
Because we have to invoke two different browsers, in order to run this code right click on the xml file and select "Run As" TestNG Suit.
Lets say we want to test our UI on multiple browsers when automating the UI flow. Cross browser testing is what is required. Discussed below are how we can achieve that with selenium web driver.
In this example, we are going to run the same selenium code on both Chrome and Firefox browsers
First in your project path create an XML file as below. What I have highlighted below are the package path and the class name.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" thread-count="2" parallel="tests" >
<test name="ChromeTest">
<parameter name="browser" value="Chrome" />
<classes>
<class name="project.CrossBrowserScript">
</class>
</classes>
</test>
<test name="FirefoxTest">
<parameter name="browser" value="Firefox" />
<classes>
<class name="project.CrossBrowserScript">
</class>
</classes>
</test>
</suite>
Before we write the below code, we need to download the relevant drivers. In this case I have downloaded the Chrome driver and the Firefox driver(geko driver).
Next, we need to write the selenium code. For this we need to use the @Parameters("browser") provided by TestNG. This enables us inform the
code what browsers we need to call.
Next within the setup method I call the relevant browser based on the identification of the browser name we call the driver type.
Within the testBrowsers() method we call the driver.get method and also call the relevant web elements to submit the details to be tested
package project;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.annotations.Optional;
public class CrossBrowserScript {
WebDriver driver;
String browser;
@BeforeTest(alwaysRun = true)
@Parameters("browser")
public void setup(@Optional String browser) throws Exception{
if(browser.equalsIgnoreCase("firefox")){
System.setProperty("webdriver.gecko.driver", "/home/shavantha/Documents/geckodriver");
driver = new FirefoxDriver();
}//Check if parameter passed as 'chrome'
else if(browser.equalsIgnoreCase("chrome")){
//set path to chromedriver.exe
System.setProperty("webdriver.chrome.driver", "/home/shavantha/Documents/chromedriver");
//create chrome instance
driver = new ChromeDriver();
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void testBrowsers() throws InterruptedException{
driver.get("https://localhost:9443/publisher/");
//Find user name
WebElement userName = driver.findElement(By.id("username"));
//Fill user name
userName.sendKeys("my username");
//Find password
WebElement password = driver.findElement(By.id("pass"));
//Fill password
password.sendKeys("mypass");
}
}
Because we have to invoke two different browsers, in order to run this code right click on the xml file and select "Run As" TestNG Suit.
Comments
Post a Comment