Difficulty Level: High
Prerequisites:
- Eclipse IDE installed
- selenium-grid-tools, selenium java client driver. jar from http://seleniumhq.org/download/
- Selenium RC server
- Firefox,Chrome and IE installed
- Driver.manage()
- Driver.get(baseUrl)
- Driver.findElement by id, name, linkText,cssSelector
- clear()
- sendkeys()
- click()
- random string
- Create a Java Project in Eclipse IDE.
- Now copy the script and save as CrossTest.java
- Include all the jar files with the created project.
- Start Selenium RC Server via command line.
- For running the same test we need to just change the driver like below script is for the Firefox driver and will run on same, so just running the script on IE, we need to load the IE browsers driver
- For IE ->
IWebDriver driver = new InternetExlorerDriver();
- For Google Chrome ->
IWebDriver driver = new ChromeDriver();
- See the Result
package com.example.tests;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Testcase1 {
private WebDriver driver;
private String baseUrl="";
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testCase1() throws Exception {
driver.get("/");
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();
driver.findElement(By.linkText("Posts")).click();
driver.findElement(By.cssSelector("a.button.add-new-h2")).click();
driver.findElement(By.id("title")).clear();
int a = Random().nextInt("123456789");
String post_title = "Title" +a;
driver.findElement(By.id("title")).sendKeys(post_title);
driver.findElement(By.id("excerpt")).clear();
driver.findElement(By.id("excerpt")).sendKeys("Post Excerpt");
driver.findElement(By.id("trackback_url")).clear();
driver.findElement(By.id("trackback_url")).sendKeys("http://www.google.com");
driver.findElement(By.id("publish")).click();
driver.findElement(By.linkText("Log Out")).click();
}
@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:
This example demonstrate how we can run the single test on different-2 browsers by just changing the browser parameter into the script’s setup method.
ReplyDeleteDone with the changes the script having Selenium2