How to kill thread in a Selenium Grid node

耗尽温柔 提交于 2019-12-02 16:50:23

问题


I am running a code in Selenium Grid. I have a class named TestBase as follows and I want to quit all the threads when I click on a button,but when I click on the button a NullpointerException is thrown.

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

I guess it is not able to find the thread which it has to quit. help please.. :)


回答1:


Personally, I wouldn't use ThreadLocal . I would just use protected member variables, having protected WebDriver being a member of a TestBase class. Then, once your session is started on the grid, make your test remember the session ID, then if there is an error, you can kill the Grid thread by sending a command to quit the driver along with the session id for that thread. I would basically be a JSON request. JSON wire protocol has a delete command that looks like this:

DELETE /session/:sessionId

By using ThreadLocal, in my opinion you are using something that is meant to handle local threads and trying to apply that concept to a remote service that has its own thread handling in it already. I guess I never use ThreadLocal because my testrunner does the thread forking for me: TestNG.



来源:https://stackoverflow.com/questions/22686920/how-to-kill-thread-in-a-selenium-grid-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!