1

I created few test cases on Selenium using Java. Unfortunately when I click an element on the page, before I could move on to any other action, I have to wait till the page loads.

I have tried driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);. Unfortunately this creates another problem. Even if the page loads, it waits 30 seconds before it started testing on the page.

What I found the best way is to send ESCAPE key to stop page load.

Is there anyway I could check if an element exists and when it does, send ESCAPE key to the browser to stop page load?

This part is bugging my mind as I have to wait till page loads before Java reads the next line of the code so I can't send ESCAPE key to browser till the page actually stops loading.

Edit

I have just tried using a new thread to do the job but it seems driver is completely locked out, can't do any process on it before page stops loading.

I'm out of ideas for the moment but I believe there should be a way.

Using timeouts() is causing whole test case to stop.

Revenant
  • 2,942
  • 7
  • 30
  • 52

3 Answers3

2

First I'd like to say this isn't a best practice. The selenium click method states that if the click triggers a page load, selenium will do its best to block until the page is loaded. Instead of clicking via the click method you could try sending the click event via JavaScript. Then wait for the element like normal.

Aaron Davis
  • 1,731
  • 10
  • 13
  • I usually do these kind of tests on new features / websites. Some of them are brand new. Sometimes I test over 100 times the same page (with different conditions). Wouldn't sending click event via JavaScript would increase bounce rate? I don't think everyone would like this if that's the case. – Revenant Aug 20 '16 at 16:36
  • You're running the tests in production? Then yeah, you'd have to check with whomever monitors analytics about the noise. Also might want to see about getting a test server to run the tests on to avoid corrupting analytics in general – Aaron Davis Aug 20 '16 at 16:40
  • That's often the case thus my concern. When I do tests on test / staging environment, I don't mind such issues. When it is on production environment I just want to be done with it as soon as possible. – Revenant Aug 20 '16 at 16:47
1

You can try driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); It is supposed to throw an error after timeout is over. I have never used it but maybe you can try and catch this error and continue with your test. But your page could end up in an unstable state with everything not loading and test interacts with elements.

Grasshopper
  • 8,908
  • 2
  • 17
  • 32
  • Yes it throws an error and then it doesn't fully function as expected. Not to mention if within 10 seconds, element is not present, this would create whole another level of issues. Which is why I want to wait till element is present and once it is, stop the page load with sending `ESCAPE` key. That's why I can't use `pageLoadTimeout` I also have tried it. – Revenant Aug 20 '16 at 16:10
  • What is taking time to load? Images? Have a look at this question specifically @kyrenia answer. It is for firefox though, http://stackoverflow.com/questions/7157994/do-not-want-images-to-load-and-css-to-render-on-firefox-in-selenium-webdriver-te – Grasshopper Aug 20 '16 at 16:29
  • Thanks for the link it will be useful for sure for some cases. It depends from page to page, sometimes css, images, js... external resources... It really chances not to mention I might need those resources available at least in most cases. – Revenant Aug 20 '16 at 16:31
0

I did it in C#, scenario is the same elsewhere. Define driver like this:

var firefoxOptions = new FirefoxOptions();    
firefoxOptions.PageLoadStrategy = PageLoadStrategy.None; 
driver = new FirefoxDriver(firefoxOptions);

PageLoadStrategy.None means when open a URL, continue to next line regardless of the results and do not wait to load the page.

Usually, it takes some seconds to load a page and element appears, suppose I'm waiting for email_user element to appears:

 int user_emailID = 0, popupAlert = 0;
 do
  {
    float timeToWait = 0;
    driver.Navigate().GoToUrl("https://stackoverflow.com");
    do
     {
     await Task.Delay(500);
       timeToWait += 0.5F;
       user_emailID = driver.FindElements(By.XPath("//input[@id=\'user_email\']")).Count;
     }
    while (user_emailID == 0 && timeToWait < 10);


    if (user_emailID == 1)
     {
      //Element exists now!do something and don't wait for page to load completely
     }

  }
 while (user_emailID != 1);

More explanation: when open a URL, first loop check the presence of element every 0.5 second, if it appears, the loop stops. If after 10 seconds it couldn't find the element, the page will reloaded !!

Hope this get you the idea.

Remember, exception must not happen in your codes !!

ehsan_kabiri_33
  • 341
  • 6
  • 13