How to kill thread in a Selenium Grid node
Asked Answered
P

1

0

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.. :)

Passel answered 27/3, 2014 at 11:58 Comment(2)
Thanks a lot Alexei for showing interest but I understood my mistake n solved it... but really thanks a ton..!! :)Passel
All I had to was include the frame code after the thread creation.. so that i can keep track of each thread seperatly.. :PPassel
I
0

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.

Inboard answered 28/3, 2014 at 21:5 Comment(2)
Thankyou djangofan.. I would try that because as m using thread my scripts are not as stable as webdriver code, which I was using so far.. It would be really kind of u if u could provide me with a demo code.. simple to understand.. As I am new to Grid.. its pretty confusing for me!Passel
Ok, look at the TestBase.doPrep() method in this project I wrote and hopefully it enlightens you. The project could be adjusted to use a local Grid instead of SauceLabs: github.com/djangofan/simple-selenium-builder-frameworkInboard

© 2022 - 2024 — McMap. All rights reserved.