gpt4 book ai didi

multithreading - 并行测试执行后,TestNG关闭浏览器

转载 作者:行者123 更新时间:2023-12-03 13:19:29 25 4
gpt4 key购买 nike

我想在完成所有测试后关闭浏览器。问题是我无法关闭浏览器,因为创建的对象ThreadLocal驱动程序在测试值返回为null后无法识别该驱动程序。

下面是我的工作代码

package demo;

import java.lang.reflect.Method;
import org.openqa.selenium.By;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class ParallelMethodTest {
private static ThreadLocal<dummy> driver;
private int input;
private int length;

@BeforeMethod
public void beforeMethod() {
System.err.println("Before ID" + Thread.currentThread().getId());
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
if (driver == null) {
driver = new ThreadLocal<dummy>();
}
if (driver.get()== null) {
driver.set(new dummy());
}

}

@DataProvider(name = "sessionDataProvider", parallel = true)
public static Object[][] sessionDataProvider(Method method) {
int len = 12;

Object[][] parameters = new Object[len][2];
for (int i = 0; i < len; i++) {
parameters[i][0] = i;
parameters[i][1]=len;

}
return parameters;
}

@Test(dataProvider = "sessionDataProvider")
public void executSessionOne(int input,int length) {
System.err.println("Test ID---" + Thread.currentThread().getId());
this.input=input;
this.length=length;
// First session of WebDriver
// find user name text box and fill it
System.out.println("Parameter size is:"+length);
driver.get().getDriver().findElement(By.name("q")).sendKeys(input + "");
System.out.println("Input is:"+input);

try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}

}

@AfterMethod
public void afterMethod() {
System.err.println("After ID" + Thread.currentThread().getId());
driver.get().close();

}


}

package demo;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;

public class dummy {

public WebDriver getDriver() {
return newDriver;
}

public void setNewDriver(WebDriver newDriver) {
this.newDriver = newDriver;
}

private WebDriver newDriver;

public dummy() {
newDriver = new ChromeDriver();
newDriver.get("https://www.google.co.in/");
}

@AfterClass
public void close(){
if(newDriver!=null){
System.out.println("In After Class");
newDriver.quit();
}
}
}

提前致谢。

最佳答案

private static ThreadLocal<dummy> driver在类级别添加。发生的情况是您已经在类级别声明了变量。即内存已分配给它。多个线程只是在设置和重置相同变量的值。

您需要做的是创建一个工厂,该工厂将根据传递给它的参数返回Driver的实例。逻辑可以是任何东西,但以一般用例为例,工厂将创建一个新对象并仅在现有对象存在时返回不存在。在@Test方法中声明并初始化驱动程序(出厂时)

工厂的示例代码将类似于

static RemoteWebDriver firefoxDriver;
static RemoteWebDriver someOtherDriver;


static synchronized RemoteWebDriver getDriver(String browser, String browserVersion, String platform, String platformVersion)
{
if (browser == 'firefox')
{
if (firefoxDriver == null)
{
DesiredCapabilities cloudCaps = new DesiredCapabilities();
cloudCaps.setCapability("browser", browser);
cloudCaps.setCapability("browser_version", browserVersion);
cloudCaps.setCapability("os", platform);
cloudCaps.setCapability("os_version", platformVersion);
cloudCaps.setCapability("browserstack.debug", "true");
cloudCaps.setCapability("browserstack.local", "true");

firefoxDriver = new RemoteWebDriver(new URL(URL),cloudCaps);
}
}
else
{
if (someOtherDriver == null)
{
DesiredCapabilities cloudCaps = new DesiredCapabilities();
cloudCaps.setCapability("browser", browser);
cloudCaps.setCapability("browser_version", browserVersion);
cloudCaps.setCapability("os", platform);
cloudCaps.setCapability("os_version", platformVersion);
cloudCaps.setCapability("browserstack.debug", "true");
cloudCaps.setCapability("browserstack.local", "true");

someOtherDriver = new RemoteWebDriver(new URL(URL),cloudCaps);
}
return someOtherDriver;
}

关于multithreading - 并行测试执行后,TestNG关闭浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35287122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com