Problem statement: I see test case is not getting failed when the method is executed inside java thread but the same case failed when executed without thread. This leads misleading results in test automation. Could someone help me to know the root cuase.?
Expected: Exception should be thrown and test case must be failed.
Actual: Exception is thrown but test case is not failed if method is executed inside thread.
With Thread:-
public void test()
{
Thread t1 = new Thread(() -> {
obj.js_FindElement(2, "foo",5);
});
t1.start();
t1.join();
}
Output:-
Exception in thread "Thread-10" org.openqa.selenium.InvalidElementStateException: invalid element state: Failed to execute 'querySelector' on 'Document': '#fooboo]' is not a valid selector.
Result: Passed
Without thread:-
public void test()
{
obj.js_FindElement(2, "foo",5);
}
Output:-
[ERROR] Errors:
[ERROR] invalid element state: Failed to execute 'querySelector' on 'Document': '#fooboo]' is not a valid selector.
Result: Failed
Method:-
@Step
public WebElement js_FindElement(int sequence, String element, int timer) {
JavascriptExecutor js = driver(sequence);
WebElement elem = (WebElement) js.executeScript("return " + element + "");
int i = 1;
while (elem == null && i <= duration(timer)) {
elem = (WebElement) js.executeScript("return " + element + "");
i += 1;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
return elem;
}