Aim:Example to demonstrate Selenium 2 using JUnit
Difficulty Level:High
Prerequisites:
Steps:
Junit - http://www.roseindia.net/junit/writingandtesting.shtml
Script:Save as Selenium2Example.java
Helpful Video:
Difficulty Level:High
Prerequisites:
- Eclipse IDE installed
- Download selenium 2 API Jar files from http://code.google.com/p/selenium/downloads/list
- JUnit4.7 jar
- Driver.manage()
- Driver.get(baseUrl)
- Driver.findElement by id, name, link text, css and xpath
- IsElementPresent()
- clear()
- sendkeys()
- click()
- capture screen shot
- store value
- random string
- Login into the Wordpress content management application.
- Creating a new user.
- Asserting that if new user is created or not.
- verify if Comments page is available into the application.
- Verify if user is able to post.
- Check if user's post is exist into the application.
- Verify if user is able to create a new page.
- Check if created page does exist into the application.
- Verify that user is able to add a link into the application.
- Check if added links exist into the application.
- verify if user is able to add a new category.
- Check if added categories exist into the application.
- Verify if user is able to add a new Media.
- Check if added Media exists into the application.
Steps:
- Use the IDE to record a simple script
- convert into Junit/Webdriver Formatted Code using the IDE
- Save the code in java Formate.
- Launch Eclipse
- Create a Java project directory, see the attached video file for how to create Java project in Eclipse
- Move saved java file to the project directory
- Now include the Selenium 2 API jars and JUnit jar with the created Project.
- Run the Project
- See the Result
- All screen shot would saved into C:\\tmp folder path
Junit - http://www.roseindia.net/junit/writingandtesting.shtml
Script:Save as Selenium2Example.java
package com.example.tests;
import java.io.File;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Selenium2Example {
private WebDriver driver;
private String baseUrl="http://foundation.time2test.co.uk/";
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void test() throws Exception {
driver.get(baseUrl);
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();
// Creating a new user in Word press
driver.findElement(By.linkText("Users")).click();
driver.findElement(By.cssSelector("a.button.add-new-h2")).click();
driver.findElement(By.id("user_login")).clear();
int a=new Random().nextInt(123456789);
String User_name= "testuser"+a;
driver.findElement(By.id("user_login")).sendKeys(User_name);
driver.findElement(By.id("email")).clear();
String Email_address= "testemail"+a+"@gmail.com";
driver.findElement(By.id("email")).sendKeys(Email_address);
driver.findElement(By.id("first_name")).clear();
driver.findElement(By.id("first_name")).sendKeys("Word");
driver.findElement(By.id("last_name")).clear();
driver.findElement(By.id("last_name")).sendKeys("Press");
driver.findElement(By.id("url")).clear();
driver.findElement(By.id("url")).sendKeys("http://www.test.com");
driver.findElement(By.id("pass1")).clear();
driver.findElement(By.id("pass1")).sendKeys("123-asd");
driver.findElement(By.id("pass2")).clear();
driver.findElement(By.id("pass2")).sendKeys("123-asd");
driver.findElement(By.id("addusersub")).click();
driver.findElement(By.cssSelector("input.button")).click();
//Verify the created user via screenshot...
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\tmp\\user.png"));
// Adding post in Word press
driver.findElement(By.linkText("Posts")).click();
File scrFile1 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//Verify the open post list via screenshot...
FileUtils.copyFile(scrFile1, new File("c:\\tmp\\Post.png"));
driver.findElement(By.linkText("Add New")).click();
driver.findElement(By.id("title")).clear();
// generating random values and using the same
String postname= "testpost"+a;
// below command is used for the Echo purpose, if we have the random value in to the variable or no
driver.findElement(By.id("title")).sendKeys(postname);
// Using wait for element present function below
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (isElementPresent(By.id("publish"))) break; } catch (Exception e) {}
Thread.sleep(1000);
}
// finding element by xpath
driver.findElement(By.xpath("//input[@id='publish']")).click();
// Creating a new page in word press
driver.findElement(By.linkText("Pages")).click();
driver.findElement(By.cssSelector("a.button.add-new-h2")).click();
driver.findElement(By.id("title")).clear();
String Page_name= "tespage"+a;
driver.findElement(By.id("title")).sendKeys(Page_name);
driver.findElement(By.id("publish")).click();
//Verify the created page via screenshot...
File scrFile2 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile2, new File("c:\\tmp\\Page.png"));
driver.findElement(By.linkText("Pages")).click();
driver.findElement(By.id("post-search-input")).clear();
// Searching for the created page if it's added or not
driver.findElement(By.id("post-search-input")).sendKeys(Page_name);
driver.findElement(By.cssSelector("p.search-box > input.button")).click();
// Working with Links page
driver.findElement(By.linkText("Links")).click();
driver.findElement(By.cssSelector("a.button.add-new-h2")).click();
driver.findElement(By.id("link_name")).clear();
String Test_link= "teslink"+a;
driver.findElement(By.id("link_name")).sendKeys(Test_link);
driver.findElement(By.id("publish")).click();
//Verify the created link via screenshot...
File scrFile3 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile3, new File("c:\\tmp\\Links.png"));
// adding a new category
driver.findElement(By.linkText("Link Categories")).click();
driver.findElement(By.id("link-name")).clear();
String Test_category= "teslinkcategory"+a;
driver.findElement(By.id("link-name")).sendKeys(Test_category);
driver.findElement(By.name("submit")).click();
driver.findElement(By.id("link-category-search-input")).clear();
driver.findElement(By.id("link-category-search-input")).sendKeys(Test_category);
driver.findElement(By.cssSelector("input.button")).click();
// Navigating Media page
driver.findElement(By.linkText("Media")).click();
driver.findElement(By.cssSelector("a.button.add-new-h2")).click();
//Verify the created Media page via screenshot...
File scrFile4 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile4, new File("c:\\tmp\\Media.png"));
driver.findElement(By.linkText("Log Out")).click();
// going to capture screen shot of the logout screen
File scrFile5 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile5, new File("c:\\tmp\\screenshot.png"));
}
@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;
}
}
}
Helpful Video: