Cross Browser Testing : Is to check that your web application works as expected in different browsers.If we are using Selenium WebDriver, we can automate test cases using Internet Explorer, FireFox, Chrome, Safari browsers.To execute test cases with different browsers in the same machine at same time we can integrate TestNG with Selenium WebDriver.
Lets create a class Testng_BC.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Reporter;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class Testng_BC
{
public WebDriver fd;
@Test
@Parameters({“browser”})
public void browserTest(String str) throws Exception
{
if(str.matches(“firefox”))
{
fd=new FirefoxDriver();
fd.get("http://qtpseleniumsolutions.blogspot.in/");
fd.close();
}
if(str.matches(“ie”))
{
//Following code is to set the ie driver with webdriver property.You need IE Driver //server. download from here.
System.setProperty(“webdriver.ie.driver”,”F:\\Selenium_project\\IEDriverServer.exe”);
fd=new InternetExplorerDriver();
fd.get("http://qtpseleniumsolutions.blogspot.in/");
fd.close();
}
else {
Reporter.log(“test failed”);
}
}
}
Now create testng.xml file and paste the below code
<suite name=”Suite“>
<test name=”FirefoxTest“>
<parameter name=”browser“ value=”firefox“ />
<classes>
<class name=”Testng_BC“ />
</classes>
</test>
<test name=”IETest“>
<parameter name=”browser“ value=”ie“ />
<classes>
<class name=”Testng_BC“ />
</classes>
</test>
</suite>
To execute a testcase in IE browser we have to use IE Driver server.And you have change few settings.
1.Open IE
2.Go to Tools -> Internet Options -> Security
3.Set all zones to the same protected mode, enabled or disabled should not matter.
Finally, set Zoom level to 100% by right clicking on the gear located at the top right corner and enabling the status-bar. Default zoom level is now displayed at the lower right.
Why are we passing parameters ??
To run the Testng_BC class in multiple browsers, we have to pass the parameters from xml.From parameters in testing.xml we can pass browser name and in test case we can create WebDriver reference accordingly.
Now run the testng.xml file, it should execute ‘browserTest’ in Firefox and IE Browsers.If you refresh the project and look into test-output folder, a html report (index.html) has been generated.
Lets create a class Testng_BC.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Reporter;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class Testng_BC
{
public WebDriver fd;
@Test
@Parameters({“browser”})
public void browserTest(String str) throws Exception
{
if(str.matches(“firefox”))
{
fd=new FirefoxDriver();
fd.get("http://qtpseleniumsolutions.blogspot.in/");
fd.close();
}
if(str.matches(“ie”))
{
//Following code is to set the ie driver with webdriver property.You need IE Driver //server. download from here.
System.setProperty(“webdriver.ie.driver”,”F:\\Selenium_project\\IEDriverServer.exe”);
fd=new InternetExplorerDriver();
fd.get("http://qtpseleniumsolutions.blogspot.in/");
fd.close();
}
else {
Reporter.log(“test failed”);
}
}
}
Now create testng.xml file and paste the below code
<suite name=”Suite“>
<test name=”FirefoxTest“>
<parameter name=”browser“ value=”firefox“ />
<classes>
<class name=”Testng_BC“ />
</classes>
</test>
<test name=”IETest“>
<parameter name=”browser“ value=”ie“ />
<classes>
<class name=”Testng_BC“ />
</classes>
</test>
</suite>
To execute a testcase in IE browser we have to use IE Driver server.And you have change few settings.
1.Open IE
2.Go to Tools -> Internet Options -> Security
3.Set all zones to the same protected mode, enabled or disabled should not matter.
Finally, set Zoom level to 100% by right clicking on the gear located at the top right corner and enabling the status-bar. Default zoom level is now displayed at the lower right.
Why are we passing parameters ??
To run the Testng_BC class in multiple browsers, we have to pass the parameters from xml.From parameters in testing.xml we can pass browser name and in test case we can create WebDriver reference accordingly.
Now run the testng.xml file, it should execute ‘browserTest’ in Firefox and IE Browsers.If you refresh the project and look into test-output folder, a html report (index.html) has been generated.
No comments:
Post a Comment