Thursday, 20 October 2011

FirstExample - Selenium 2 using WebDriver API

Aim:Example to demonstrate Selenium 2 using Webdriver API.
Difficulty Level: High


Prerequisites:
  1. Eclipse IDE installed
Commands used in example:
  • Driver.manage()
  • Driver.get(baseUrl)
  • Driver.findElement by id, name, link text, css and xpath
  • IsElementPresent()
  • clear()
  • sendkeys()
  • click()
  • random string
  • assertTrue()
  • getEval()
Steps in implementation:
  1. In first step, created a Java Project directory On Eclipse IDE.
  2. In second step. copy all the scripts and save as .java formate.
  3. Downloaded Selenium 2 Library.
  4. Included all the downloaded libraries with the project’s directoryaccording to video.
  5. Run the scripts successfully.

Scripts:
Save as Suite.java
package com.example.tests;
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Suite {
private WebDriver driver;
private String baseUrl="http://foundation.time2test.co.uk/";
public StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testSuite() throws Exception {
Login ln=new Login(driver);
ln.loginAs();
PostBlog post=new PostBlog(driver);
post.newPost();
AddUser user=new AddUser(driver);
user.newUser();
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}

}


Save as Login.java
package com.example.tests;
import org.openqa.selenium.*;
import org.testng.Assert;
import com.thoughtworks.selenium.Selenium;

public class Login {
private WebDriver driver;
public Login(WebDriver driver) {
this.driver = driver;
}
public void loginAs() {
driver.get("http://foundation.time2test.co.uk/");
Selenium selenium = new WebDriverBackedSelenium(driver, "http://foundation.time2test.co.uk/");
driver.findElement(By.linkText("Log in")).click();
driver.findElement(By.id("user_login")).clear();
Assert.assertTrue(selenium.isTextPresent("Username"));
Assert.assertTrue(selenium.isTextPresent("Password"));
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();
try{
Thread.sleep(3000);
}catch(Exception e){}
}
}

Save as AddUser.java
package com.example.tests;
import org.openqa.selenium.*;
import com.thoughtworks.selenium.Selenium;
import java.util.*;


public class AddUser {
private WebDriver driver;
public AddUser(WebDriver driver) {
this.driver = driver;
}
public void newUser() throws Exception
{
driver.findElement(By.linkText("Users")).click();

Random rand = new Random();
int numNoRange = rand.nextInt();
String userstr="TestNewUser"+numNoRange;
String email="Testemail"+numNoRange+"@yahoo.com";
driver.findElement(By.xpath("//li[@id='menu-users']/div[3]/ul/li[2]/a")).click();

driver.findElement(By.id("user_login")).clear();
driver.findElement(By.id("user_login")).sendKeys(userstr);
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys(email);

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();
DeleteUser del=new DeleteUser(driver);
del.delUser(userstr);
}

}

Save as DeleteUser.java
package com.example.tests;

import org.openqa.selenium.*;
import com.thoughtworks.selenium.*;
import org.openqa.selenium.WebElement;

public class DeleteUser {
private WebDriver driver;
public DeleteUser(WebDriver driver) {
this.driver = driver;
}
public void delUser(String user) throws Exception
{
Selenium seleniumDriver = new WebDriverBackedSelenium(driver, "http://foundation.time2test.co.uk/");
driver.findElement(By.cssSelector("a.wp-first-item.current")).click();
driver.findElement(By.id("user-search-input")).clear();
driver.findElement(By.id("user-search-input")).sendKeys(user);
driver.findElement(By.cssSelector("input.button")).click();
driver.findElement(By.xpath("//div[@id='wpwrap']/div[@id='wpcontent']/div[@id='wpbody']/div[@id='wpbody-content']/div[3]/form[@id='posts-filter']/table/thead/tr/th[@id='cb']/input")).click();
WebElement element = driver.findElement(By.name("action"));
/*seleniumDriver.select("name=action", "label=Delete");
seleniumDriver.click("id=doaction");
seleniumDriver.waitForPageToLoad("30000");
*/

element.sendKeys("Delete");
driver.findElement(By.id("doaction")).click();
driver.findElement(By.name("submit")).click();
}

}



Save as PostBlog.java
package com.example.tests;
import org.openqa.selenium.*;
import com.thoughtworks.selenium.Selenium;


public class PostBlog {
private WebDriver driver;
public PostBlog(WebDriver driver) {
this.driver = driver;
}
public void newPost()throws Exception
{
Selenium selenium = new WebDriverBackedSelenium(driver, "http://foundation.time2test.co.uk/");
driver.findElement(By.linkText("Posts")).click();
try{
Thread.sleep(3000);
}catch(Exception e){}

driver.findElement(By.linkText("Add New")).click();
driver.findElement(By.id("title")).clear();
selenium.type("id=title", selenium.getEval(""Post" + new Date().getMilliseconds();"));
driver.findElement(By.id("publish")).click();
driver.findElement(By.cssSelector("a.wp-first-item.current")).click();
}

}




Helpful Video:

No comments:

Post a Comment