1

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

下面是我的工作代码

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();
        }
    }
}

提前致谢。

4

3 回答 3

2

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;
}
于 2016-02-19T10:04:05.303 回答
1

您有一个并发问题:多个线程可以创建一个ThreadLocal实例,因为并行运行时dummy == null可以评估true多个线程。因此,一些线程可以执行driver.set(new dummy());,但随后另一个线程替换driver为新ThreadLocal实例。

以我的经验,它更简单,更不容易出错,总是ThreadLocal用作static final确保多个对象可以访问它(static)并且它只定义一次(final)。

您可以查看我对以下 Stack Overflow 问题的回答,了解相关详细信息和代码示例:

于 2016-02-09T10:49:54.353 回答
0

发生这种情况是因为您正在函数中创建驱动程序实例,beforeMethod因此它的范围在函数结束后结束。

因此,当您的 afterMethod 启动时,它会变为 null,因为 webdriver 实例已经销毁,因为beforeMethod功能已经完成。

请参阅以下链接:-

http://www.java-made-easy.com/variable-scope.html

Java中方法的默认范围是什么?

于 2016-02-09T09:17:21.810 回答