Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何杀死Selenium网格节点中的线程_Java_Multithreading_Selenium Grid - Fatal编程技术网

Java 如何杀死Selenium网格节点中的线程

Java 如何杀死Selenium网格节点中的线程,java,multithreading,selenium-grid,Java,Multithreading,Selenium Grid,我正在Selenium网格中运行代码。 我有一个名为TestBase的类,如下所示,我想在单击按钮时退出所有线程,但当我单击按钮时,会抛出一个NullpointerException public class TestBase { protected ThreadLocal<RemoteWebDriver> threadDriver = null; Button b; Frame f; static boolea

我正在Selenium网格中运行代码。 我有一个名为TestBase的类,如下所示,我想在单击按钮时退出所有线程,但当我单击按钮时,会抛出一个NullpointerException

public class TestBase {    
        protected ThreadLocal<RemoteWebDriver> threadDriver = null;
        Button b;
        Frame f;
        static boolean  flag = true;

    @BeforeMethod
    public void setUp() throws MalformedURLException {
        if(flag)
        {
            JFrame calcFrame = new JFrame();
            flag = false;
            calcFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            final JButton button1 = new JButton("1");
            button1.addActionListener(new ActionListener() 
            {public void actionPerformed(ActionEvent ae) {closure();}
            });
            calcFrame.add(button1);
            calcFrame.setVisible(true);
        }

        threadDriver = new ThreadLocal<RemoteWebDriver>();
        DesiredCapabilities dc = new DesiredCapabilities();
        FirefoxProfile fp = new FirefoxProfile();              
        dc.setCapability(FirefoxDriver.PROFILE, fp);
        dc.setBrowserName(DesiredCapabilities.firefox().getBrowserName());
        threadDriver.set(new RemoteWebDriver(new URL("http://192.168.3.7:4444/wd/hub"), dc));             
    }

    public WebDriver getDriver() {
        return threadDriver.get();
    }

    public  void closure()
    {
        getDriver().quit();
    }

    @AfterMethod
    public  void closeBrowser() {
    //  log.info("In closeBrowser...");
        getDriver().quit();
    }
}
公共类TestBase{
受保护的ThreadLocal threadDriver=null;
按钮b;
帧f;
静态布尔标志=true;
@预处理法
public void setUp()引发畸形的DurException{
国际单项体育联合会(旗)
{
JFrame calcFrame=新JFrame();
flag=false;
calcFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
最终按钮1=新按钮(“1”);
button1.addActionListener(新ActionListener()
{public void actionPerformed(ActionEvent ae){closure();}
});
calcFrame.add(按钮1);
calcFrame.setVisible(真);
}
threadDriver=新的ThreadLocal();
DesiredCapabilities dc=新的DesiredCapabilities();
FirefoxProfile fp=新的FirefoxProfile();
dc.setCapability(FirefoxDriver.PROFILE,fp);
setBrowserName(DesiredCapabilities.firefox().getBrowserName());
threadDriver.set(新的RemoteWebDriver(新的URL)(“http://192.168.3.7:4444/wd/hub),;
}
公共WebDriver getDriver(){
返回threadDriver.get();
}
公众假期结束()
{
getDriver().quit();
}
@后置法
公共浏览器(){
//log.info(“在closeBrowser中…”);
getDriver().quit();
}
}

我猜它无法找到它必须退出的线程。请帮忙……)

就个人而言,我不会使用ThreadLocal。我将只使用受保护的成员变量,将受保护的WebDriver作为TestBase类的成员。然后,一旦您的会话在网格上启动,让您的测试记住会话ID,然后如果出现错误,您可以通过发送命令退出驱动程序以及该线程的会话ID来终止网格线程。我基本上是一个JSON请求。JSON wire协议有一个delete命令,如下所示:

DELETE /session/:sessionId

在我看来,通过使用ThreadLocal,您正在使用一些旨在处理本地线程的东西,并试图将该概念应用于一个已经有自己的线程处理的远程服务。我想我从来没有使用过ThreadLocal,因为我的testrunner为我做了线程分叉:TestNG。

非常感谢Alexei表现出兴趣,但我理解我的错误并解决了它。。。但真的非常感谢……!!::)我所需要做的就是在线程创建之后包含框架代码。。这样我就可以分别跟踪每个线程谢谢djangofan。。我会尝试这样做,因为在我使用线程时,我的脚本不如我目前使用的webdriver代码稳定。。如果你能给我提供一个演示代码,那真是太好了。。简单易懂。。因为我是网格新手。。这让我很困惑!好的,看看我编写的这个项目中的TestBase.doPrep()方法,希望它能启发您。该项目可以调整为使用本地电网而不是SauceLabs:非常感谢。。djangofan:)