I'm looking for a solution to implement FluentWaits in my java selenium test. The problem is that is use ThreadLocal to declare my drivers as thead-local to run them parallely.
Here is my code :
//My variable declaration
protected ThreadLocal<RemoteWebDriver> threadDriverFirefox = null;
//I create one for my thread in my BeforeTest
threadDriverFirefox = new ThreadLocal<RemoteWebDriver>();
threadDriverFirefox.set(new RemoteWebDriver(new URL(urlnode), DesiredCapabilities.firefox()));
//Add this method to get my driver
public WebDriver driverFirefox() {
return threadDriverFirefox.get();
}
//And use it like this in my test
driverFirefox().get(weburl);
My problem is with the driverFirefox() I can't find a way to implement it in the FluentWait structure :
Wait waitfluent = new FluentWait(driverFirefox()).withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(2, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);
WebElement testElement = waitfluent.until(new Function() {
public WebElement apply(WebDriver driverFirefox() ) {
return WebDriver driverFirefox().findElement(By.id("logEmailField"));
}
});
But i've a syntax error...
Multiple markers at this line - Syntax error on token ")", delete this token - Syntax error on token(s), misplaced construct(s)
Any idea to fix it ?
Thanks
If you use a ThreadLocal, it should be static, otherwise you could introduce nasty memory leaks. And if the ThreadLocal is static, it should be accessed that way. Further, as the reference is shared, you shouldn't initialize it by a thread (the reference) but only the value (calling set()
).
public class TestContext {
static ThreadLocal<WebDriver> CURRENT_DRIVER = new ThreadLocal<>();
public static WebDriver currentDriver(){
return CURRENT_DRIVER.get();
}
public void beforeTest(RemoteWebDriver driver) {
CURRENT_DRIVER.set(driver);
}
}
Now you can define the fluent wait using the shared driver:
Wait waitfluent = new FluentWait(TestContext.currentDriver())
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(2, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);