Wednesday 3 July 2013

Executing javascript using Selenium WebDriver


Using webdriver sometimes we need to run javascript code dircetly from our script.

In one of the previous post we have discussed how to break catcha on webpage . To break captcha we need to run javascript code directly from our script .So it is important to know how to run javacript code using WebDriver. Here are the steps:

1.Cast the WebDrier instance to a  JavascriptExecutor
  1. WebDriver driver;  
  2. JavascriptExecutor js = (JavascriptExecutor) driver;  
2.Use executeScript method to run the script
  1. js.executeScript("return document.title");  
Here is the sample script using TestNG.
  1. import java.util.ArrayList;  
  2. import org.openqa.selenium.JavascriptExecutor;  
  3. import org.openqa.selenium.WebDriver;  
  4. import org.openqa.selenium.firefox.FirefoxDriver;  
  5. import org.testng.Reporter;  
  6. import org.testng.annotations.AfterTest;  
  7. import org.testng.annotations.BeforeTest;  
  8. import org.testng.annotations.Test;  
  9.   
  10. public class ExecuteJavascript {  
  11.       
  12.  WebDriver driver;  
  13.   
  14.  @BeforeTest  
  15.  public void start(){  
  16.   driver = new FirefoxDriver();  
  17.  }  
  18.    
  19.  @Test  
  20.  public void javaScriptExec(){  
  21.     
  22.   driver.get("http://duckduckgo.com/");  
  23.   JavascriptExecutor js=(JavascriptExecutor) driver;  
  24.     
  25.   String readyState=(String)js.executeScript("return document.readyState");  
  26.   System.out.println("readyState  : "+readyState);  
  27.   
  28.   String title=(String)js.executeScript("return document.title");  
  29.   System.out.println("title  : "+title);  
  30.             
  31.   String domain=(String)js.executeScript("return document.domain");  
  32.   System.out.println("domain  : "+domain);  
  33.             
  34.     
  35.   String lastModified=(String)js.executeScript("return document.lastModified");  
  36.   System.out.println("lastModified  : "+lastModified);  
  37.             
  38.   String URL=(String)js.executeScript("return document.URL");  
  39.   System.out.println("Full URL  : "+URL);  
  40.   
  41.  String error=(String) ((JavascriptExecutor) driver).executeScript("return window.jsErrors");  
  42.   System.out.println("Windows js errors  :   "+error);  
  43.              
  44.    }  
  45.   
  46. @AfterTest  
  47.  public void stop(){  
  48.  driver.quit();  
  49.  }  
  50.   
  51. }  
If you run the above program then the output will come like below :

readyState  : complete
title  : DuckDuckGo
domain  : duckduckgo.com
lastModified  : 02/02/2013 13:11:42
Full URL  : https://duckduckgo.com/
Windows js errors  :   null

46 comments:

  1. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.

    Click here:
    angularjs4 Training in Chennai
    Click here:
    angularjs5 Training in Chennai

    ReplyDelete
    Replies
    1. Useful information. keep posting. IntelliMindz is the best IT Training in Bangalore with placement, offering 200 and more software courses with 100% Placement Assistance.

      Selenium Training In Bangalore
      Splunk Training In Bangalore
      Tableau Training In Bangalore
      Power BI Course In Bangalore
      Building Estimation and Coasting Course In Bangalore

      Delete
  2. I am a regular reader of your blog and being students it is great to read that your responsibilities have not prevented you from continuing your study and other activities. Love
    Click here:
    Microsoft azure training in chennai

    ReplyDelete
  3. I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post I would like to read this

    click here

    Selenium Training in Bangalore|
    Selenium Training in Chennai

    ReplyDelete
  4. Thanks for such a great article here. I was searching for something like this for quite a long time and at last I’ve found it on your blog. It was definitely interesting for me to read  about their market situation nowadays.
    java training in tambaram | java training in velachery

    java training in omr | oracle training in chennai

    ReplyDelete
  5. This is a good post. This post give truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. thank you so much. Keep up the good works.
    angularjs Training in marathahalli

    angularjs interview questions and answers

    angularjs Training in bangalore

    angularjs Training in bangalore

    angularjs Training in chennai

    ReplyDelete
  6. If so how do you stop it, any plugin or anything you can advise? I get so much lately it’s driving me insane, so any assistance is very much appreciated.
    industrial course in chennai

    ReplyDelete
  7. Very creativity blog!!! I learned a lot of new things from your post. It is really a good work and your post is the knowledgeable. Waiting for your more updates...
    Blue Prism Training Institute in Bangalore
    Blue Prism Course in Bangalore
    Blue Prism Training Bangalore
    Blue Prism Classes in Bangalore
    Blue Prism Course in Adyar
    Blue Prism Training in Mogappair

    ReplyDelete
  8. Thanks for sharing this Information. The information you Provided is much useful.

    Best Microsoft Training in Bangalore for Microsoft, we provide the Microsoft training project with trainers having more than 5 Years of Microsoft training experience, we also provide 100% placement support.

    ReplyDelete
  9. This comment has been removed by a blog administrator.

    ReplyDelete
  10. Awesome..You have certainly explained …Its very beneficial for me to recognise about new matters.
    .Keep on blogging.
    click here for more info.

    ReplyDelete
  11. This site certainly has all the info I wanted concerning this subject and didn’t know who to ask.

    ReplyDelete
  12. Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work.
    Angular js Training in Chennai

    Angular js Training in Velachery

    Angular js Training in Tambaram

    Angular js Training in Porur

    Angular js Training in Omr

    Angular js Training in Annanagar



    ReplyDelete
  13. You can't imagine what I am going to tell you. Our institution is offering you a CS executive classes and a free of cost CSEET classes and many more to explore. So please contact us or visit our website at https://uniqueacademyforcommerce.com/

    ReplyDelete
  14. I'm dazzled, I should say. Only sometimes do I go over a blog that is both educative and intriguing, and doubtlessly, you have nailed it. The issue is an issue that insufficient people are talking cleverly about. Presently i'm upbeat I unearthed this during my chase for something with respect to this. tech updates

    ReplyDelete
  15. This is the correct site for every individual who might want to get some answers concerning this point. You understand such a lot of its practically intense to contend with you (not that I really would need to… HaHa). You certainly put a new turn on a theme which has been talked about for a very long time. Awesome stuff, simply extraordinary! tech updates

    ReplyDelete
  16. Don't Waste Your Time Checking USD TO INR FORECAST Every Day! Get The Most Accurate Exchange Rate For The USD TO INR FORECAST With Our Original Universal Currency Converter.

    ReplyDelete
  17. Do You Now AximTrade Review Login Is A Secure, Multi-channel, Multi-factor Authentication System, Enabling Customers To Securely Access Their Accounts To Fund/deposit, Request Withdrawal, Update Or Manage Their Profile And More.

    ReplyDelete
  18. Do You Know AximTrade Review Is A Global Financial Firm That Offers A Wide Range Of Financial Services, Including Fx, Cfd, No Deposit Bonus And More Sign Up With AximTrade Login Account And Trade In Forex

    ReplyDelete
  19. Great post. keep sharing such a worthy information.
    Python Training Institute In Chennai

    ReplyDelete
  20. Thank you for Sharing best Useful information !!!!
    Jewellery Billing Software
    Jewellery Billing Software

    ReplyDelete

Angular JS Protractor Installation process - Tutorial Part 1

                     Protractor, formally known as E2E testing framework, is an open source functional automation framework designed spe...