Make selenium No wait for element - c#

I am using selenium for a while and have some question about the IWebElement Wait procedure.
We have the Explicit Wait and Implicit I use them and understand the meaning but..
How can I make selenium no wait for element at all?
I tried not to use wait functions but still when I call FindElement or GoToUrl its not always return immediately sometimes still wait for 0 to 60 sec
I noticed that in most of time the wait in FindElement didn't return the element and wait for no reason.
for example: call for element id can take 3 sec and not immediately (lot off calls lot of time...)
maybe I am doing something wrong.
the main purpose is to take full control of the program and handle the wait time myself (for better efficiency)
maybe there are better articles to understand selenium architecture of finding elements ? (not the selenium API)
(I am using latest version of selenium 2.48.0)
Code example:
driver = new FirefoxDriver();
js = driver as IJavaScriptExecutor;
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(0));
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl("someUrl");
IList<IWebElement> loginFrame = driver.FindElements(By.TagName("iframe"));
driver.SwitchTo().DefaultContent().SwitchTo().Frame(loginFrame[0]);
driver.FindElement(By.Id("userID")).SendKeys("username");
driver.FindElement(By.Id("userPassword")).SendKeys("userPassword");
driver.FindElement(By.Id("login")).Click();
driver.SwitchTo().DefaultContent();
driver.FindElement(By.XPath("//div[#class='something']/ul/li[2]/a")).Click();
driver.FindElement(By.PartialLinkText("someText")).Click(); // *
At Last call its throw exception after something like 3 ~ 5 seconds and not immediately (When I set Implicit to 60 sec it's find the element!)

I am not sure what you mean by "how can I make selenium no wait for element at all? "
If you mean that you dont want selenium to wait at all for an element at all. I think this might work -
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
WebElement textbox = driver.findElement(By.id("textbox"));
Use this and write your own method which takes a locator and no of seconds to wait and then set the implicit wait inside the method depending on what has been passed to the method.
public WebElement locateElementById(int timeInSec, String id){
driver.manage().timeouts().implicitlyWait(timeInSec, TimeUnit.SECONDS);
WebElement element = driver.findElement(By.id(id));
return Element
}
Something like this. And then you can call it with the seconds you want.
when you want 0 seconds pass 0 when you want 10 seconds pass 10.

Related

Thread.sleep() being ignored by selenium C#

I have a selenium test that works great while debugging and stepping slowly through. I added a Thread.Sleep() in order to stop selenium while a file is downloaded. However selenium in completely ignoring the Thread.Sleep(). Any thoughts?
IWebElement iframe = driver.FindElement(By.Id("genFrame"));
CurrentFrame = driver.SwtichTo();
CurrentFrame.Frame(iframe);
driver.FindElement(By.Id("date").Sendkeys("05/11/2021" + Keys.Enter));
driver.FindElement(By.Id("export-btn"));
System.Thread.Sleep(5000);
In all possibilities System.Thread.Sleep() is never ignored unless some previous line of code raises an exception.
As per your code the unit is milliseconds, so effectively 5000 milliseconds, i.e. 5 seconds.
Solution
You may like to increase the waiting time to greater value, e.g. 10 seconds or more:
using System;
using System.Threading;
System.Thread.Sleep(10000);
Update
As you can visually see selenium add the date field, possibly the line in the mid:
driver.FindElement(By.Id("export-btn"));
raises an exception.

Selenium explicit wait upper limit

I'm using Selenium to do some automated testing in a C# app. I have a component that's not disappearing after the 90 seconds explicit wait. Is there an upper limit to the timeout:
Driver.WaitUntil(ExpectedConditions.InvisibilityOfElementLocated(By.CssSelector("section > h1 > img")), 90);
I haven't found anything indicating there's an upper limit.
Cannot say about explicit wait upper limit, but if you want to make it more you can put the wait in loop. So the loop will make it 90*number of loops.
WebDriverWait constructor is WebDriverWait(IWebDriver, TimeSpan) TimeSpan has several properties, one of them is Days, witch receives double as parameter. So theoretically you can do
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromDays(double.MaxValue));
And see what happens first, your explicit wait ends or the sun goes supernova :)
(The script will probably crash much sooner).

Selenium C# Element Not Found Taking a Long Time

