Creating java script event listeners for webdriver instance.

130 views
Skip to first unread message

suneel kumar

unread,
Aug 29, 2014, 11:06:14 PM8/29/14
to seleniu...@googlegroups.com

Hi all,

I want to create record and play back program in java for webdriver instance.. We can achieve it through java script...

Now the question is how to interact with the java script listeners through webdriver code. Any ideas appreciated..

Thanks and Regards,
Suneel Kumar Oleti..

Xiang Dong

unread,
Aug 31, 2014, 9:42:05 PM8/31/14
to seleniu...@googlegroups.com
Hi Kumar,

A possible way is that you serialize user web action to a string and send it back to your Java program by Ajax. then, java program can parse the input string to anything.

Of course it requires your java program be a web application. 

The other way maybe you can write your serialized string to a javascript variable and read by web driver periodically, but I think it must be very complicated, you must consider 

the race condition between your java program and AUT web page. such as click a link on AUT web page and cause page navigate to other page, for this scenario, it is not easy for your java 

program to read the javascript variable because it may disappear with the page navigation.

Best Regards,
--david


Date: Sat, 30 Aug 2014 08:35:59 +0530
Subject: [selenium-users] Creating java script event listeners for webdriver instance.
From: o.sunee...@gmail.com
To: seleniu...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CAFyF91zNCTFDdVFmgi7G%2Buy%3DqYv3e4LM77YVNt%3DPWTStnesw7A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

suneel kumar

unread,
Sep 1, 2014, 12:57:50 AM9/1/14
to seleniu...@googlegroups.com

Hi Xiang,

I have developed a extension which has js and gonna start my driver with that extension loaded. Now trying to talk to the js script which is loaded.
Is there a better approach than this??

Regards,
Suneel

Xiang Dong

unread,
Sep 1, 2014, 5:21:54 AM9/1/14
to seleniu...@googlegroups.com
HI Sunnel,

Web driver are running in your test program, Javascript are running on the AUT web page. It is hard to let your JS send message to the test program, not sure do we has other approaches, Send Ajax request maybe the most possible way to do it. 
Web driver has the capability to execute JS on the AUT web page, consider such scenario, javascript capture user action and format it to string, then, save string to a javascript variable, web driver will retrieve this variable periodically. it has a race condition between reader and writer. especially when your web page get navigation. 
I am not very familiar with browser extension, but like Selenium Firefox IDE, it also can generate test script without sending Ajax request. But I am sorry I didn't use IDE and know it. Maybe you can use it as a reference.

Best Regards,
--david


Date: Mon, 1 Sep 2014 10:26:50 +0530
Subject: RE: [selenium-users] Creating java script event listeners for webdriver instance.

suneel kumar

unread,
Sep 1, 2014, 11:38:03 AM9/1/14
to seleniu...@googlegroups.com

Hi,

I'm going for the worst case scenario where I want to store the actions in a JS string and retrieve it periodically.. But not able to do that, my Webdriver code is not waiting for the return of the JS.. It is simply going ahead.

Any way, where I can achieve it??

Regards,
Suneel

Xiang Dong

unread,
Sep 1, 2014, 9:36:46 PM9/1/14
to seleniu...@googlegroups.com
Hi Suneel,

If you use your web driver retrieve a js variable back, it will back immediately. That's why you should retrieve it periodically, the interval should be small than the gap in two user actions. otherwise. I think it is reasonable and possible to reach the goal. The only thing I mentioned before is that if your web page get changed, the old js variable will be disappear.

--david


Date: Mon, 1 Sep 2014 21:07:46 +0530

suneel kumar

unread,
Sep 1, 2014, 10:07:32 PM9/1/14
to seleniu...@googlegroups.com

Hi,

Yeah I agree with that.. But I have implicit wait in my JS before returning value. It is working fine when I run it individually, where as with webdriver it is exiting immediately..

Regards,
Suneel

Xiang Dong

unread,
Sep 2, 2014, 3:56:03 AM9/2/14
to seleniu...@googlegroups.com
1. first , how do you do implicit wait in js? jaavscript does not have wait or something similar, do you use a timer to do it?
 you can't let user wait, for example, you capture a action and store to js variable, you expect web driver read it before next user action, but it is not easy to guarantee, I guess what you can do is less the sleep time in Java code.

2. I am not fully understand what do you mean web driver exiting immediately, if you put your read codes in a loop, you can periodically read the variable, if codes is correct, you must can read something back.

--david


Date: Tue, 2 Sep 2014 07:37:17 +0530

suneel kumar

unread,
Sep 2, 2014, 9:24:42 AM9/2/14
to seleniu...@googlegroups.com

Hi,

Below I'm adding the JS code which I used :

var flag = 0;

       var elementId;

   

       window.addEventListener("click", function(e){        

       elementTagName=e.target.id;

       alert(elementTagName);  

       flag++;});

   

      var timer = setInterval(function(){myTimer()},1000); 

 

      function myTimer() {   

       if(flag==0){

        document.getElementById("demo").innerHTML=flag;

        }else {

         clearInterval(timer);

         return elementId; //Returning the element ID which was clicked

        }

       }

Regrets for the format, I'm sending it from mobile..

Regards,
Suneel.

Xiang Dong

unread,
Sep 2, 2014, 11:20:36 PM9/2/14
to seleniu...@googlegroups.com
HI Suneel,

I am a little confused that do you want to your web driver codes waits timer timeout and get result? which method are you call in your javacode? is it myTimer()?

If yes, I do think the web driver call will return immediately because it only consider it is a javascript method, has nothing to do with your timer.

the typical solution may like below:


JS:

var recordAction='';

window.addEventListener("click", function(e){        

       var elementId=e.target.id;

       recordAction = "action=click;elementId="+elementId;

   }

);


on your java side


call js "return recordAction;" periodically. 



I didn't understand the meaning of your timer. you can't let a js timer work with your java codes because there are separate.


Best Regards,

--david



Date: Tue, 2 Sep 2014 18:54:03 +0530

suneel kumar

unread,
Sep 2, 2014, 11:45:20 PM9/2/14
to seleniu...@googlegroups.com

Hi David ,

Thanks a lot. I did little research also on this. I need a better approach rather than periodically calling this. Can we store all the actions in file or something else and read it at once through our java program??

Which would be little efficient.

But the main problem is browser side script has limitations for writing in local file.
Any other way which we can achieve it? Like store in some temp file and read that file.

Regards,
Suneel

Xiang Dong

unread,
Sep 3, 2014, 12:58:31 AM9/3/14
to seleniu...@googlegroups.com
My suggestion is send ajax request from js to java side, maybe javascript socket also be a solution. But I think the point is the same. we should let javascript send message to Java. 

If your java side is not a web application, I guess socket is more better for you.


Best Regards,
--david


Date: Wed, 3 Sep 2014 09:15:04 +0530
Reply all
Reply to author
Forward
0 new messages