I am new to parallel testing (I am using java selenium with TestNG as my project framework).
when I launch parallel testing using my pom.xml file I can see that browsers opened for me but the logic is happening only in one browser (so I can see that the input for the login page for example is being executed only in one browser), at the beginning I had static WebDriver instance so I changed it to be dynamic.
This is my pom.xml file (partial, I did not copied the dependencies part):
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<!-- <suiteXmlFiles> will be added in AUT-98-->
<!-- <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>-->
<!-- </suiteXmlFiles>-->
<systemPropertyVariables>
<allure.results.directory>${project.build.directory}/allure-results</allure.results.directory>
</systemPropertyVariables>
<argLine>
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
</argLine>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>reg</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>critical</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<debugForkedProcess>true</debugForkedProcess>
<suiteXmlFiles>
<suiteXmlFile>critical_tests.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
this is my testng.xml file:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Automation_Suite" parallel="methods">
<test name="Critical tests to run after DEV deploy build">
<groups>
<run>
<include name="critical"/>
</run>
</groups>
<classes>
<class name="com.hackeruso.automation.ui.cms.categories.CategoriesTest"/>
<class name="com.hackeruso.automation.ui.cms.content_manager.ContentManagerPresentationTest"/>
<class name="com.hackeruso.automation.ui.cms.cyberpedia.CyberpediaCategoriesTest"/>
</classes>
</test>
</suite>
And this is how I initialized my webdriver (method from BaseTest) which being extended from all test classes):
@BeforeClass(alwaysRun = true)
public final void BaseTestSetUp(ITestContext context) throws IOException {
driver = DriverWrapper.open(BROWSER, TEST_OUTPUT_FOLDER);
DriverFactory.getInstance().setDriver(driver);
driver = DriverFactory.getInstance().getDriver();
}
This is the 'open' method which init the webdriver:
public class DriverWrapper implements WebDriver {
private WebDriver driver;
private DriverWrapper(WebDriver driver){
this.driver = driver;
}
public static DriverWrapper open(Browser browser, File downloadsFolder) {
Log.info(String.format("Starting new %s browser driver", browser));
switch (browser) {
case FIREFOX:
return createFireFoxInst();
case CHROME:
return createChromeInst(downloadsFolder);
default:
throw new IllegalArgumentException("'" + browser + "'no such browser type");
}
}
private static DriverWrapper createFireFoxInst() {
WebDriverManager.firefoxdriver().setup();
FirefoxOptions options = new FirefoxOptions();
options.setAcceptInsecureCerts(true);
options.setHeadless((EnvConf.getAsBoolean("selenium.headless")));
FirefoxDriver driver = new FirefoxDriver(options);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
return new DriverWrapper(driver);
}
private static DriverWrapper createChromeInst(File downloadsFolder){
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.setHeadless(EnvConf.getAsBoolean("selenium.headless"));
options.setAcceptInsecureCerts(true);
options.addArguments("--lang=" + EnvConf.getProperty("selenium.locale"));
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.BROWSER, Level.SEVERE);
options.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
options.addArguments("--window-size=" + EnvConf.getProperty("selenium.window_size"));
ChromeDriverService service = ChromeDriverService.createDefaultService();
ChromeDriver driver = new ChromeDriver(service, options);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
return new DriverWrapper(driver);
}
}
I am using mvn test -Pcritical
to run my tests.
Expected results: expected number of browsers will be open and will start to run different class tests in parallel.
Actual results: expected number of browsers opened, but interactions are being done only on one browser.
I don't understand what was going wrong, first I declared webdriver as static and then I changed it to non-static variable and still not working as expected. I can provide more chunks of code if needed.