I know to run multiple browser sessions on Chrome without @BeforeSuite, but at times, I need to assign variables or something else in @BeforeSuite annotation before going to @BeforeClass or @BeforeTest. And, at the same time I need to start multiple sessions in parallel. How do I do that?
I'v shared sample code below. Here, I use @BeforeSuite to assign few variables and then call 2 parallel tests from TestNG.xml. It will only call 1 test (not 2). But when I don't use @BeforeSuite, it works perfectly fine (both tests run parallelly).
So, Is it possible to run parallel tests while also using @BeforeSuite? Sometimes we do need to use @BeforeSuite in some test scenarios and call multiple browsers sessions.
Thanks.
public class MyClass {
String baseURL;
String browser;
@BeforeSuite
private void setTheVariables() {
//Some codes here
//Some codes here
this.browser = "chrome";
}
@BeforeClass
private void myBeforeClass() {
//Some codes here
//Some codes here
}
@BeforeTest
private void myBeforeClass() {
//Some codes here
//Some codes here
}
@Test
@Parameters("baseURL")
public void f(String baseURL) {
if (this.browser == "chrome") {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\abc\\Downloads");
DesiredCapabilities caps = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.BROWSER, Level.ALL);
caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
WebDriver driver = new ChromeDriver(caps);
System.out.println("Navigating to " + baseURL);
driver.get(baseURL);
}
}
}
Below is my testNG.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestSuite" parallel="tests" thread-count="2">
<test name="Test1" preserve-order="true">
<parameter name="baseURL" value="http://www.amazon.com" />
<classes>
<class name="package.myClass" />
</classes>
</test>
<test name="Test2" preserve-order="true">
<parameter name="baseURL" value="http://www.google.com" />
<classes>
<class name="package.myClass" />
</classes>
</test>
</suite>