Cross browser testing using TestNG framework?

Let us understand first what is cross browser testing.
 
Whenever the application is developed, it is intended to use on most of the web browser. So, while testing we need to make sure that the application works all and all for major browsers like IE, firefox, Chrome, Safari etc. To test the application over multiple browsers is known as cross browser testing.
In an automation framework, there are two ways to pass browser name which will decide on which browser our test should run.
            1.      To pass the browser name / value from external file like properties file or excel file
            2.      To pass the browser name / value from testing.xml file.
Let us understand how can we pass the browser value from testing.xml here.
Below piece of code accepts the parameter form testing.xml
In Testng.xml, browser values are given.

package crossBrowserTests;

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;

public class CrossBrowserTestFromTestNG {

WebDriver driver;

@Parameters("browser")
@BeforeMethod
public void lanuchBrowser(String browser){

if (browser.equalsIgnoreCase("firefox")){

driver = new FirefoxDriver();
System.out.println("Browser selected for testing is : Firefox");

}
else if (browser.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", "C:chromedriver.exe");

driver = new ChromeDriver();
System.out.println("Browser selected for testing is : Google Chrome");

}
else if (browser.equalsIgnoreCase("ie")){
System.setProperty("webdriver.ie.driver", "C:IEDriverServer.exe");
driver = new InternetExplorerDriver();
System.out.println("Browser selected for testing is : IE");
}
}

@org.testng.annotations.Test
public void Test(){

driver.navigate().to("https://learnaboutsoftwaretesting.blogspot.in");
System.out.println("Title of the webpage is: "+driver.getTitle());
}

@AfterMethod
public void closeBrowser(){
driver.close();
}


}

Sample Testng.xml (where browser value is supplied)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default suite">
<test verbose="2" name="FireFox Test">
<parameter name="browser" value="firefox"></parameter>
<classes>
<class name="crossBrowserTests.CrossBrowserTestFromTestNG"/>
</classes>
</test> <!-- Default test -->
<test verbose="2" name="Chrome Test">
<parameter name="browser" value="chrome"></parameter>
<classes>
<class name="crossBrowserTests.CrossBrowserTestFromTestNG"/>
</classes>
</test> <!-- Default test -->
<test verbose="2" name="Internet Explorer Test">
<parameter name="browser" value="ie"></parameter>
<classes>
<class name="crossBrowserTests.CrossBrowserTestFromTestNG"/>
</classes>
</test> <!-- Default test -->
</suite> <!-- Default suite -->
When you run testing.xml, it will run 3 tests. A similar test in 3 different browsers (firefox, ie, chrome).
Hope this helps !!!

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories