Testing in multiple browsers or Browser compatibility Testing: these are the word we always bother while automating our Test Cases. Following code will help how to execute the same script with multiple browsers.
Step 1: We need to write our case with small change by passing a parameter.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class MultiBrowser {
private WebDriver driver;
@Parameters("browser")
@BeforeMethod
public void setup(String browser)
{
if(browser.equalsIgnoreCase("firefox"))
{
driver = new FirefoxDriver();
}
else if(browser.equalsIgnoreCase("iexplorer"))
{
// Update the driver path with your location
System.setProperty("webdriver.ie.driver", "Drivers\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
else if(browser.equalsIgnoreCase("chrome"))
{
// Update the driver path with your location
System.setProperty("webdriver.chrome.driver", "Drivers\\chromedriver.exe");
driver = new ChromeDriver();
}
driver.manage().window().maximize();
}
@AfterMethod
public void tearDown()
{
driver.quit();
}
@Test
public void testMultiBrowser() throws InterruptedException
{
driver.get("http://www.google.com");
Thread.sleep(3000);
}
}
2. Now we need to create TestNG.xml and write the following code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MultiBrowser">
<test name="TestFirefox" verbose="10">
<parameter name="browser" value="firefox" />
<classes>
<class name="MultiBrowser" />
</classes>
</test>
<test name="ChromeTest">
<parameter name="browser" value="chrome" />
<classes>
<class name="MultiBrowser" />
</classes>
</test>
<test name="IETest">
<parameter name="browser" value="iexplorer" />
<classes>
<class name="MultiBrowser" />
</classes>
</test>
</suite>
3. Now run the code from TestNG.xml
Step 1: We need to write our case with small change by passing a parameter.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class MultiBrowser {
private WebDriver driver;
@Parameters("browser")
@BeforeMethod
public void setup(String browser)
{
if(browser.equalsIgnoreCase("firefox"))
{
driver = new FirefoxDriver();
}
else if(browser.equalsIgnoreCase("iexplorer"))
{
// Update the driver path with your location
System.setProperty("webdriver.ie.driver", "Drivers\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
else if(browser.equalsIgnoreCase("chrome"))
{
// Update the driver path with your location
System.setProperty("webdriver.chrome.driver", "Drivers\\chromedriver.exe");
driver = new ChromeDriver();
}
driver.manage().window().maximize();
}
@AfterMethod
public void tearDown()
{
driver.quit();
}
@Test
public void testMultiBrowser() throws InterruptedException
{
driver.get("http://www.google.com");
Thread.sleep(3000);
}
}
2. Now we need to create TestNG.xml and write the following code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MultiBrowser">
<test name="TestFirefox" verbose="10">
<parameter name="browser" value="firefox" />
<classes>
<class name="MultiBrowser" />
</classes>
</test>
<test name="ChromeTest">
<parameter name="browser" value="chrome" />
<classes>
<class name="MultiBrowser" />
</classes>
</test>
<test name="IETest">
<parameter name="browser" value="iexplorer" />
<classes>
<class name="MultiBrowser" />
</classes>
</test>
</suite>
3. Now run the code from TestNG.xml