Selenium Webdriver test removes input on button click - c#

I am running into an issue with my selenium webdriver test where, if I input something into a phone number field and then click the call button, it will clear out the input and do nothing. However, if I run it in debug with a break point on one of my waits, it will run fine. The code is for a softphone application and I am just trying to input the phone number and then click the call button. I have narrowed the issue down to starting with line 302, but have no clue what it could be.
Things I've tried:
I have tried using waits, thread.sleeps, milliseconds, seconds, etc.
I have tried moving the if statement out into an actions class where it is called from the test like my other actions are. I have tried getting rid of the if statement and just using waits and the code seems to just skip over them.
This is C# code. All of the PerformAction are referencing action classes that check to ensure that the element is visible/clickable. The action classes reference the elements. I have set my implicit wait to 0 so it should only use the explicit.
So my question is, how do I get my code to stop skipping the waits and/or stop clearing out the input field?
test code screenshot

What ended up being the issue is that the elements were being checked for visibility/clickability before they were gotten to in the test. This made the functionality weird and skippy. Adding a sleep before checking the elements in my action methods seems to have resolved the issue.

Related

The web page could not be accessed Coded UI Error

I am running a Coded UI test that clicks a hyperlink, for example, "Home" then goes off and does a few different things on the web page, then tries to click the exact same "Home" link and an error is thrown saying
The web page could not be accessed. If the page is refreshing, please
wait until the page refreshes and then perform actions on it.
Now I get this when I am stepping through the test, so I know at the point this link is trying to be clicked the page is not refreshing. Digging through the UITest error logs I see this line every time before the browser refresh error is thrown:
E, 6468, 13, 2013/10/18, 15:08:47.406, 271797587803, QTAgent32_40.exe,
IEDOM : EnsureValid failed for html node: A, 52
I have compared the uitest mappings for this link and the properties are all identical, also the page html is identical.
I am running in IE10, KB2879017. VS2012 4 RC.
Any idea's what's causing this to happen?
NOTE: WaitForControlReady() does nothing to stop this from happening. Also a hard coded Playback.Wait() doesn't help here either.
Failing line of code, this works the first time, but not the second time it is executed in a test.
this.UIWindow.UIDocument.UIHomeHyperlink.WaitForControlReady();
Mouse.Click(this.UIWindow.UIDocument.UIHomeHyperlink);
Remember that the search properties being used for the UIHomeHyperlink mapped control are the exact same the second time these lines are executed. Mouse.Click() execution is when the "Web page not available" error is thrown in VS2012.
After some brainstorming and trial and error testing, came to the conclusion that mapped controls are holding onto properties after being found on a page. For some reason those properties weren't matching up exactly with the page properties on the second time around.
So a "solution" to this problem was to force a new up of the entire mapped UI___Window each time we enter the page. Coded UI prevents this in the designer file by checking if the control is null every time, if it is then the control is new'ed up. If not, then Coded UI uses the same control and it's properties from when it was first defined/discovered. Hope this helps anyone else that was having problems with this error.
There also seems to be a bug in CodedUI when calling .GetParent() twice on the same object tree, which causes this exception to be thrown. We worked around it by determining the parent in another way.
On another workstation (W7) another exception was thrown when calling .GetParent() twice, something about that the IE version should be more than IE10 on 64bit versions of Windows, which was clearly nonsense, since the computer had IE11 installed.

