Saturday, 1 February 2014

Creating webdriver scripts



·         Creating the scripts
There are multiple ways to create WebDriver scripts, execute the scripts, Let us discuss about them.
1. Using SELENIUM IDE   : 
·         Open Firefox browser
·         Go to Tools->Open Selenium IDE
·         Record the script
·         Export the script to Desired format in desired language
  

·         Save the Test case
·         Now open the Recorded script, it will look like below :

              Note : As it don’t have any main method, You can run those scripts using Junit/TestNG



     1.  Hand Coding:
You can create selenium script by just coding yourself in eclipse, for this you just need to create     a simple java program with the main method, Let us stop theoretical and start practising it
·         Go to WebDriver project ,that we have created in Eclipse
·         Right click -> New->Class

·         Specify Name as “HandCodedTest” and select the public static void main(String[] args) check box as shown in below pic:


·         Click on ‘Finish’, Now it will create an empty Class body as below:

·         Now we have to write our test in the main method, for that we need to import the following references
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

We have to write the actions which you want to do in main method, For example look at the below code to login to Yahoo mail

WebDriver driver;
            driver = new FirefoxDriver();
            String baseUrl = "http://my.naukri.com/";
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
           
                  driver.get("https://login.yahoo.com/config/login_verify2?.intl=in
      &.src=ym");
            driver.findElement(By.id("username")).clear();
            driver.findElement(By.id("username")).sendKeys("re");
            driver.findElement(By.id("passwd")).clear();
            driver.findElement(By.id("passwd")).sendKeys("re");
            driver.findElement(By.id(".save")).click();
         driver.quit();

·         Finally your complete code class looks like
           


        You can run the above script by Right click->Java Application
NOTE: It is always better to record the script and export it to the language you required, and tweak your code as per your requirements; Complete hand coding is time taking and not even worth.

1 comment:

  1. at HandCodedTest.main(HandCodedTest.java:12)
    Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 1 more

    getting this error

    ReplyDelete