This post was most recently updated on July 31st, 2024
Single script can be ran in multiple browsers. To perform that we need to define browser configurations in testng.xml file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!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="Package1.test1"> </class> </classes> </test> <test name="FirefoxTest"> <parameter name="browser" value="Firefox" /> <classes> <class name="Package1.test1"> </class> </classes> </test> </suite> |
In class name we can mention script name. We can mention same script name or multiple script names. If you want to run in 3 different browsers then just copy test block and paste inside suite. And mention browser value.
And in create class demoBrowser and paste below code. Pass browser parameter using testng annotations and we have used @BeforeSuite annotation which will run at the starting of every execution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
@Parameters("browser") @BeforeSuite public void demoBrowser(String browser){ if (browser.equalsIgnoreCase("Firefox")) { driver = new FirefoxDriver(); } else if (browser.equalsIgnoreCase("Chrome")) { System.setProperty("webdriver.chrome.driver",+pathToChromeDriver+"chromedriver.exe"); driver = new ChromeDriver(); } else { System.out.println("invalid browser name"); } } |