In real time scenario we have to run our selenium test script in multiple browser simultaneously.
TestNG makes is possible to achieve that using @Parameter annotation.
TestNG allows the user to pass values to test methods as arguments by using parameter annotations through testng. xml file. Some times it may be required for us to pass values to test methods during run time. ... The @Parameters annotation can be placed on any method that has a @Test, @Before/After or @Factory annotation. Here i will show an example that how can we use this annotation and run our test cases in parallel .
package Baseclass; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.ITestResult; import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Parameters; import io.github.bonigarcia.wdm.WebDriverManager; public class Baseclass { public static WebDriver driver; public static pageobjects pageobjects; @Test @Parameters(value="browsername") public static void setup(String browsername) { if(browsername.equalsIgnoreCase("chrome")) { WebDriverManager.chromedriver().setup(); driver=new ChromeDriver(); } if(browsername.equalsIgnoreCase("ff")) { WebDriverManager.firefoxdriver().setup(); driver=new FirefoxDriver(); } driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); driver.get("https://www.goibibo.com/"); driver.manage().window().maximize(); String title=driver.getTitle(); System.out.println("page title is ---->>"+title); pageobjects=new pageobjects(driver); }
Now, just create a TestNG file of this project by Right click on project name > TestNG>convert to TestNG
Now, add this code to the TestNG.xml file.
<parameter name="browsername" value="chrome"> </parameter>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="Suite" parallel="tests" thread-count="2"> <test name="testonchrome"> <parameter name="browsername" value="chrome"> </parameter> <classes> <class name="Baseclass.Hotelsearch" /> </classes> </test> <test name="testonff"> <parameter name="browsername" value="ff"> </parameter> <classes> <class name="Baseclass.Hotelsearch" /> </classes> </test> </suite>