I have my code written in Selenium for automating my application[Loading time of it varies] and it works fine when we use Thread.sleep for its loading time.I need to move out of thread.sleep as it is not ideal way of writing code,so i have tried to use all waits[implicit,explicit and fluent] given by Selenium.
In spite of that the script is not consistent and it is failing very often[mostly on element not found and stale element exception],so need to know since i have my all frameworks and code in Selenium is that any provision that i can import protractor jar file in it and use some lines of code in order to make my application to get synchronize with my script.
Your implicit wait is not gonna help , if your code works fine with Thread.sleep().
Thread.sleep(time) is an extreme case of Explicit wait.
An explicit wait is code you define to wait for a certain condition to occur before proceeding further in the code.
In Java selenium binding we can defined and use Explicit wait like this :
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully.
Note that : Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times.
Question : mostly on element not found and stale element exception occurs?
Just verify your locators. Locators should be static not dynamic. Just for example you should never use dynamic ID of any web element which is generated by JS or any other programming language.
Using protractor, you can't just sync your script. All you can do now is to make changes to your locator and waiting time.
Hope this will help you.
It is not possible to use protractor into Selenium, but you can rewrite all tests into protractor and you will inherit all from selenium. Protractor is wrapper for Webdriverjs so you get each and every faeture of webdriverjs in protractor + protractor's own feature of locators and asynchronous waits.
Related
I have many locators and if any one among them fails the program fails, We can not get a list of locators failed.
is there some way/method to get all the invalid locators in selenium automation???
The easy way (in case of you're using a framework and the code is well structured) :
Add the action performed on any locator into a try-catch block and store locator's name into a file, log it into console etc (it's up to you). In this way, you can follow up and update them after.
The manual way:
Go manually trough all your locators and look after them in HTML code in browser's console
The less painful manual way:
Execute all tests, and from your report (if you're using any reporting tool) or from console, collect and check each locator .
Now, if you're just doing a clean-up, I recommend the first approach. If you're facing issues with the locators, flaky situations, I recommend creating dynamic locators (xpath or css).
I have a doubt related to the implicit wait of selenium? As we know that Implicit Wait is dynamic wait that means if we mention that wait for 10 seconds for any element to be loaded but if the element is loaded within 4 seconds then driver comes out of the wait.
So, the question is that how driver came to know that element is loaded in 4 seconds and lets come out from the wait? We have not mentioned any condition in Implicit Wait like look for the visibility of any element and then come out then how exactly implicit wait takes a call to to come out of the wait?
The implicit wait is happening at the driver level, Explicit wait is happening at the programming level. Most people are not aware of this.
Implicit wait only checks whether it exists or not, so you don't have to specify any condition as you do for an explicit wait. But I have raised a ticket to include the implicit wait for visibility as well in Chrome(See here https://bugs.chromium.org/p/chromedriver/issues/detail?id=2016) and Firefox, Chrome has incorporated that change but firefox still hasn't. I asked them to include it because Firefox Legacy driver waits for element existence and visibility so I want the same to be here.
Implicit Wait can't be handled dynamically using Selenium. In his epic comment Jim Evans [Maintainer - DotNet client / IEDriverServer] mentioned that, implicitlyWaits are often (always may not be) implemented on the remote side of the WebDriver system. That means ImplicitWait are baked in to the WebDriver variants i.e. GeckoDriver, IEDriverServer, ChromeDriver, the WebDriver Firefox extension that gets installed into the anonymous Firefox profile (Selenium RC), and the Java remote WebDriver server (selenium-server-standalone.jar).
Once you set the implicitly_wait, the WebDriver instance would be able to carry this configuration till its lifetime. To set the timespan of the waiting time, you can:
Python:
driver.implicitly_wait(5)
Java:
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
DotNet:
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
If at any point of time you want to remove the ImplicitWait you can achieve it as follows:
Python:
driver.implicitly_wait(0)
Java:
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
DotNet:
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(0);
I'm automating functionality of My web project . I've created one test suit using selenium Webdriver which is working fine on Local Server(created on same). But while I do execute the same on Azure server(obviously slow as compared to local environment), The script get failed and I need to debug the whole script and need to put some wait where its taking time to locate the element. And after all changes on azure server some time it failed on local server too.
How to deal with slow website response ?
Is there any effective way of scripting which required less effort to execute the script on different environment ?
You can set Implicit wait (less desired) or Explicit waits.
var driver = new ChromeDriver();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(1000));
driver.FindElement(By.XPath("your xpath")).Click();
In this case, you're 'telling' Selenium to wait the specified timeout before it will search the element you want to be found. If the element was not found, you will get the Timeout error.
var driver = new ChromeDriver();
driver.FindElement(By.XPath("your xpath")).Click();
In this case you're telling Selenium to get the element automatically, so in case it will not be found, you will get a NoSuchElementException, with no timeouts because there's no time to wait before the element to be present.
And if we're expanding it to the Explicit wait:
var driver = new ChromeDriver();
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 1000));
wait.Until(d => d.FindElement(By.XPath("your xpath"))).Click();
In this case you're telling Selenium to keep searching for the element until it is found but no longer than the maximum timeout specified as a param to WebDriverWait.
Let's take for example the case where on your local environment a page loads on 1 minute and on Azure it takes 10 minutes (far stretched example), if you're going ahead with the Explicit wait, then you could set the timeout to let's say 11 minutes.
Those 11 minutes are the maximum timeout in which Selenium is polling for a specific element to be found. However, if you run your tests on the local environment and the element is found in 30 seconds, then your test will continue with the execution. It will not wait for the maximum timeout to complete before doing that.
It's most likely not time to locate the element, it's probably the speed of pages loading. Use WebDriverWaits for page transitions and any dynamic page changes. Once you wait for an element or dynamic section of the page to load, you can interact with the page without having to worry about speed. That will probably take care of most of your issues but without more specifics, it's hard to say what else might be going on.
please help me in understanding the following issue. please.
I have to get all links and check them later. I used the following code:
open(url);
List<String> links = new ArrayList<>();
for (SelenideElement link : $$("a"))
links.add(link.attr("href"));
when I used this with Linux with these api version:
Maven 3.1
Selenide v3.5
Selenium v2.53
Firefox v45.0.1
Then code can't take time enough to catch links from the page. Then I have to add driver wait before get links.
I Add the following (which is conditional wait):
WebDriverWait waitLog = new WebDriverWait(WebDriverRunner.getWebDriver(), 20);
waitLog.until(ExpectedConditions.visibilityOf($(By.tagName(Selector))));
And it worked fine and I run it more than one time.
I got surprised when running it yesterday, it didn't work and can't get time enough to get links!
So I replace the conditional wait with implicit wait, and add the following:
WebDriverRunner.getWebDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Now it is working fine.
What happened?
What is this thing make it work sometimes and sometimes can't work?
How to return back to the conditional wait with keeping the code working well?
So how to recover this problem? and prevent this problem from happens in the future.
There might be some delay in loading the url for the second time. Please try increasing the delay time for conditional wait. The main difference between explicit and implicit wait is as follows.
Explicit or conditional wait stops the WebDriver for the specified amount of time, till the mentioned element is available. Whereas implicit wait will skip execution of WebDriver for specified amount of time, for every element which is not found on the page.
Hope this helps.
I am interested in the default value of timeouts on selenium webdriver.
ImplicitlyWait, SetPageLoadTimeout and SetScriptTimeout.
Because I want to know, Do I need to set a values for those timeouts? or the default value is good for selenium webdriver working.
But I cannot find a correct answer, someone say the default value is 0, and other one say it is 30 sec.
These three timeouts are managed by the server-side of the Selenium equation. Your script, be it in Java, Python, Ruby, C#, or whatever, is a client that sends commands to a server that lives in the browser. (There may be an intermediary that relays commands to the browser, like Selenium grid. Unfortunately, it is also sometimes called a "server".)
The WebDriver specification, which was derived from Selenium has settled on the following values:
For implicit waits: 0 seconds. This means that if a selenium command does not find an element immediately, it reports immediately, rather than wait until an element is found.
For page loads: 300 seconds.
For script timeouts: 30 seconds.
(The specification gives the values in milliseconds. I've converted them to seconds for ease of reading.)
Selenium now follows the WebDriver specification.
In the past Selenium has used other values for these, however. For instance, the Firefox driver used to define its timeouts like this:
The implicit wait timeout is set to 0 by default. This means that if a command that finds elements does not find anything, it won't wait.
The page load timeout is set to -1 by default. This means that Selenium will wait indefinitely for the page to load.
What Saifur found is not the same as the page load timeout. That's a timeout between the Selenium client and the Selenium server, which is not particularly well explained on the page Saifur found.
The script timeout is set to 0 by default. A comment in the source code explains:
The amount of time, in milliseconds, this session should wait for asynchronous scripts to finish executing. If set to 0, then the timeout will not fire until the next event loop after the script is executed. This will give scripts that employ a 0-based setTimeout to finish.
So even if it set to zero, an asynchronous script can still execute but it has to complete before Selenium's timeout gets a chance to run again.
This is from the code that Selenium uses for Firefox. The other browsers use different code bases but they are supposed to behave consistently, at least with regards to things that are proper to Selenium itself, like these timeouts. So the values and their interpretations should be the same for other browsers too.
For implicit wait always default wait it ZERO. , you can check it here :
Selenium Webdriver diff. waits
And if you set custom time then web driver will wait to get element till that time and if element does not found till that time then only web driver will throw exception.
The Selenium documentation is very much unclear on these timeouts.
According to
this
the default timeout of the implicit-wait is 0
According to
this
any page that does not load in 60s will return http communication
timeout unless you explicitly overwrite the timeout.
Unfortunately, I did not find any reference to provide on
ScriptTimeout. But, it's defaults to 0 according to my knowledge
and experience. Will update you with any reference later