Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Webdriver - How do I skip the wait for page load after a click and continue

I have an rspec test using webdriver that clicks on a button... after clicking the button, the page never fully loads (which is expected and correct behaviour). After clicking the button, I want to wait 2 seconds and then navigate to a different URL... despite the fact that the page has not loaded. I don't want to throw an error because the page hasn't loaded, I want to just ignore that, and continue on as if everything is good. The page should never load, that is the expected and correct behaviour.

How can I avoid waiting until the timeout, and secondly, how can I have that not throw an error which casuses the test to fail.

Thank you!

like image 327
TylerM Avatar asked Nov 05 '22 01:11

TylerM


1 Answers

WebDriver has a blocking API and it will always wait for page to be loaded. What you can do instead, is to press the button via JavaScript, i.e. trigger its onclick event. I am not familiar with Ruby, but in Java it would be:

WebDriver driver = ....; // Init WebDriver
WebElement button = ....; // Find your element for clicking
String script = "if (document.createEventObject){"+
      "return arguments[0].fireEvent('onclick');"+
  "}else{"+
    "var evt = arguments[0].ownerDocument.createEvent('MouseEvents');"+
    "evt.initMouseEvent('click',true,true,"+
    "element.ownerDocument.defaultView,1,0,0,0,0,false,"+
    "false,false,false,1,null);"+
  "return !element.dispatchEvent(evt);}" ;
((JavascriptExecutor)driver).executeScript(script, button);

After this you can wait for 2 seconds and continue

like image 167
Sergii Pozharov Avatar answered Nov 09 '22 15:11

Sergii Pozharov