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));
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
}
Please bear with me as I am relatively new to Appium. I am writing C# tests in Appium for my Android app. I am stuck finding answers to questions below.
1) How to check if a particular element exists? Is there any boolean property or function returning true or false? The methods driver.GetElementById, driver.GetElementByName etc. throw exceptions if element doesn't exists.
2) Suppose I want to write a test for login. The user enters username and password and hits login button. The requests goes to server and it checks whether username-password pair exists in database. Meanwhile the loading indicator (progress dialog in Android) is shown on screen. How shall make a test suspend it's execution until response comes from server assuming I don't want to use something like Thread.Sleep function?
3) Can I check whether textfield validation is failed on screen? A control with black background and white text is shown below textfield upon validation failure if we set validation for that textfield through setError function. Is there any way to check that validation has failed?
Anticipating answers. Thanks.
For the first 2 questions (This is what I do in java, definitely can be implemented in c#) -
1) Use the polling technique - In a loop check for the element return of the following
#param - By by , int time
driver.findElement(By by);
This must not be null or empty.
If within the threshhold time the element is not present then fail the test.
In appium mode - isVisible() will be same as the above, as an element not visible will not be present.
2) Check for the next activity to be awaited. Use the same polling technique to keep on comparing the current activity with the awaited activity, If the awaited activity does not start within the threshold time then fail the test.
#param int time, String awaitedActivity
1) Get the current activity.
2) Compare with the awaited activity.
3) If same then break the loop.
4) Else sleep for a second and then continue till the time is exhausted.
TPL Dataflow block has .InputCount and .OutputCount properties. But it can perform execution over item right now, and there is no property like .Busy [Boolean]. So is there a way to know if block is now operating and one of item still there?
UPDATE:
Let me explain my issue. Here on pic is my current Dataflow network scheme.
BufferBlock holds URLs to load, number of TransformBlocks load pages through proxy servers and ActionBlock at the end performs work with loaded pages. TransformBlocks has predefined .BoundedCapacity, so BufferBlock waits for any of TransformBlocks becomes free and then post item into it.
Initially I post all URLs to Buffer Block. Also if one of TransformBlocks throw exception during loading HTML, it returns it's URL back to BufferBlock. So my goal is somehow wait until all of my URLs was guarantee loaded and parsed. For now I'm waiting like this:
Do While _BufferBlock.Count > 0 Or _
GetLoadBlocksTotalInputOutputCount(_TransformBlocks) > 0 Or _
_ActionBlock.InputCount > 0
Await Task.Delay(1000)
Loop
Then I call TransformBlock.Complete on all of them. But in this case, there still can be last URLs loading it TransformBlocks. If last URL was not successfully loaded, it becomes 'lost', because none of TransformBlocks wouldn't take it back. That's why I want to know if TransformBlocks are still operating. Sorry my bad English.
Even if you could find out whether a block is processing an item, it wouldn't really help you achieve your goal. That's because you would need to check the state of all the blocks at exactly the same moment, and there is no way to do that.
What I think you need is to somehow manually track how many items have been fully processed and compare that with the total number of items to process.
You should know the number of items to process from the start (it's you who sends them to the buffer block). To track the number of items that have been fully processed, you can add a counter to your parsing action block (don't forget to make the counter thread-safe, since your action block is parallel).
Then, if the counter reaches the total number of items to process, you know that all work is done.
I am creating a live editor in Windows 8.1 App using JavaScript. Almost done with that, but the problem is whenever I run such bad loops or functions then it automatically hangs or exits.
I test it with a loop such as:( It just a example-user may write its loop in its own way..)
for(i=0;i<=50000;i++)
{
for(j=0;j<5000;j++){
$('body').append('hey I am a bug<br>');
}
}
I know that this is a worst condition for any app or browser to handle that kind of loop. So here I want that if user uses such a loop then how I handle it, to produce their output?
Or if its not possible to protect my app for that kind of loop, if it is dangerous to my app so I alert the user that:
Running this snippet may crash the app!
I have an idea to check the code by using regular expressions if code have something like for(i=0;i<=5000;i++) then the above alert will show, how to do a Regex for that?
Also able to include C# as back-end .
Unfortunately, without doing some deep and complex code analysis of the edited code, you'll not be able to fully prevent errant JavaScript that kills your application. You could use, for example, a library that builds an abstract syntax tree from JavaScript and not allow code execution if certain patterns are found. But, the number of patterns that could cause an infinite loop are large, so it would not be simple to find, and it's likely to not be robust enough.
In the for example, you could modify the code to be like this:
for(i=0;!timeout() && i<=50000;i++)
{
for(j=0;!timeout() && j<5000;j++){
$('body').append('hey I am a bug<br>');
}
}
I've "injected" a call to a function you'd write called timeout. In there, it would need to be able to detect whether the loop should be aborted because the script has been running too long.
But, that could have been written with a do-while, so that type of loop would need to be handled.
The example of using jQuery for example in a tight loop, and modifying the DOM means that solutions that trying to isolate the JavaScript into a Web Worker would be complex, as it's not allowed to manipulate the DOM directly. It can only send/receive "string" messages.
If you had used the XAML/C# WebView to host (and build) the JavaScript editor, you could have considered using an event that is raised called WebView.LongRunningScriptDetected. It is raised when a long running script is detected, providing the host the ability to kill the script before the entire application becomes unresponsive and is killed.
Unfortunately, this same event is not available in the x-ms-webview control which is available in a WinJS project.
I've got 2 solutions:
1.
My first solution would be defining a variable
startSeconds=new Date().getSeconds();.
Then, using regex, I'm inserting this piece of code inside the nested loop.
;if(startSecond < new Date().getSeconds())break;
So, what it does is each time the loop runs, it does two things:
Checks if startSecond is less than current seconds new Date().getSeconds();.
For example, startSecond may be 22. new Date().getSeconds() may return 24.Now, the if condition succeeds so it breaks the loop.
Mostly, a non dangerous loop should run for about 2 to 3 seconds
Small loops like for(var i=0;i<30;i++){} will run fully, but big loops will run for 3 to 4 seconds, which is perfectly ok.
My solution uses your own example of 50000*5000, but it doesn't crash!
Live demo:http://jsfiddle.net/nHqUj/4
2.
My second solution would be defining two variables start, max.
Max should be the maximum number of loops that you are willing to run. Example 1000.
Then, using regex, I'm inserting this piece of code inside the nested loop.
;start+=1;if(start>max)break;
So, what it does is each time the loop runs, it does two things:
Increments the value of start by 1.
Checks whether start is greater than the max. If yes, it breaks the loop.
This solution also uses your own example of 50000*5000, but it doesn't crash!
Updated demo:http://jsfiddle.net/nHqUj/3
Regex I'm using:(?:(for|while|do)\s*\([^\{\}]*\))\s*\{([^\{\}]+)\}
One idea, but not sure what is your editor is capable of..
If some how you can understand that this loop may cause problem(like if a loop is more than 200 times then its a issue) and for a loop like that from user if you can change the code to below to provide the output then it will not hang. But frankly not sure if it will work for you.
var j = 0;
var inter = setInterval( function(){
if( j<5000 ){
$('#test').append('hey I am a bug<br>');
++j;
} else {
clearInterval(inter);
}
}, 100 );
Perhaps inject timers around for loops and check time at the first line. Do this for every loop.
Regex: /for\([^{]*\)[\s]*{/
Example:
/for\([^{]*\)[\s]*{/.test("for(var i=0; i<length; i++){");
> true
Now, if you use replace and wrap the for in a grouping you can get the result you want.
var code = "for(var i=0; i<length; i++){",
testRegex = /(?:for\([^{]*\)[\s]*{)/g,
matchReplace = "var timeStarted = new Date().getTime();" +
"$1" +
"if (new Date().getTime() - timeStarted > maxPossibleTime) {" +
"return; // do something here" +
"}";
code.replace(textRegex, matchReplace);
You cannot find what user is trying to do with a simple regex. Lets say, the user writes his code like...
for(i=0;i<=5;i++)
{
for(j=0;j<=5;j++){
if(j>=3){
i = i * 5000;
j = j * 5000;
}
$('body').append('hey I am a bug<br>');
}
}
Then with a simple regex you cannot avoid this. Because the value of i is increased after a time period. So the best way to solve the problem is to have a benchmark. Say, your app hangs after continuos processing of 3 minutes(Assume, until your app hits 3 minutes of processing time, its running fine). Then, whatever the code the user tries to run, you just start a timer before the process and if the process takes more than 2.5 minutes, then you just kill that process in your app and raise a popup to the user saying 'Running this snippet may crash the app!'... By doing this way you dont even need a regex or to verify users code if it is bad...
Try this... Might help... Cheers!!!
Let's assume you are doing this in the window context and not in a worker. Put a function called rocketChair in every single inner loop. This function is simple. It increments a global counter and checks the value against a global ceiling. When the ceiling is reached rocketChair summarily throws "eject from perilous code". At this time you can also save to a global state variable any state you wish to preserve.
Wrap your entire app in a single try catch block and when rocket chair ejects you can save the day like the hero you are.