Limiting the timeout period for Selenium FindElement() - c#

How can I limit/reduce the timeout period for FindElement? I am scraping a website. For a table which appears in thousands of pages, I can have either an element stating there is no information, or the table.
I search for one of theses elements and when missing, I search for the other. The problem is that when one of them does not exist, it takes a long time until the FindElement times out. Can this period be shortened? Can the timeout period be defined per element? All I found about waits are to prolong the timeout period...
I'm working in a .NET environment, if that helps.

The delay in FindElement is caused by the Implicit Wait settings. You can set it temporary to different value
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(0)); // setting to 0 will check one time only when using FindElement
// look for the elements
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(original settings));

Related

C#: use Timer in smart way

Now I understand it's two possible cases to use Timer:
do some action with specified interval, it's quite easy.
another usage (and more interesting for me): for instance we have array of times [11.24, 15.06, 17.47] (and it's possible to add more times there). We need to execute some operation at this moments. It looks like here I need timer with 1 minute interval to compare current hours and minutes with all items from array. And in case of TRUE execute action. But what if this operation takes 2 minutes for instance? In this case it's possible to miss some item from array. I think solution here is to separate logic for queueing (timer with 1 min interval to check time and add to queue) and logic for listening queue and execute action.
What do you think?
Option 3: Calculate how long (from now) until each item in the array - and set a timer for each interval. There is no "minute polling" so no chance of missing a time.
Option 4: Use one of the many pre-canned "Scheduler" libraries.

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
}

TcpClient.ReceiveTimeout is not working on small values?

I have tried the following cases and used a stopwatch to mesaure the actual time it takes for the socket to receive. Note that the timeouts are in miliseconds.
Dim NextClient As New TcpClient
NextClient.ReceiveTimeout = 1 //Case 1
NextClient.Client.ReceiveTimeout = 1 //Case 2
Dim ns As Net.Sockets.NetworkStream = client.GetStream()
ns.ReadTimeout = 1 //Case 3
Dim sw As New Stopwatch
sw.Start()
ns.Read(gbytes, 0, 5997)
sw.Stop()
BTW, ns.CanTimeout returns true. Also, I am not really expecting a 1 ms precision and it is only for testing purposes. I have actually started with 500 ms but first wanted to test with this.
In all cases and their combinations, even though the I have measured 100+ miliseconds each time with the stopwatch, I receive the data with no exceptions. If I intentionally delay the response over few seconds, however, I can get the exception. But even my ping / 2 to the server is much more than 1 ms.
Oddly, if I set the timeout to 1000 ms and the server response takes about 1020 ms the exception is triggered.
So, is there a minimum value or something else?
MSDN:
The ReceiveTimeout property determines the amount of time that the Read method will block until it is able to receive data. This time is measured in milliseconds. If the time-out expires before Read successfully completes, TcpClient throws a IOException. There is no time-out by default.
... if I set the timeout to 1000 ms and the server response takes about 1020 ms the exception is triggered. Well, if the response takes longer than the configured timeout, you'll get the exception.
If the time-out period is exceeded, the Receive method will throw a SocketException.
... is there a minimum value or something else?
Applicable values vary from -1 over 0 to MAX_INT_32.
The time-out value, in milliseconds. The default value is 0, which indicates an infinite time-out period. Specifying -1 also indicates an infinite time-out period.
However, the underlying Hardware and/or OS determines the resultion/granularity of the timeout (e.g. Windows has typically a default timer resolution of 15.625 ms. Consequently, exceptions are only raised at multiples of 15.625 ms).
The definition from Microsoft appears to be here:
https://msdn.microsoft.com/en-us/library/bk6w7hs8%28v=vs.110%29.aspx
and the remark against it reads:
"If the read operation does not complete within the time specified by this property, the read operation throws an IOException."
So the issue is somewhat complicated because, as usual, it relies on the wording being written accurately and distinctly.
The definition makes no reference to any kind of timer quanta or limitations of any timer, therefore if you specify a timeout of 1000mS and a read is not received within 1000mS then an exception must be thrown otherwise the mechanism is not compliant with its definition.
To make it correct any definition would have to specify some value of timer accuracy otherwise there is no workable definition of 'exceeds'. Just because the timeout is specified as an integer doesn't mean the interval is exceeded after 1001mS - you could justifiably say it's exceeded after 1000.0000000000000001ms.
There is also no definition of 'complete' when it states "If the read operation does not complete...".
Does it mean if it hasn't read ALL the bytes you asked for, or, if you ask for a partial read, does it mean if it hasn't read anything?
It's just not worded very well, and I suspect that Microsoft would say that the definition is wrong rather that the function being wrong, which doesn't help you much because they could change the way the function worked between releases in a way that might break your code without even commenting on it.
Provided it still does what the description says then it still works according to spec, and if you use features that are 'undocumented' - in other words things you observe that it does rather than things that the specification says it does - then that's just tough.
Unfortunately this simply doesn't do what the spec says in the first place, so all you can do is to use and exploit its 'observed behaviour' and hope that it stays the same.

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.

How to count the data sent or received by my PC (all processes/programs)?

I need to count the amount (in B/kB/MB/whatever) of data sent and received by my PC, by every running program/process.
Let's say I click "Start counting" and I get the sum of everything sent/received by my browser, FTP client, system actualizations etc. etc. from that moment till I choose "Stop".
To make it simpler, I want to count data transferred via TCP only - if it matters.
For now, I got the combo list of NICs in the PC (based on the comment in the link below).
I tried to change the code given here but I failed, getting strange out-of-nowhere values in dataSent/dataReceived.
I also read the answer at the question 442409 but as I can see it is about the data sent/received by the same program, which doesn't fit my requirements.
Perfmon should have counters for this type of thing that you want to do, so look there first.
Alright, I think I've found the solution, but maybe someone will suggest something better...
I made the timer (tested it with 10ms interval), which gets the "Bytes Received/sec" PerformanceCounter value and adds it to a global "temporary" variable and also increments the sum counter (if there is any lag). Then I made second timer with 1s interval, which gets the sum of values (from temporary sum), divides it by the counter and adds to the overall amount (also global). Then it resets the temporary sum and the counter.
I'm just not sure if it is right method, because I don't know, how the variables of "Bytes Received/sec" PerformanceCounter are varying during the one second. Maybe I should make some kind of histograph and get the average value?
For now, downloading 8.6MB file gave me 9.2MB overall amount - is it possible the other processes would generate that amount of net activity in less than 20 seconds?

Categories

Resources