Mocking Ajax Requests Executed During OnDocumentReady

281 views
Skip to first unread message

Aeden Jameson

unread,
Jun 19, 2012, 2:33:20 PM6/19/12
to seleniu...@googlegroups.com
I would like to mock ajax requests via selenium webdriver. My current technique is to override $.ajax like so,

            var Js = (IJavaScriptExecutor)Driver;
            Js.ExecuteScript(@"
                $.ajax = function(params){
                 if(params.url === "/person/1"){
                  params.success({ ... }); 
                }        
                }");

This approach works for all my needs so far except those calls that are made in $(function(){....}) . Which tells me I'm not redefining $.ajax soon enough. How do I get the script I'm using to override $.ajax to run before the document ready event?
Thank You.

Aeden

Peter Gale

unread,
Jun 19, 2012, 2:57:09 PM6/19/12
to seleniu...@googlegroups.com
Where are you defining this bit of code? I presume its in your java program handling the test execution rather than in the source code of the web page itself.

I've not done much javascript at all, but I presume you'd have to define or trigger the override in the source of the web page itself, where you could quite possibly place it before the document ready event. But I'd assume that Selenium only takes control of the browser after the document ready event, so this seems impossible to do, unless there's some nifty way of triggering your java code from your javascript code, but then you'd have to update the source web page somehow to do that, so you might as well just update the source page with your mocked request itself in raw javascript.


Date: Tue, 19 Jun 2012 11:33:20 -0700
From: aeden....@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Mocking Ajax Requests Executed During OnDocumentReady
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/h5qRSRaGR-4J.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en-US.

Aeden Jameson

unread,
Jun 19, 2012, 5:47:33 PM6/19/12
to seleniu...@googlegroups.com



Aeden


Where are you defining this bit of code? I presume its in your java program handling the test execution rather than in the source code of the web page itself.

The code is in a test setup method. Just to clarify I'm using NUnit and C#, but I imagine all the same ideas apply.  

 
I've not done much javascript at all, but I presume you'd have to define or trigger the override in the source of the web page itself, where you could quite possibly place it before the document ready event. But I'd assume that Selenium only takes control of the browser after the document ready event, so this seems impossible to do,

I think you're right Peter, I need to define the call after the inclusion of jquery.js and before the document ready event. Any ideas on how to approach this? 
 
unless there's some nifty way of triggering your java code from your javascript code, but then you'd have to update the source web page somehow to do that, so you might as well just update the source page with your mocked request itself in raw javascript.


That's my last resort.

Peter Gale

unread,
Jun 19, 2012, 5:54:03 PM6/19/12
to seleniu...@googlegroups.com
> ... I need to define the call after the inclusion of jquery.js and before the document ready event. Any ideas on how to approach this?
I just don't think this is possible from your C# code. My understanding is that you can only do it from javascript, and you won't be able to call your C# code from there.

Date: Tue, 19 Jun 2012 14:47:33 -0700
From: aeden....@gmail.com
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Mocking Ajax Requests Executed During OnDocumentReady
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/rszov9-FphYJ.
Reply all
Reply to author
Forward
0 new messages