Javascript to Java method callback in selenium webdriver script

1,172 views
Skip to first unread message

vinod kumar

unread,
Nov 4, 2015, 9:15:33 AM11/4/15
to Selenium Users
Hello All,

i am trying to automate a video player .... where while playing video it will trigger certain events like adStart, adEnd, pause, play .......

i am injecting js snippet mentioned below after launching url in browser using javascript executor

(function(){
    player.ads.addEventListener("adStarted",adStarted);
 })();

(function(){
    player.ads.addEventListener("adEnded",adEnded);
 })();

function adStarted(event) {
}

My requirement is like once the event is triggered for ex: adStarted it will invoke adStarted function and somehow that function should make a call to automation script and pass the javascript Object i.e, event as parameter to automation script.

could you please help me out in achieving this or let me know is it possible with selenium or not ?

Automation script steps... i am using java with testng
1. launch browser
2. open url
3. injecting listener javascript snippet
4. click on player play button
5. first ad's will start play and adStarted listener will invoke and it will call adStarted function.....

David

unread,
Nov 5, 2015, 12:37:29 AM11/5/15
to Selenium Users
And what would the automation script do with the javascript (event) object? What automation script are you referring to? the javascript snippet that gets injected when an event triggers will then callback to the Java TestNG test case?

You can't really pass back a javascript object, and definitely not a javascript event object. Java from Selenium won't be able to do much with it.

And any javascript executed by Java from Selenium does not wait for a callback. It just returns after executing the code snippet. So either your code snippet returns something back immediately or it does not return anything. If it is trying to return something later via event triggering, Java/Selenium won't be able to pick it up, at least not automatically.

The hack around that would be that either:

* you would kind of know when the event would trigger and just sleep/wait for it in Selenium until that time interval then call javascript execution to handle whatever at that point

* continuously poll from Java Selenium via javascript executor (and loops or maybe using ExpectedConditions from WebDriver) when the event has triggered, and if yes, then proceed onwards

as for both cases, how to proceed onward, you would typically have your event handling javascript function save state/data to some other javascript objects/variables. You can then retrieve the state from Java/Selenium later when you detect the event has occurred, via javascript executor to return back the javascript object value to Java as a Java value.

a sample object in javascript you could use to store the event state could be:

var eventResult = { "triggered" : false, "data" : "whatever data you wish to save and process later for the event, if any" };

and adStarted() for example at a minimum would set eventResult.triggered = true; and the java code would simply poll for when eventResult.triggered = true to continue to the next step.
Reply all
Reply to author
Forward
0 new messages