I'm reading the docs on Implicit Waiting with Webdriver but I'm not sure I totally understand.
As I understand it,
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
This will put in place a timeout of 10 seconds whenever looking up any element.
What, exactly, does this do?
Does it mean that when looking up any element, I will wait 10 seconds every time, even if the element is present? Or, when looking up any element, will the driver give the browser a 10-second grace period to load the element before deciding the element missing?
Since this is applied to every element, am I correct in supposing that this is just executed only once for each instance of Webdriver?
This will look for the element up to 10 seconds, trying to locate it every 500 milliseconds (default timeout).
From the docs
Specifies the amount of time the driver should wait when searching for
an element if it is not immediately present.
When searching for a
single element, the driver should poll the page until the element has
been found, or this timeout expires before throwing a
NoSuchElementException. When searching for multiple elements, the
driver should poll the page until at least one element has been found
or this timeout has expired.
The locating algorithm is described in W3C specifications
The Find Element, Find Elements, Find Element From Element, and Find
Elements From Element commands allow lookup of individual elements and
collections of elements. Element retrieval searches are performed
using pre-order traversal of the document’s nodes that match the
provided selector’s expression. Elements are serialized and returned
as web elements.
When required to find with arguments start node, using and value, a
remote end must run the following steps:
Let end time be the current time plus the session implicit wait
timeout.
Let location strategy be equal to using.
Let selector be equal to value.
Let elements returned be the result of trying to call the relevant
element location strategy with arguments start node, and selector.
If a DOMException, SyntaxError, XPathException, or other error occurs
during the execution of the element location strategy, return error
invalid selector.
If elements returned is empty and the current time
is less than end time return to step 4. Otherwise, continue to the
next step.
Let result be an empty JSON List.
For each element in elements returned, append the serialization of
element to result.
Return success with data result.
implicitlyWait is defined once for the WebDriver and last its lifetime.
Defines a wait time globally in your project. Your telling your driver to wait for n number of seconds before selenium throws an exception. If element is found earlier then the n number of seconds you mentioned, webdriver will click it once its available and dont wait for the maximum n number of seconds. It does have to wait before it throws an exception.
Related
I have been testing with element.IsPresent(), for some reason my test start failing when trying to do browser.wait(element(by.id('id').IsPresent()). It never get away from the wait, even the element being present.
I started using the code with protractor.ExpectedConditions and it start working. just want to know what is the difference between one and the other.
Here is the code with the Expected Conditions.
const EC = protractor.ExpectedConditions;
const ele = element(by.id('id'));
return browser.wait(EC.presenceOf(ele));
What is the main difference between one and the other? I have search in google but haven't found a proper answer.
If you take a look at this answer on another question you will see that both presenceOf() and isPresent() are almost entirely the same. The primary difference being that presenceOf() wraps around isPresent() and returns a Function rather than a Promise.
So why is this important? Well it has to do with how browser.wait() works. If we take a look at the docs we can see that it:
Schedules a command to wait for a condition to hold or promise to be resolved.
This means that if you pass a Promise to browser.wait() it will only wait until that Promise is resolved before continuing on and executing further commands (it doesn't necessarily matter if it resolved true or false). Whereas if you pass a Function to it, it will wait until that condition "holds" before continuing.
Additionally, you can specify a custom timeout parameter for the browser.wait() method. If you do not specify a timeout it will default to 30 seconds according to the docs. I believe this is why you felt that the wait never resolves when using isPresent() (it was likely just taking 30 seconds).
What I would suggest to do is use isPresent() when you expect an element to be present at a specific moment in time and use presenceOf() when you want to wait for an element to be or become present.
Here's an example of how I would use the two:
const EC = protractor.ExpectedConditions;
const ele = element(by.id('id'));
browser.wait(EC.presenceOf(ele), 5000); // Waits a maximum of 5000 milliseconds (5 seconds)
expect(ele.isPresent()).toBe(true); // Expects this element to be present **right now**
When i write some implicit wait like 10 sec and then for a element i give 5 seconds time in explicit wait...then will the implicit wait become zero and only wait for 5 sec or it will add the implicit wait time as well means will it wait for 15 secs
Let me answer you one by one:
Whenever you put an ImplicitlyWait Selenium will perform that wait after each action. So it becomes contagious.
When you put an Explicit Wait, those are defined as per certain conditions like "visibility of an element" within until block.
So each type of wait behaves in their own fashion just by the way you implement them.
Once you assign timeunits for each type of wait, they are followed in execution. They never gets added or substructed.
ImplicitlyWait is mentioned only once within your code. This instruction is for the Webdriver to follow. ImplicitlyWait have no effect on any element in particular.
Where as Explicit Wait is assigned to ask the Webdriver to wait for a defined time period (e.g. 5 seconds) with a until condition which specifies the state of an element (e.g. element_to_be_visible) the Webdriver should look for.
Now answering your question: 10 seconds ImplicitlyWait for a element is not a valid statement. Explicit Wait of 5 second you have to put for a certain state/behavior of the element (e.g. element_to_be_clickable). If the element doesn't shows that behavior within the defined time slot Selenium will throw an exception.
Let me know if this answers your question.
This question already has answers here:
Combining implicit wait and explicit wait together results in unexpected wait times
(2 answers)
Closed 8 years ago.
I understand both waits and use of it. But one thing that I could not figure out is : If I put implicit wait for 5 seconds at top and then use explicit wait for an element. Then how selenium will behave. Tried but could not get satisfactory answer on net.
First understand the concepts of Explicit and Implicit wait
Implicit Wait: An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
For Example:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
Explicit Wait:
An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code.
There are some cases where the explicit wait is functionally equivalent to implicit wait, means
a) Where waiting time is not predefined like below (please note they were distinct by method category they belong explicit or implicit)
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
b) Cases where the webdriver has a time to wait 10 seconds but at the 5 second the element was found then the webdriver will proceed.
Answer of your Question:
1) Suppose you have defined 10 seconds then driver waits for 10 maximum but at minimum it can wait for 0.001 seconds means in cases of implicit wait we have to give maximum limit for wait while minimum limit depends on the finding the element or getting the condition done.
2) While in explicit wait except some case webdriver has to wait for maximum limit.
So, as a answer your webdriver first will follow the implicit wait and then follow the explicit wait since browser behavior will be sequential like other programing languages due single thread usage.
Reference :
Selenium Explicit and Implicit wait
What is the difference between Thread.Sleep() and selenium.setSpeed("2000")?
setSpeed : Set execution speed (i.e., set the millisecond length of a delay which will follow each selenium operation). By default, there is no such delay, i.e., the delay is 0 milliseconds.
Thread.sleep : It causes the current thread to suspend execution for a specified period.
So the main difference between them is setSpeed sets a speed while will apply delay time before every selenium operation takes place. But one thread.sleep() will set up wait only for once. So, if we have 3 selenium operations written like below:
Opeartion 1
Opeartion 2
Opeartion 3
and we want to set a delay time 2000 for each of these, defining setSpeed() method once will accomplish the task something like below:
selenium.setSpeed("2000");
Opeartion 1
Opeartion 2
Opeartion 3
But if we use Thread.sleep(), it will be something like below:
Thread.sleep(2000);
Opeartion 1
Thread.sleep(2000);
Opeartion 2
Thread.sleep(2000);
Opeartion 3
Thread.sleep() will stop the current (java) thread for the specified amount of time. It's done only once.
Selenium.setSpeed() will stop the execution for the specified amount of time for every selenium command. It is useful for demonstration purpose (you will see thing moving in your browser) or if you are using a slow web application (there are better technique to handle slow applications but that's off topic.)
I face some issue whereby the test just hang there (Browser open and not able to proceed next test) due to my test statement not able to find the element.
My TestStatemet like this:
driver.findElement(By.xpath("//input[#name='AID' and contains(#value,'sampleDataThatwillNotFound')]"));
The test hang only when find by XPATH, no issue when find by NAME/ID. I had set the timeout to 60 seconds, after the 60 seconds, it still hang.
Anyone out there facing this issue before? or Anyone got any idea how to resolve this issue?
Well, I had the same problem and found this in webdriver api doc:
findElement should not be used to look for non-present elements, use findElements(By) and assert zero length response instead.
So I use something like
List<WebElement> found = driver.findElements(By.id("elementid"));
if (found.size() > 0)
{
// get the 1st element
} else {
// time out
}
to resolve this issue. And implicit timeout works fine with findElements in my case.
I was having the same issue after upgrading Firefox (25 to 26) and Selenium (2.37.1 to 2.39.0 driver + server). No Exception thrown, hang forever, etc. It got "solved" by removing the implicitlyWait declaration. Not a real solution but good enough in my case.
try this one locator
driver.findElement(By.xpath("//input[#name='AID'][contains(#value,'sampleDataThatwillNotFound')]"));
I got some feedback from darrell from Google Groups, and i agree with him, below is his feedback:
https://groups.google.com/forum/#!topic/webdriver/Vt0DuQHOAg8
*I haven't seen these locators hanging but anything is possible.
Generally, if the DOM is large and/or complex a combination locator
(one with contains plus an and) could make it really slow. My general
experience has been the more complex the locator the long it takes.
The longer it takes the more likely you will see a
NoSuchElementException. It could be that something else you are doing
is causing a second problem, i.e. the hanging.
An and statement is multipling. So #name='AID' is relatively fast.
There is no substring checking. It either matches or it does not. So
this locator will run in order n, where n is the number of input tags.
A locator like contains(#value, 'someString') will have to scan each
tag for each attribute for a match to every possible substring. If the
contains() is implemented well it could be a little faster than brute
force but the type of data in the DOM will determine how long this
search will take. It will definitely be slow.
Now if you take the contains() search (relative slow) and the exact
match (relatively fast) then look for the AND of the two matches, you
are multiplying them. Two exact matches are going to be order n times
order n (or n squared). This is not good. An exact match times a
contains is REALLY bad. Depending on the DOM it could be order n
cubed. This means if n takes 10 seconds, n cubed is 10 * 10 * 10
seconds (1000 seconds or over 16 minutes). If the DOM causes things to
be even worse you could be looking at exact match is seconds and
combination is hours.
Darrell*
So to solve this issue, i think it is time to enforce the development team to apply to the common development practise to put in unique id for every element/control. So that the test automation script can directly perform any verification/input thru ID, instead of xPath.
I had try some answer above, but still having same issues.. i switch back to old firefox version 17.0.3 .... everything just solve.. abit funny.. those that facing the same issue you can try at FF Version 17.0.3