Clicking on a button in my web page sometimes causes the entire page to load, and sometimes only part of it to load.
How can I call waitForPageToLoad without the page loading, and to be able to run additional commands after all elements are present, or what other command can I use, that will wait for the page to be loaded and enable me to run additional commands on the page.
(Using selenium 2.)
Clicking on a button in my web page sometimes causes the entire page to load, and sometimes only part of it to load.
I assume this is by design, and not the problem.
If you are testing, then you should know which behavior you are expecting. If you are expecting a full page load, then use clickAndWait. If you are expecting a partial load, then use click followed by waitForCondition.
You can use the wait() command to wait a specified amount of time, and continue with your actions afterward.
synchronized (driver) {
try {
driver.wait(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
It may be better to use clickAndWait or waitForCondition, but this is an alternative for just waiting in general.
Wait for element you want to proceed with instead of wait for page to load
Related
I want to run a java based automation test (using testng) on a windows application 24*7. The code is working fine but after some time the winappdriver responds very slowly and selenium's basic operation like clicking element is taking a lot of time unless I restart my windows machine. WinAppDriver gets stuck on the state shown in image.
winappdriver state
I tried driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS); so that if it could not find an element within 2 seconds then it should throw an exception but it is not working. I just want that if it is taking too long to perform an operation then it should throw an exception and my Retryanalyzer will restart the test but I dont know how to achieve this.
try {
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
do action
CustomReporter.log("\n action");
} catch (NoSuchElementException e) {
call reset method
CustomReporter.log("\n reset");
}
But using implicitlyWait is not a very good approach in general
Did you check memory usage? It would be better to check it
I have a Dynamic web page that refreshes and loads new data. I am using Selenium and I need to wait till the page has finished rendering before I can continue checking the page. I have seen many posts about waiting for the page to load or using explicit waits (implicit wait could solve the problem but is very unelegant and not fast enough). The problem with using explicit waits is not having any info on what will come up after the refresh. I have seen some solutions talking about waiting for all the connections to the server to end but that will not promise me that the actual UI has finished rendering.
What I need is a way to know if a page has finished to render (not load!!)
First of all you DO KNOW what sctructure will come after the page will load. You don't know what tags were not on the page, but you know what DOM will look like. That's why usually it's worst practice to test with page text, but a good one to have a website tested by DOM only. So, depending on what structure appears on the page you will need to have explicit ways waiting for specific tags will appear on the page.
One more solution (but as I told it's not really the best practice) is to wait for document.readystate is "complete". This state will not help in every situation, but if you have something still loading on the page, in more then half cases it will not return complete. So, you should have some kind of implicit state that is executing:
string readyState = javascript.ExecuteScript("if (document.readyState) return document.readyState;").ToString();
and then checking:
readyState.ToLower() == "complete";
btw if you will use protractor as an angular js application test executor, it's waiting for angular page loaded by default, or in some difficult situations you can use:
browser.waitForAngular();
OR do something in a callback
button.click().then(function() {
// go next step
});
I have had similar problem with the product I am testing. I had to create special functions for click,click_checkbox,sendKeys and getText for example. These include try catch methods for cathcing NoSuchElement StaleElementReference exceptions. Also it retries if the actions fail, like sending the wrong text.
document.readyState
This isnt enough for you. It only waits for the DOM to load. WHen the HTML tag are loaded, it returns "complete". So you have to wait for each individual item.
This works for me well with dynamically rendered websites - I do not have a time constraint in getting the data as it would run in background for me, if efficiency is an issue may be this is not the solution for you (50s is a huge time for any website to load after the ready state is completed) :
Wait for complete page to load
WebDriverWait wait = new WebDriverWait(driver, 50);
wait.until((ExpectedCondition<Boolean>) wd -> ((JavascriptExecutor) wd).executeScript("return document.readyState").equals("complete"));
Make another implicit wait with a dummy condition which would always fail
try {
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(),'" + "This text will always fail :)" + "')]"))); // condition you are certain won't be true
}
catch (TimeoutException te) {
}
Finally, instead of getting the html source - which would in most of one page applications would give you a different result , pull the outerhtml of the first html tag
String script = "return document.getElementsByTagName(\"html\")[0].outerHTML;";
content = ((JavascriptExecutor) driver).executeScript(script).toString();
I'm working on big liferay project and I have the following problem :
everytime when something is loading, processing etc. I should change cursor to waiting gif. It's easy when using Ajax, but in many cases here I don't use it. So I thought maybe if I could catch any action phase I'd somehow set cursor to wait at the beginning of action method and then turn back to regular 'auto' at the end.
Is that possible ? I don't like this 'solution' but I can't think of any better. Currently I have div with loading image in my jsp which is then removed by jquery document.ready() - not satisfied at all, because all the proccessing have been performed earlier in action phase.
I'd appreciate any suggestions.
Well, if you are not using Ajax, it might be difficult to achieve what you are looking for.
You could start showing the throbber ("loading image") once the link on the first page is clicked, keep it on the next page and remove it on document.ready().
But you'll still have a few ms or seconds without throbber, during page load.
If the processing time is really long, you could do the following :
The idea is to use a thread for the processing. A reference to the thread is stored in server's session. When the link is clicked :
Display a page with the loading image and start the processing in the thread
Poll the server every X second to check if the thread (in session !) is done
When the thread is done, display the result.
I am testing a web application which needs to refresh one of the header elements every 5 seconds. That header element updates all the users with a message whenever a Quote/Policy is issued through anyone using this same application in a group. So for that, our developers are doing AJAX calls continuously every 5 seconds on all pages and for all users.
For navigating through different pages, I was initially using implicit wait like,
DRIVER.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
After that continuous AJAX calls, I had to use WebDriver wait explicit function and look for object visibility when navigating through pages; like,
Wait<WebDriver> wait = new WebDriverWait(DRIVER, 20);
wait.until(visibilityOfElementLocated(By.id(objID)));
(Please note visibilityOfElementLocated is another function which also handles exception handling.)
My issue now is, this explicit calls code changes works great when I run the scripts in IE 8. But, when I run this on IE 9 it still behaves the same and endlessly wait for page to load (or wait for that AJAX call to finish). And, if I stop the browser calls (by pressing Esc or x link), my script continues for that page and hangs again for next page.
Any idea, why IE 9 doesn't work as IE 8 does for page load? Is there a way I can debug this?
PS: I tried updating IEDriverServer but in vain. Also, this works fine on Chrome and FF browser.
I can't say why IE 8/9 behave differently.
If you want to wait for AJAX to complete you can call a Javascript statement to check for active calls.
JQuery (which I assume you are using) has $.active which gives you the number of currently running ajax calls. So you can use the WebDriver as JavascriptExecutor and call this JS snippet:
return typeof $ === 'function' && $.active === 0;
When the result is true there is no AJAX in progress and you can continue with your test.
Looks like I figured the reason (sadly not the solution). It's a known issue in IEDriverServer of WebDriver for IE9.
Refer below link:
Issue 6402: IEDriverServer always returns document.readyState != complete for application using SignalR - library
https://code.google.com/p/selenium/issues/detail?id=6402
I'm using Selenium.waitforpagetoload method to wait in an ajax enabled webpage. When I run the test code, selenium waits even after the page has loaded and throws a "timed out waiting for action to complete" Exception. And I may have used waitforpagetoload in places where there isn't even a page load event. I presumed it just skips the part if the page is loaded. My code is like this
selenium.click("id=elmnt_id");
selenium.waitForPageToLoad("50000");
I'm using selenium-java version 2.33.
google Chrome 27.0
I found out that selenium.click() itself implicitly waits for pages to load after the click event. The timeout occurred inside click method. But the page loads after the click event in less than 10 seconds. selenium.click waits for more than 30 seconds and says timed out waiting for action to complete. Any advice is much appreciated. thanks!
I'm not sure exactly how org.openqa.selenium.internal.seleniumemulation.WaitForPageToLoad
works, but the package name says "internal.seleniumemulation" . I am not sure what that means but because I don't know what "emulation" means in this context, I would instead choose to use a different method of waiting for the page to load, such as a JavaScriptExecutor or using a FluentWait method in a loop until the element is ready.