I am using Selenium for C# to test a page.
Is there a way to quickly fail if the element is not found on the page?
I am experiencing if the HTML element is not found on the page the Selenium test a very long time and then eventually it fails. Recommendations on quickly failing if the element not found is appreciated!
return WebDriver.FindElement(By.Id(myTextBoxId)
You can try to change the Timeout wait time at the beginning of your test.
// In C# you can use
ChromeDriver driver = new ChromeDriver("Path to Driver");
driver.Manage().Timeouts().ImplicitlyWait(new Timespan(0,0,2));
This should now wait for 2 seconds for an element to appear before failing. You can set this value to anything you want.
The search for element should fail right away if you do not use any implicit wait or explicit wait. If you are doing that please remove them. And, if you are mixing implicit and explicit waits then that's even going to make it slower. On the other hand, if you expect the element not to exist and want to quickly check if the element exists or not and proceed, use findElements() and size() on the list. Something like the following:
List<WebElement> elements = driver.findElements(By.xpath("something"));
if(elements.size()>0){
//element exist
}else{
//does not exist
}

How to apply Dynamic wait in Ranorex?

I wants to apply dynamic wait in ranorex.
To open a webpage I used static wait like this :-
Host.Local.OpenBrowser("http://www.ranorex.com/Documentation/Ranorex/html/M_Ranorex_WebDocument_Navigate_2.htm",
"firefox.exe");
Delay.Seconds(15);
Please provide me a proper solution in details. Waiting for your humble reply.
The easiest way is use the wait for document loaded method. This allows you to set a timeout that is the maximum to wait, but will continue when the element completes it's load. Here is the documentation on it,
http://www.ranorex.com/Documentation/Ranorex/html/M_Ranorex_WebDocument_WaitForDocumentLoaded_1.htm
First of all, you should be more detailed about your issues. Atm you actually don't state any issue and don't even specify the reason for the timeout.
I don't actually see why you would need a timeout there. The next element to be interacted with in your tests will have it's own search timeouts. In my experience I haven't had a need or a reason to have a delay for the browser opening.
If you truelly need a dynamic delay there, here's what you actually should validate.
1) Either select an element that always exists on the webpage when you open the browser or
2) Select the next element to be interacted with and build the delay ontop of either of these 2
Let's say that we have a Input field that we need to add text to after the page has opened. The best idea would be do wait for that element to exists and then continue with the test case.
So, we wait for the element to exist (add the element to the repository):
repo.DomPart.InputElementInfo.WaitForExists(30000);
And then we can continue with the test functionality:
repo.DomPart.InputElement.InnerText = "Test";
What waitForExists does is it waits for 30 seconds (30000 ms) for the element to exists. It it possible to catch an exception from this and add error handleing if the element is not found.
The dynamic functionality has to be added by you. In ranorex at one point you will always run into a timeout. It might be a specified delay, it might be the timeout for a repo element, etc. The "dynamic" functionality is mostly yours to do.
If this is not the answer you were looking for, please speicify the reason for the delay and i'll try to answer your specific issue more accurately.

Measure performance using SeleniumWebDriver vs native javascript

Let's assume that some action is performed when I click the button on a web-page. Let's say it takes X seconds and during these seconds a div is displayed in the center of the page. When action processing is finished the div is removed and focus goes to element E.
1) I've written a test in Selenium ( C# ):
stopwatch.Restart();
button.Click();
while (driver.FindElementsById(PopupDivId).Count != 0)
{
Thread.Sleep(10);
}
stopwatch.Stop();
2 ) And a test in javascript (inside my page). Simplified code:
OnClick button handler:
console.time('test');
GotFocus handler on textbox (element E):
console.log(document.getElementById('My_PopupDivId')); // just to be sure - it returns null
console.timeEnd('test');
For some reason Selenium measurements are ~2x bigger than direct javascript measurements.
(400ms vs 800ms).
Is this something wrong with my code or it's just Selenium inaccuracy ( does it actually make sense to measure such things as javascript/DOM performance using Selenium?)
John Koerner's comment is right on the money. You are comparing apples and oranges. Here are some factors affecting the difference:
Selenium uses a wire protocol to talk to your browser. It has to take your parameters, marshal them, send them to the browser, wait for a response, unmarshal the return value and give it to your code. That's in addition to whatever DOM operations are performed.
Your Selenium timing include the click operation as part of what is timed, whereas you start your JavaScript timing inside the event handler for the click. Selenium does some housekeeping work when you ask it to click on a button, like for instance check whether it is visible and bring it in to view if it is not visible. Your Selenium timing includes the cost of this, but your JavaScript timing does not.
Your Selenium code is subject to whatever implicit wait value is set. I don't typically use implicit wait but I've just tested it and found that if I ask Selenium for a list of elements, it will wait until the implicit wait has elapsed before telling me there are none.
To get a better idea of how much Selenium is affecting your results, you should try your test with different delays between the appearance of the div and its removal. I really doubt that a case where your JavaScript timing would yield 2 seconds would translate to a 4 second timing in Selenium.
As for using Selenium for performance measurements, I'd say Selenium is a very blunt tool for such task. When I'm worried about code performance in a browser, I skip Selenium and use JavaScript directly. I have used Selenium to get an idea about performance, in the aggregate. For instance, there's been times I reworked some core part of an application and then ran a test suite that uses Selenium and found the time to run the whole test suite significantly improved. However, as I said, this is blunt. We're talking seconds of difference, not milliseconds.

Categories

Resources