When I run this code, first it opens two Browsers ( Chrome ) and goes to the base URL. But here is the confusing part for me. When the browsers are open, out of these two browsers, only one browser will receive test commands. In my cases, I am running one class and this class has two test cases. but all of the test cases will run in one browser. 2nd browser just open and goes to the bases URL
BASE CLASS :
package com.cross.base;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Parameters;
public class MultiThread_Class {
public static WebDriverWait driverWait;
protected static ThreadLocal<WebDriver> driverThreadLocal = new ThreadLocal <>();
@Parameters({"browserName"})
public synchronized ThreadLocal<WebDriver> setUP(String browserName) {
if (browserName.equalsIgnoreCase("chrome")) {
WebDriverManager.chromedriver().setup();
driverThreadLocal.set(new ChromeDriver());
} else if (browserName.equalsIgnoreCase("firefox")) {
WebDriverManager.firefoxdriver().setup();
driverThreadLocal.set(new FirefoxDriver());
} else if (browserName.equalsIgnoreCase("edge")) {
WebDriverManager.edgedriver().setup();
driverThreadLocal.set(new EdgeDriver());
} else if (browserName.equalsIgnoreCase("IE")) {
WebDriverManager.iedriver().setup();
driverThreadLocal.set(new InternetExplorerDriver());
} else {
System.out.println("Wrong Driver");
}
driverWait = new WebDriverWait(getDriver(),15);
return driverThreadLocal;
}
public synchronized WebDriver getDriver(){
return driverThreadLocal.get();
}
public synchronized void browserclose(){
getDriver().close();
}
}
TEST CLASS: Testing class with two cases
package Parallel_testing;
import com.cross.base.MultiThread_Class;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class CrossBrowserYouTube_test extends MultiThread_Class {
@BeforeMethod
@Parameters({"browserName"})
public void browserLaunch(String browserName){
setUP(browserName);
}
//@AfterMethod
public void teardown(){
browserclose();
}
@Test
public void test1(){
getDriver().get("https://www.youtube.com/");
System.out.println("Test 1 Thread id is : " + Thread.currentThread().getId());
driverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='search']"))).click();
driverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='search']"))).sendKeys("Automation ");
driverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//yt-icon[@class='style-scope ytd-searchbox']"))).click();
}
@Test
public void test2(){
getDriver().get("https://www.youtube.com/");
System.out.println("Test 2 Thread id is : " + Thread.currentThread().getId());
driverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='search']"))).click();
driverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='search']"))).sendKeys(" Testing");
driverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//yt-icon[@class='style-scope ytd-searchbox']"))).click();
}
}
XML FILE :- XML Test file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite" parallel="methods" thread-count="12">
<parameter name="browserName" value="chrome"/>
<test verbose="2" preserve-order="true" name="chrome">
<classes>
<class name="Parallel_testing.CrossBrowserYouTube_test">
<methods>
<include name="test1"/>
<include name="test2"/>
</methods>
</class>
</classes>
</test>
</suite>
question from:
https://stackoverflow.com/questions/65893972/testng-parallel-testing-opens-two-browsers-but-runs-all-tests-only-in-1-browser 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…