问题描述
当我运行此代码时,它首先打开两个浏览器 (Chrome) 并转到基本 URL。但对我来说,这是令人困惑的部分。当浏览器打开时,在这两个浏览器中,只有一个浏览器会收到测试命令。在我的例子中,我正在运行一个类,这个类有两个测试用例。但是所有的测试用例都将在一个浏览器中运行。第二个浏览器刚刚打开并转到基础网址
基础类:
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:有两种情况的测试类
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 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>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)