Showing posts with label 1. Show all posts
Showing posts with label 1. Show all posts

Tuesday, 19 July 2011

Example - Selenium 2.0 using WebDriver API

Aim:
Example to demonstrate Selenium 2.0 using Webdriver API, here are the aims of examples:

  1.  to setup the development environment - Eclipse , jUnit and Selenium 2
  2. to run and execute your first selenium 2 test script
  3. get a feel for the commands and execution on Eclipse



Difficulty Level: High

Exercise Execution Time: more details coming soon

Prerequisites:
  1. None - this example will demonstrate installation of all the executables
Commands used in example:
  • Driver.manage()
  • Driver.get(baseUrl)
  • Driver.findElement by id, name, linkText and xpath
  • clear()
  • sendkeys()
  • click()
  • random string
Steps in implementation:
  1. Download and Install Eclipse IDE using link http://www.eclipse.org/downloads/
  2. Tip: download the version suitable for your PC ( 32 bit/64bit))
  3. Download the Selenium2 API 'selenium-server-standalone-*.jar' library using link http://seleniumhq.org/download
  4. No need to get JUnit jar file in separate as it is inbuilt in  'selenium-server-standalone-*.jar'.
  5. Launch the Eclipse IDE
  6. Create a new Java project and name it FirstExample. You will see a default folder 'src' inside it.
  7. Do right click on the created project and select Build Path >> Configuration Build Path
  8. Add 'selenium-server-standalone-*.jar' under Libraries tab by clicking 'Add External Jars'.
  9. Right click on 'src' and add a package 'com.example.tests'.
  10. Create two java classes  'addPost.java' and 'addUser.java' into the created package.
  11. Paste the below mentioned two script in corresponding java files.
  12. Right click 'addPost.java' and click 'Run as >> JUnit test'.
What does script simulate?
    1. login using username and password to foundation.time2test.co.uk wordpress portal
    2. click "posts"
    3. Click on "add new"
    4. enter details in form for new post like title,
    5. click on "publish"
    6. click on "users"
    7. enter details for new user in form
    8. click on add user
Java Files: Save as addPost.java
    package com.example.tests;
    
    import java.util.concurrent.TimeUnit;
    import org.junit.*;
    import static org.junit.Assert.*;
    import org.openqa.selenium.*;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class addPost {
    private WebDriver driver;
    // Initializing the parameters
    private String baseUrl="http://foundation.time2test.co.uk/";
    private StringBuffer verificationErrors = new StringBuffer();
    @Before
    public void setUp() throws Exception {
    // Initializing the driver that script would run the Firefox
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }
    
    @Test
    public void testSelenium2Example() throws Exception {
    // opening the base URL using the driver's get command
    driver.get(baseUrl);
    // Login into the application using the valid credential
    driver.findElement(By.linkText("Log in")).click();
    driver.findElement(By.id("user_login")).clear();
    driver.findElement(By.id("user_login")).sendKeys("admin");
    driver.findElement(By.id("user_pass")).clear();
    driver.findElement(By.id("user_pass")).sendKeys("time4test");
    driver.findElement(By.id("wp-submit")).click();
    
    // Asserting if we have logged into the application or not
    for (int second = 0;; second++) {
    if (second >= 60) fail("timeout");
    try { if (isElementPresent(By.xpath("//div[@id='user_info']/p/a[2]"))) break; } catch (Exception e) {}
    Thread.sleep(1000);
    }
    
    // Clicking the Post link for adding a new post
    driver.findElement(By.linkText("Posts")).click();
    for (int second = 0;; second++) {
    if (second >= 60) fail("timeout");
    try { if (isElementPresent(By.xpath("//div[@id='wpbody-content']/div[3]/h2"))) break; } catch (Exception e) {}
    Thread.sleep(1000);
    }
    // Add a new post
    driver.findElement(By.linkText("Add New")).click();
    driver.findElement(By.id("title")).clear();
    driver.findElement(By.id("title")).sendKeys("MyPost New Post");
    driver.findElement(By.id("publish")).click();
    // calling a method for adding an user
    addUser se=new addUser();
    se.seleniumExample(driver);
    
    }
    
    @After
    public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
    fail(verificationErrorString);
    }
    }
    
    private boolean isElementPresent(By by) {
    try {
    driver.findElement(by);
    return true;
    } catch (NoSuchElementException e) {
    return false;
    }
    }
    }
    
    
Save as addUser.java
    package com.example.tests;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.*;
    import java.util.*;
    
    public class addUser {
    
    public void seleniumExample(WebDriver driver)
    {
    driver.findElement(By.linkText("Users")).click();
    // generating a random value
    driver.findElement(By.xpath("//li[@id='menu-users']/div[3]/ul/li[2]/a")).click();
    Random rand = new Random();
    int numNoRange = rand.nextInt();
    String userstr="TestNewUser"+numNoRange;
    String email="Testemail"+numNoRange+"@yahoo.com";
    driver.findElement(By.id("user_login")).clear();
    // Using the random values in adding a new user
    driver.findElement(By.id("user_login")).sendKeys(userstr);
    driver.findElement(By.id("email")).clear();
    driver.findElement(By.id("email")).sendKeys(email);
    // Adding an user
    driver.findElement(By.id("first_name")).clear();
    driver.findElement(By.id("first_name")).sendKeys("TestFirst");
    driver.findElement(By.id("last_name")).clear();
    driver.findElement(By.id("last_name")).sendKeys("TestLast");
    driver.findElement(By.id("url")).clear();
    driver.findElement(By.id("url")).sendKeys("http://www.google.com/");
    driver.findElement(By.id("pass1")).clear();
    driver.findElement(By.id("pass1")).sendKeys("time2test");
    driver.findElement(By.id("pass2")).clear();
    driver.findElement(By.id("pass2")).sendKeys("time2test");
    driver.findElement(By.id("addusersub")).click();
    }
    }
Helpful Video: