【问题标题】:How to kill thread in a Selenium Grid node如何杀死 Selenium Grid 节点中的线程
【发布时间】:2014-03-27 11:58:22
【问题描述】:

我正在 Selenium Grid 中运行代码。 我有一个名为 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();
    }
}

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

【问题讨论】:

  • 非常感谢阿列克谢的兴趣,但我理解我的错误并解决了它......但真的非常感谢......! :)
  • 我所要做的就是在线程创建后包含框架代码..这样我就可以单独跟踪每个线程..:P

标签: java multithreading selenium-grid


【解决方案1】:

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

DELETE /session/:sessionId

通过使用 ThreadLocal,在我看来,您正在使用旨在处理本地线程的东西,并尝试将该概念应用于已经拥有自己的线程处理的远程服务。我想我从不使用 ThreadLocal,因为我的测试运行程序为我执行线程分叉:TestNG。

【讨论】:

  • Thankyou djangofan.. 我会尝试这样做,因为当我使用线程时,我的脚本不像我目前使用的 webdriver 代码那样稳定.. 如果你能提供的话,你会真的很好我有一个演示代码.. 很容易理解.. 因为我是 Grid 的新手.. 这对我来说很困惑!
  • 好的,看看我写的这个项目中的TestBase.doPrep()方法,希望对你有所启发。该项目可以调整为使用本地 Grid 而不是 SauceLabs:github.com/djangofan/simple-selenium-builder-framework
  • 非常感谢.. djangofan :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-27
  • 2013-08-19
  • 1970-01-01
  • 2012-03-07
  • 2010-10-10
  • 2011-06-29
  • 1970-01-01
相关资源
最近更新 更多