0

I am using selenium's RemoteWebDriver and c#. Right now I am injecting a javascript function into the head of a page that looks like this

"window.__webdriver_javascript_errors = [];
                window.onerror = function(errorMsg, url, line) {
                window.__webdriver_javascript_errors.push(
                    errorMsg + ' (found at ' + url + ', line ' + line + ')');
                };";

It is injected by fiddler proxy and it stores javascript errors on the page into an array. I can then use IJavascriptExecuter to extract this method.

However, instead of this, I want to have the javascript automatically call a c# method whenever an error is found so I can log it right away. Is there a way to do this?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
user2932876
  • 332
  • 5
  • 15
  • possible duplicate of [Can you call C# function from javascript?](http://stackoverflow.com/questions/3994150/can-you-call-c-sharp-function-from-javascript) – Arran Nov 11 '13 at 14:55
  • I don't know if Selenium has anything which abstracts this, but I imagine you'd need to make an AJAX request to a server-side resource (ashx handler, controller action, etc.) and pass it the error data. That server-side handler can call whatever server-side method(s) it needs to. – David Nov 11 '13 at 14:55
  • The calling c# function from javascript is for asp.net, so I think the c# function that is called there is from serverside.Correct me if I am wrong but I think its a different matter – user2932876 Nov 11 '13 at 15:04
  • @user2932876, take a look at the answers and find out. It's all down to the same idea. The answers in that question have many links for you to start researching the subject. The fact it's ASP.NET doesn't have any bearing. The point is the JS must call something, usually something like a web service, which is then it's "route" into your code. (As an aside, I'd say your current implementation is the better one, it makes more logical sense, I don't see the need to do this) – Arran Nov 11 '13 at 15:07
  • Yeah I took a look at them before I posted this question. The thing is that in asp.net it is clear which method in c# will be called, since it is easily found in the serverside of the code. In selenium though, I don't see how they can find where the c# method is, since the method can be in any file. My implementation is ok, but I need to use IJavascriptExecuter every time I want to find errors, and it is unclear when an error is found. – user2932876 Nov 11 '13 at 15:11

1 Answers1

0

Since WebDriver runs outside and independently of the browser process, the injected JavaScript is going to know nothing about the language of the automating process. They are fundamentally disconnected, and this is entirely by design. You can either implement the logging function on the server side of the application you're automating, pushing the errors into a log; or you can poll for errors, pulling them from the page.

There may be some ways that you can make this a little easier and transparent to your code. You might consider using the EventFiringWebDriver implementation found in the WebDriver.Support.dll assembly. It wraps many of the WebDriver API calls, firing events before and/or after a method call has been invoked. You could place your JavaScript log check in the event handlers of those events, and it would be transparent to your test code. If the current implementation doesn't meet your needs, you could use the implementation as a template for an implementation that would work for you.

JimEvans
  • 27,201
  • 7
  • 83
  • 108
  • Thanks for the post. The implementation is interesting, I will see if I can use it to enhance my code. I have found a workaround for this issue though, although it is not too elegant. Whenever an error is found, an alert pops up, and when that happens an exception is thrown in my code. When handling the exception, I can log the javascript error. – user2932876 Nov 11 '13 at 18:38