0

I have a JavaFX button which executes following code (simplified)

Task<List<PubListEntry>> theTask = new Task<List<PubListEntry>>() {
    @Override
    protected List<PubListEntry> call() {
        dh = (DataHandler) selectedSource.getSourceClass().newInstance();
        return dh.extractInformation(...);
    }
};
Thread t= new Thread(theTask );
t.start();

It will create a new instance of a class depending of a selected entry of a ComboBox and then calls a method. In one of the classes I want to use Selenium to control a browser window.

public class Source1 implements DataHandler {
    public Source1 () {
        Browser.start();
    }
    @Override
    public List<PubListEntry> extractInformation(...) {...}
}

And the static method start look like this.

static void start() {
    if (driver == null) {
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }
}

But the task will fail if I instantiate Source1. So I think it has something to do with Selenium, but I can't figure it out what it is. Previously the Browser class was an abstract class and I had to extend Source1. Everything worked well with as an abstract class. Now I prefer if it would be an independent class. Does anybody have an idea why the Thread/Task ist failing?

JumbleGee
  • 85
  • 1
  • 9

1 Answers1

1

Let’s not reinvent the wheel. Here are some past references I had: https://groups.google.com/forum/m/#!topic/webdriver/cw_awztl-IM

Can Selenium use multi threading in one browser?

Hope it helps.

tsumit
  • 312
  • 2
  • 10
  • I don't want to do multithreading with the WebDriver neither does the WebDriver interact with different threads. I'm creating a thread because of the UI, otherwise it will freeze. So my intentions are (1) start a thread, (2) WebDriver does its job within the thread and (3) WebDriver finishes the job, close the browser and the the thread will close too. This is because WebDriver will only be used within the `extractInformation` method. I'm just wondering why it worked when the `Browser` class was abstract and now (as a static class) it doesn't? – JumbleGee Apr 08 '18 at 19:14
  • Why would the webdriver freeze if you do not put it inside a separate thread? Is your driver instance static? – tsumit Apr 08 '18 at 19:22
  • It's not because of the WebDriver, it is because of the other possible classes that don't use Selenium. Classes that don't use Selenium will get there information through a API call. Those classes will freez the UI during a call. Since I treat every class that implements `DataHandler` the same (first code example above), every `DataHandler` will get a new threat. And since all possible `DateHandler`'s are in a ComboBox only one class will be instantiated. – JumbleGee Apr 08 '18 at 19:34
  • The problem has probably to do something with the static WebDriver. Since it is not instantiated, so it will not be called within the same thread. Thanks anyway. – JumbleGee Apr 08 '18 at 20:10