How to use implicit wait and explicit wait together? - selenium

In my framework we are using singleton driver and we are mainly using explicit wait. As documentation suggests we shouldn't use both together but at some points because of synchronization issues I need to use implicit wait.
In that case I found a workaround, I add implicit wait to my driver, then at the end of this test I just create brand new driver.
But I would like to know if there is any other way to do that ?

As per the documentation:
Warning: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example, setting an implicit wait of 10 seconds and an explicit wait of 15 seconds could cause a timeout to occur after 20 seconds.

Related

For some reason, I want to use thread.sleep in my serenity screenplay project, how can I use it?

I am already using implicit and fluent wait, but want to use thread.sleep, so want to know the syntax of it
Using Thread.sleep() is discouraged and there is no Performable for that in serenity.
Many testers pepper their web tests with Thread.sleep() statements, but this is the sub-optimal at best. It slows down the tests, makes them more brittle, and can hide genuine application issues. More insidiously, when our steps are artificially slowed by sleep statements, it takes longer not only to run and troubleshoot existing tests, but also to develop new ones.
https://johnfergusonsmart.com/handling-waits-and-asynchronous-pages-in-serenity-bdd/
I know my reply is very late and it's a very bad practice but I am posting here just for the sake of if it can be done. Also from your question it seems like you want to make a task of it. To do this you can make a anonymous task. For example
Integer wait = 500;
Task.where("{0} uses thread sleep",(actor)-> {
try {
Thread.sleep(wait);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
You can wrap it inside a function or assign it to a Task variable. Just if you are wandering Task class have a overloaded method named where which takes a Consumer<Actor> as second argument instead of Performable.
Also you can make a normal task class by implementing Task and use thread sleep in performAs method.
But again considering you are using serenity I doubt it will come to the point where you would have to use Thread.sleep.

what is need of explicit wait if we can set implicit wait for more amount of time at once? [duplicate]

This question already has answers here:
How to combine implicit and explicit timeouts in Selenium?
(1 answer)
Replace implicit wait with explicit wait (selenium webdriver & java)
(5 answers)
How to properly configure Implicit / Explicit Waits and pageLoadTimeout through Selenium?
(3 answers)
Closed 3 years ago.
we declared implicit Wait of 10 secs but an element takes more than that, say 20 seconds and sometimes may appears on 5 secs, so in this scenario, Explicit wait is declared.
anytime implicit wait don't wait for the default time it will stop waiting once element is visible
It's generally bad practice to mix your implicit and explicit waits -- rather, you should stick with doing one or the other, and not both, if possible.
Based on the problem you described -- you can increase your implicit wait to 30 seconds to account for long loading times, and the wait will be finished even if the element only takes 5 seconds to load.
I prefer explicit wait because I can perform negative wait too. Sometimes, I want to wait until a certain element is NOT visible on the page. With explicit wait, my wait is finished as soon as element disappears. However, with implicit wait, you will have to wait the full 30seconds to know if element has disappeared or not.
Differences between implicit wait and explicit wait in selenium webdriver :
Implicit Wait
Applied on entire page
Once you declared implicit wait it will be available for the entire life of web driver instance
Wait is suggested
Implicit Wait is applicable for all web elements that are on a web page
No conditions involved
Only checks for the presence of web elements
Explicit Wait
Applied on an element
It will be used if we want the execution to wait for some time until some condition achieved.
Wait is directly expressed
Explicit wait can applied against a single or multiple web elements
Involves conditions provided by ExpectedCondition class’s static methods
Waiting period with certain conditions.

In selenium,If we add both implicit(10sec) and explicit wait (5sec) in script then which wait is applicable to element

I would like to know ,In selenium,If we add both implicit(10sec) and
explicit wait(5sec) in script then which wait is applicable to element
The Documentation clearly mentions :
Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times.
For example setting an implicit wait of 10 seconds and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds.
Either use Explicit OR Implicit Waits, dont use both at once
If you use both then
our 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.
Explicit and Implicit Waits
Waiting is having the automated task execution elapse a certain amount of time before continuing with the next step. You should choose to use Explicit Waits or Implicit Waits.
**
WARNING: Do not mix implicit and explicit waits. Doing so can cause
unpredictable wait times. For example setting an implicit wait of 10
seconds and an explicit wait of 15 seconds, could cause a timeout to
occur after 20 seconds.
**
Read Selnium DOC
If you are really in need with Explicit wait (i.e. wait until element to be clickable, invisible etc), you need to add below code above the explicit wait.
driver.manage().timeouts().implicitlyWait(0);
Once done with explicit wait, revert the implicit wait as much wait time you required for further elements. This will avoid cumulative wait time of both implicit and explicit wait.

Speeding up find_elements_by_css_selector in selenium

With reference to this question https://sqa.stackexchange.com/questions/3481/quicker-way-to-assert-that-an-element-does-not-exist I'm trying to write an element_is_not_present helper function:
def element_is_not_present(selector):
self.d.implicitly_wait(0)
number_present = len(self.d.find_elements_by_css_selector(selector))
self.d.implicitly_wait(10)
return number_present == 0
find_elements_by_css_selector will wait for the implicit wait to timeout before returning so I have added mutators before and after the call to speed up the method. The issue comes in restoring the implicit wait, apparently there is no getter for the implicit wait value, so I must hard code the restore value causing issues when using a different base values.
The question is, how can I speed up my method whilst negating this issue? Failing this, maybe there is another way of writing the helper method or dealing with the explicit wait?
My current solution is to use my own implicit wait default value global but this is far from ideal.
the short answer is: do not use implicit wait. use explicit wait in all your code.
the bit longer answer: this is exactly one of the problems why people, me included, advise you to not use implict wait. using implict wait there is no way to test for absence of element without waiting for the timeout. and the only way to avoid that is by disabling the implicit wait during the absence test. or by using explicit wait.
here is even more longer answer: When to use explicit wait vs implicit wait in Selenium Webdriver?
also interesting: How to test for absence of an element without waiting for a 30 second timeout

When To Use Implicit wait and where to Use

I am having some confusion regarding Implicit wait method provided by Selenium Webdriver.
When to Use Implicit wait
a- For Page Load (when Using driver.get) or for Ajax PopUp Load Like let say I am entring something in Edit Box and some Look up or Ajax call is happening.
Where To Use Implicit wait
Should I use after all the methods wherever Ajax call or Page load happening or only once it is enough (I am just taking the reference from Selenium RC where We can Use Selenium.SetSpeed Method).
Thanks,
Arun
An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.Example is as follows:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
However, depending on the language implementation varies a little bit. See here more about ExpectedCondition
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. Below is an implementation of implicit wait:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Both of these definitions are from seleniumhq and most perfect definition out there.
There is a great explanation in toolsQA how and when to use them. Plus a comparison between implicit, explicit and FLUENT waits which are worth taking a look.
Implicit wait and Explicit wait are related to the driver instances which we are using in our program. First about explicit
wait we can use explicit wait for particular condition/Web-element to
happen/click and its life depends on that Wait object created.
Example:
WebDriverWait explicit_wait_Example = new WebDriverWait(driver, 10);
explicit_wait_Example.until(ExpectedConditions.elementToBeClickable(By_Locator)).click();
Above is the example of using explicit wait with until which is one of the most efficient and effective ways to use such kind of wait.
Second, when it comes to Implicit wait, this wait goes with the Life
of the driver instance. Just declare it for one time and you can use it wherever driver instance is invoked.
About the question you mentioned, Whether after Ajax call or page
load, so I would suggest to check how many instances you have created of driver class. because once you declare implicit wait for one driver instance then you don't have to declare again for that particulate driver instance.
Implicit wait is used in the program when you are sure about the time
taken by all web-elements on the web-page to load/visible and for
certain Web-elements which you find time as one the varying factor in
it's loading then you can use explicit wait.
For ajax call, I would prefer Explicit wait. But if you can figure out what is the min timestamp of your ajax calls you can provide in implicitlyWait.
Implicitwait is enforced on the driver permanently. So u need not declare again and again. It would affect the driver to wait for a particular time until it throws NoSuchElementException. But if you are using xpaths more, then it would be better you provide greater timeouts in implicitly wait.
Another thing to add, implictlyWait affects only findElement and findElements functions. Other functions are unaffected of it.