Handle infinite loop in third party javascript file (c# .net)

I need to load a third party script file(which I do not have any control), but I want to avoid hanging the page in case the script has an infinite loop in it.
I tried calling the javascript file and put it in an updatepanel only on a click of a button but the page still hangs. I even tried putting it in an iframe just to separate it from other controls and the same scenario happens.
Is there really NO way to stop an infinite loop once it has started executing?
You are trying to add handling to a bug that is already fixed, so all that is going to happen is your code is going to become more complex unnecessarily.
I would have faith in the fix that they have produced, and you could look into adding Unit Tests that will allow you to test the bug in a secure manner
Edit
If you think the bug still exists then you could create a small test example (i.e your unit test) to send to the third party in hope of a better fix
Use debugger; to debug the script file. Put debugger; symbol beginning of the script's method which will be executed and run in chrome (since it is easy to debug) . click F12 to switch on the debugger tool. The script execution will stop on the line you put the debugger; and debug it line by line.
If the script is executing an infinite loop the chrome tab will crash with a message "Aw, Snap!". You can refer the call stack to find the method causing problem.
One possible solution could be is using Web Worker and run your JS code inside it. After a while, if there is no any responce from it, call worker.terminate() to "kill" it.
Pay attention that web workers has strong limitations: for example you can not access UI element from within the code that runs in web worker scope, and it's not supported on old browsers.
I don't know if web worker is suitable in your case, but this one is option that may help.

Values updating on debug but not on runtime

I have a weird problem to explain here. I have a wizard with multiple screens. In my first screen I am reading an XML file and its been read and its values shown/used for screens. In one of the screen, I am changing these values and then hit Update button to update the data. But at the end of the wizard, when I click finish the changed values are not saved properly. I have confirmed this with code as well that the changed values were not present while exporting the data.
But the real kick came when I started debugging. Till now I was debugging only when the data is being exported. But then I started debugging from start of the wizard. I noticed while debugging that, when I hit Update button after changing values, this time the values were updated and correct data was exported. But when I run the application in runtime or put a debug point directly at export process, the changed data is not reflected.
So the problem is, when I debug the changed values are being exported and in runtime it do not. Can anyone suggest me something on this?
However I noticed later that sometimes in runtime its updating and sometimes not. But it gets always updated when I debug. If it matters, I am using Winforms and MVP.
EDIT
I must also admit that when I put Thread.Sleep(2000) at the end of Update button event method, everything looks good and works fine. But however this way is not being appreciated by my code reviewers and either this method is not pleasing me.
Edit Again
The Update button uses the main thread and updates the value in the same thread. However, when I click finish, its creating a BackgroundWorker and running a ExportData method from there. I cannot see any more threads there with my present observation and knowledge.

How to wait for the post back in Watin?

In my C# code, I am using Watin to navigate the web, to log in to a page, I need to click the log in button, but right after I want to log out, so I have the click log out button right after, but the log out part doesn't work. I even tried closing the browser (using the close method) after logging in, but it didn't work. It feels like as soon as the page gets changed (i.e. after logging in) no more commands from the c# will work.
Does anyone know whats wrong?
As mentioned in another answer Thread.Sleep(milliseconds) is a way to wait for a time period for something to load. Very, very easy to implement, but it is far from optimal due to varying load times, and if you make it long enough so that it will always wait long enough you'll end up with a lot of wasted time. On one test this is not a big deal, but for instance if you have to wait 5 seconds and you have 1000 tests.... etc etc etc.
The route I've gone is:
Put in Thread.Sleep()s to determine if it is a "wait" issue.
If the the code with the Sleep() is going to be used more than once figure out what is causing the need for the sleep().
Refactor out the Sleep() using various Wait...() methods. WaitTilExists, WaitForAttributeEqualsWhatever, WaitForAsyncToFinish <- Not real methods, but WatiN has a bunch built in
The big cause of waits for me now is JQuery asynchronous calls in ASP.NET and I made a static helper class that works well for me to wait for async calls to finish. These tend to be very specific to what framework(s) the sites you're testing are written in.
The watin click command wait until the browser is loaded so practically it wait for the postback.
In case if you using ClickNoWait() command it will not wait.
So if your code looks like this it should work:
browser.GoTo("www.your-site.com");
// fill user/pass
browser.Button(Find.ByClass("login-class")).Click();
browser.Button(Find.ByClass("logout-class")).Click();
In case it's still not working you can add this after login click browser.WaitForComplete();
In Watin you will encounter many situations where the code is non blocking (you'll execute a line of code and will immediately keep going) so for those cases you'll need to find a different way to know that the next page (action, etc.) is already there. For example, on a login page you could check if that pages has a TextBox called UserName:
<code>
TextField uName = browser.TextField(Find.ByName("userName"));
if(uName.Exists)
{
// Then do the login code....
}
</code>
In the same way you should control that the page after the login is there before you keep going executing your code. So for example, if you are logging in into a page that you know that will contain the text: "Your Account Details" you might do something like this:
<code>
browser.GoTo("http://www.yourdomain.com/login.aspx");
//do your login code
browser.WaitUntilContainsText("Your Account Details", 240); // the second parameter indicates the seconds it will wait before it times out.
// your code to deal with the page after the login.
</code>
Using Thread.Sleep is a recipe for confusion and that's a problem for sure, you will NEVER get the timing right with a web page (even if you think it will take 10 seconds it might never come back and at that point the server will be terminating the opened connection).
Hope it helps.
Use Thread.sleep in your scripts to sync with logout and login...
or
instead of logout you directly close application and use ie instance to relogin to application

Odd form behaviour. Text only appearing in textbox sometimes after setting Text property

In vb6 you could see a textbox update immediately when the value was changed, but I've noticed in .net that it will not update until after the method you are in has been exited. A preliminary question I have is whether there is a way to make the textbox update before the method is completed.
The issue is that I have two textboxes whose Text properties are set and when the method completes, only one of them consistently updates on screen. The other usually does not, but sometimes does. It's very sporadic. I literally called someone over to verify I wasn't taking crazy pills and of course it started working when she came over (though I made no code changes). Then when the QA guy came around it stopped working again (again, didn't make any code changes).
All I've been doing is setting break points and stepping through code trying to figure out what's keeping this text box from being updated. At the end of the procedure, right before it exits, I can check on the Text property and it has the correct value, but as soon as the method finishes it disappears.
I will try to get a code snippet up soon, but in the meantime I would love to know if anyone else has had this problem and any good ideas on how to debug b/c I'm getting a bit frustrated! There is a timer on the form that is enabled at a certain point and disables itself when it's run... setting a breakpoint in this timer verifies that it is not the culprit b/c the breakpoint never gets hit. But I am wondering if there could be some other asynchronous process I might be missing... I don't think so but if you can tell me anything I should be looking for that I might not have thought of please do.
Edit: I would post a code snippet, but the snippet I wanted to post doesn't reproduce the problem and I have not isolated the problem to a small enough section of code for it to be practical to post. I will add a little more info though:
After the method that updates these textboxes is completed, control returns to the form. There are no other processes going on. I kept thinking maybe some code was getting run somewhere that was blanking out the textbox but a thorough look through the code has confirmed this is definitely not the case... when the method completes nothing else happens.
I noticed when I was debugging that sometimes it would work properly, and on a rare occasion it would even work properly when running normally. I added a DoEvents() to see if it would work and it did the first time... but then did not continue to work. Out of frustration I added repeated calls to set the textbox and DoEvents() after each one and that did not make it perfect. Lastly I added a call for the thread to Sleep() for 300 milliseconds and it seems to be better now.
Keep in mind that there is another almost identical textbox on the form that gets set in the same procedure and never has any problem updating whatsoever. I have searched for any difference between the two controls and aside from size, location, and name I cannot find any difference.
I suppose you could add: Application.DoEvents() after you set the textbox text value. Its usually frowned upon to use DoEvents, though it may solve your issue in the intermediate.
i think you better use Invalidate(); this will cause control to redraw yourtextbox.Invalidate();
you can use Threading for this purpose

Categories

Resources