It is very important to test your web application across multiple browser. Each browser as different way of processing the code and hence user experience might differ from browser to browser as a tester it is our responsibility to make sure our application works fine on most commonly used browsers such as firefox, chrome and IE.
TestNG allows us to automate the multi-browser testing using TestNG parameters. In this post we will see in detail how we can achieve this withe simple example.
Example.
1. Create a simple test script.
2. Configure it to pass Browser Type as a parameter.
package test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class MultiBrowserTest { public WebDriver driver; @Parameters("browser") @BeforeClass // Passing Browser parameter from TestNG xml public void beforeTest(String browser) { // If the browser is Chrome, then do this if(browser.equalsIgnoreCase("chrome")) { System.setProperty("webdriver.ie.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe"); driver = new InternetExplorerDriver();; // If browser is IE, then do this }else if (browser.equalsIgnoreCase("ie")) { // Here I am setting up the path for my IEDriver System.setProperty("webdriver.ie.driver", "D:\\QA\\drivers\\IEDriverServer.exe"); driver = new InternetExplorerDriver(); } // Doesn't the browser type, lauch the Website driver.get("http://www.qaautomated.com"); } // Once Before method is completed, Test method will start @Test public void search() throws Exception { Thread.sleep(5000); driver.findElement(By.xpath("*//input[@class='search-field']")).sendKeys("appium"); driver.findElement(By.xpath("*//input[@class='search-submit']")).click(); driver.quit(); } @AfterClass public void afterTest() { driver.quit(); } }
3. Mention browser type details in testng.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="ChromeTest"> <parameter name="browser" value="chrome" /> <classes> <class name="test.MultiBrowserTest" /> </classes> </test> <test name="IETest"> <parameter name="browser" value="ie" /> <classes> <class name="test.MultiBrowserTest" /> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
4. Run it as TestNG Suit.
Parallel Execution - Consider you want to execute your tests in parallel on multiple browser then with a small change to your testng.xml you achieve this
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite" parallel="tests"> <test name="ChromeTest"> <parameter name="browser" value="chrome" /> <classes> <class name="test.MultiBrowserTest" /> </classes> </test> <test name="IETest"> <parameter name="browser" value="ie" /> <classes> <class name="test.MultiBrowserTest" /> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
.
Click Here Best RPA Online Training to go to RPA Online Training Institute Details .
ReplyDeleteNice post..
ReplyDeleteDOT NET training in btm
dot net training institute in btm
dot net course in btm
best dot net training institute in btm
Nice Article Thanks for sharing this post
ReplyDeleteselenium training centers in Bangalore
best software testing training institutes in Bangalore with placements
automation testing courses in Bangalore
selenium testing course in Bangalore
software testing institutes in Bangalore
selenium training in Bangalore
best selenium training in Bangalore
selenium course in Bangalore
Your Blog is awsem. I also find a blog Gk Questions and Answers which is also awsem
ReplyDeleteSuch a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
ReplyDeleteExcelR Business Analytics Course
I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job ExcelR Business Analytics Courses
ReplyDelete