Aim:
Example to demonstrate Selenium 2.0 using Webdriver API, here are the aims of examples:
Difficulty Level: High
Exercise Execution Time: more details coming soon
Prerequisites:
Example to demonstrate Selenium 2.0 using Webdriver API, here are the aims of examples:
- to setup the development environment - Eclipse , jUnit and Selenium 2
- to run and execute your first selenium 2 test script
- get a feel for the commands and execution on Eclipse
Difficulty Level: High
Exercise Execution Time: more details coming soon
Prerequisites:
- None - this example will demonstrate installation of all the executables
- Driver.manage()
- Driver.get(baseUrl)
- Driver.findElement by id, name, linkText and xpath
- clear()
- sendkeys()
- click()
- random string
- Download and Install Eclipse IDE using link http://www.eclipse.org/downloads/
- Tip: download the version suitable for your PC ( 32 bit/64bit))
- Download the Selenium2 API 'selenium-server-standalone-*.jar' library using link http://seleniumhq.org/download
- No need to get JUnit jar file in separate as it is inbuilt in 'selenium-server-standalone-*.jar'.
- Launch the Eclipse IDE
- Create a new Java project and name it FirstExample. You will see a default folder 'src' inside it.
- Do right click on the created project and select Build Path >> Configuration Build Path
- Add 'selenium-server-standalone-*.jar' under Libraries tab by clicking 'Add External Jars'.
- Right click on 'src' and add a package 'com.example.tests'.
- Create two java classes 'addPost.java' and 'addUser.java' into the created package.
- Paste the below mentioned two script in corresponding java files.
- Right click 'addPost.java' and click 'Run as >> JUnit test'.
- login using username and password to foundation.time2test.co.uk wordpress portal
- click "posts"
- Click on "add new"
- enter details in form for new post like title,
- click on "publish"
- click on "users"
- enter details for new user in form
- click on add user
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: