Passing data back to the application from javascript

180 views
Skip to first unread message

face20010

unread,
Sep 29, 2011, 12:43:43 PM9/29/11
to webdriver
I know I can execute javascript in the browser from my application
using - (IJavaScriptExecutor)driver).ExecuteScript(......) and that
that method can return data.

However I am looking for another way of returning data from the
browser as in my initial driver.ExecuteScript() call I am simply
overriding the click event for a button on the page. I then want to
return data to my application when that button gets clicked. So can
this be done?

Simon Stewart

unread,
Sep 30, 2011, 1:39:51 AM9/30/11
to webd...@googlegroups.com
Take a look at ExecuteScriptAsync

Simon

> --
> You received this message because you are subscribed to the Google Groups "webdriver" group.
> To post to this group, send email to webd...@googlegroups.com.
> To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.
>
>

face20010

unread,
Oct 1, 2011, 6:32:02 PM10/1/11
to webdriver
Im not really sure how that works, cant find any examples on the net.
Here are the exact steps of what I am tring to achieve -

1. User goes to site www.xyz.com
2. They click a link that makes a form appear on the page via ajax
3. They fill in some details on this form and click submit
4. Now once they click submit I want to return the html contents of
this form to my c# application, the innerHTML value.

So any ideas on if this can be done with Selenium? Can
ExecuteAsyncScript do it?

Daniel Wagner-Hall

unread,
Oct 1, 2011, 7:33:13 PM10/1/11
to webd...@googlegroups.com
I'm still not sure what you're trying to achieve here; why is it
important that the values are retrieved on submit, rather than
beforehand? I'm assuming when you say "User" you mean "WebDriver
acting as a user". Because you're having webdriver enter the details,
it must already know what the details will be. Or if not, *before*
you press submit, you could just get the contents of the form fields
by looking them up and executing WebElement.getAttribute("value")...

If you *really* need to values to be returned on-submit, the only way
I can really think of doing this is:

1) Go to site
2) Click on link to make form appear
3) Execute script which registers an onsubmit handler on the form,
along the lines of driver.executeScript("arguments[0].onsubmit =
function() { ... }", formElement)
4) Fill in form
5) Press submit
6) Onclick you injected stalls the page load
7) Execute script which polls the stalled page for the data, and then
executes some callback which continues the submit action

Where the function in 3) looks something like

* Initialise global variable somewhere to contents you want
* return false, so submit isn't processed

And the injected script in 7: looks something like:

* Check if global variable has value; if so, get it, if not, return
false and retry
* Remove the onclick handler that was injected in 3)
* Call submit() on the form
* Return the value you retrieved.

There are a number of problems with this approach. It hugely changes
the behaviour of the web app under test, it relies on timing quite
awkwardly, and it relies on the fact that you can stall submits and
resume them without worry (which isn't necessarily the case).

If you can give us a more concrete idea of what you're trying to do,
we can probably help better.

face20010

unread,
Oct 3, 2011, 6:21:17 PM10/3/11
to webdriver
Hi mate, cheers for the indepth answer. I meant to reply to you
yesterday but I had to work all day on a college assigment. Just to
clear a few things up, the "User" I was talking about is an actual
person, not the webdriver. And I am not using selenium for browser
automation, I using it for a data capture and analysis application -
basically selenium will handle the capturing part.

As you said you are still unsure of what Im trying to achieve I
created a simple example webpage that shows clearly exactly what Im
trying to do - http://www.viperwork.com/form_capture.html

As you can see there is a (empty) form on the page and there are a
couple of links the user can click. So...
1. The user clicks a few links and adds a few items to the form (I
know these aren't form <input> elements but it doesnt matter, I just
want to get the actual html in the form...)
2. They then click submit
3. I want to now capture the data they entered by returning the
innerHTML of the form.

As you can see, I can only get this data when they click submit as
there is no data beforehand, and when they click submit I know the
data will be there and ready to be captured. So now that I've,
hopefully, described what Im trying to do a bit more clearly, do you
think this can be done with selenium?


PS. I realise in earlier posts I said the user makes the form appear
via ajax by clicking on a link. I was actually wrong here, the form is
already on the page, it's the elements that get put in the form that
are added via ajax/javascript.
> On 1 October 2011 23:32,face20010<facecr...@live.ie> wrote:
>
>
>
>
>
>
>
> > Im not really sure how that works, cant find any examples on the net.
> > Here are the exact steps of what I am tring to achieve -
>
> > 1. User goes to sitewww.xyz.com
> > 2. They click a link that makes a form appear on the page via ajax
> > 3. They fill in some details on this form and click submit
> > 4. Now once they click submit I want to return the html contents of
> > this form to my c# application, the innerHTML value.
>
> > So any ideas on if this can be done with Selenium? Can
> > ExecuteAsyncScript do it?
>
> > On Sep 30, 6:39 am, Simon Stewart <simon.m.stew...@gmail.com> wrote:
> >> Take a look at ExecuteScriptAsync
>
> >> Simon
>
Reply all
Reply to author
Forward
0 new messages