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

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.

Related

Selenium Webdriver test removes input on button click

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.

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

C#, WebBrowser, how to inject and execute JavaScript before any other script on the page?

I know how to inject my own scripts into web pages on load event, but it doesn't always work as expected. Sometimes the page succeeds to run its own scripts before mine and it makes my whole app fail. I try do disable the possibility the page spawns another browser window. I have an app running on my server, when it starts Internet Explorer instances randomly - it soon crashes the whole machine which is a disaster.
I made a script which changes window.open method and it does the trick perfectly - except the case when the page pops up another window BEFORE my code is executed. Is there a way to freeze JS before I finish injecting my script? Maybe is there a way to inject my script in an earlier point, before DocumentCompleted event? But how?
This doesn't directly answer your question about how to do it in the browser - but I'm aware of a similar thing that is done by Avast internet security and it does it using an HTTP proxy.
The idea is to intercept HTML pages as they are received over the wire, and inject your script into the HTML itself; thus there's no way that any other script can execute before it.
Whilst that might sound scary - you can do it using the technology underpinning the very excellent Fiddler HTTP debugger - the FiddlerCore API

Errors in Razor pages cause server to hang

I've been writing an app using the awesome new Razor view engine and for the most part, things have been great.
One issue I keep running into, however, is that if I should happen to write invalid code, such as referencing a null property or even a non existent property, rather than throwing an error, something happens in the background that causes the browser to wait and wait and wait and if I do not cancel the browser's request quick enough, IIS will simply hang.
It seems as if it enters some sort of loop. CPU usage goes up (though not terribly high) and restarting IIS via either GUI or iisreset command seems to take abnormally long (presumably while it waits for the process to safely shutdown).
This also happens for other invalid code scenarios such as failing to close a code block with a closing brace.
I notice this behavior on more than one machine.
Any ideas if this is a known issue or have I mis configured something?
Thanks!
I found the problem. I had installed a package via NuGet called "Razor Debugger" and apparently this plugin was intercepting Razor errors somehow, and was consequently not showing them.
Removing that package now allows Razor to work properly. What a relief.
Steer clear of this library.

reload the sourcecode of a C# app on command

i have a C# application, and id like to be able to make a system, so that within the program, you can display the sourcecode of the application. easy enough so far right?
well, i need them to be able to edit that code, almost like the debug break option... then load the new code and continue without stopping the program. so my problem is, that when this app loads its several thousand lines of code, and it takes a good block of time. after its loaded, it needs to do several hundred operations before allowing user input the first time it loads. also, it has a tcp client in it, and it is very important that it does not get disconnected.
i want people to be able to edit the source, click a button, and wait a few seconds, and have the new code inserted and "rehashed" so to speak, without having to break the overall function of the application.
im looking thorough code examples where possible and an input weather this is possible or not
~ thanks
If you want to allow people to make arbitrary changes to your program, that would be very complex. However, if you want to let them change specific behavior (like rewriting a calculation algorithm) you could have a look at Microsoft.CSharp.CSharpCodeProvide as discussed here.
I don't think you can do that (change a .net app without rebuilding it) but you can have dynamic code loaded and run at any time..
Some people use plugins with Boo, people can change the plugins and these can be loaded at any time by the main app.
But I would suggest you have a look at the Ruby usage inside SilverLight..
This is something completely different, but its something I'm reading on how to start playing with Dynamic code handling: here

Categories

Resources