Difficulty Level: HIGH
Pre-requisites:
- JDK (JAVA version "1.6.021") should be installed and working
- Ant (Apache Ant version 1.7.1) should be working
- selenium-server-standalone-2.12.0.jar
- testng-5.14.1.jar
IWebDriver driver = new InternetExlorerDriver();
IWebDriver driver = new ChromeDriver();
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);
}
}
}
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){}
}
}
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);
}
}
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();
}
}
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();
}
}
package com.example.tests;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;
public class QuickTest extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://foundation.time2test.co.uk/");
selenium.start();
}
@Test
public void testQuick() throws Exception {
selenium.open("/");
selenium.click("link=Log in");
selenium.waitForPageToLoad("30000");
assertTrue(selenium.isTextPresent("Username"));
assertTrue(selenium.isTextPresent("Password"));
selenium.type("id=user_login", "admin");
selenium.type("id=user_pass", "time4test");
selenium.click("id=wp-submit");
selenium.waitForPageToLoad("30000");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isElementPresent("//div[@id='user_info']/p/a[2]")) break; } catch (Exception e) {}
Thread.sleep(1000);
}
selenium.click("link=Posts");
selenium.waitForPageToLoad("30000");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isElementPresent("//div[@id='wpbody-content']/div[3]/h2")) break; } catch (Exception e) {}
Thread.sleep(1000);
}
selenium.click("link=Add New");
selenium.waitForPageToLoad("30000");
selenium.type("id=title", "MyPost");
selenium.click("id=publish");
selenium.waitForPageToLoad("30000");
selenium.click("link=Users");
selenium.waitForPageToLoad("30000");
assertTrue(selenium.isTextPresent("Users"));
selenium.click("//li[@id='menu-users']/div[3]/ul/li[2]/a");
selenium.waitForPageToLoad("30000");
selenium.type("id=user_login", selenium.getEval(""Testuser" + new Date().getMilliseconds();"));
selenium.type("id=email", selenium.getEval(""Testemail" + new Date().getMilliseconds()+"@gmail.com";"));
selenium.type("id=first_name", "TestFirst");
selenium.type("id=last_name", "TestLast");
selenium.type("id=url", "http://www.google.com/");
selenium.type("id=pass1", "time2test");
selenium.type("id=pass2", "time2test");
selenium.click("id=addusersub");
selenium.waitForPageToLoad("30000");
selenium.click("link=Log Out");
selenium.waitForPageToLoad("30000");
}
@After
public void tearDown() throws Exception {
selenium.stop();
}
}