Friday, 21 October 2011

Example-Cross Browser Testing

Aim:To run the Script on different browser.
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
Commands used in example:
  • Driver.manage()
  • Driver.get(baseUrl)
  • Driver.findElement by id, name, linkText,cssSelector
  • clear()
  • sendkeys()
  • click()
  • random string
Steps in implementation:
  1. Create a Java Project in Eclipse IDE.
  2. Now copy the script and save as CrossTest.java
  3. Include all the jar files with the created project.
  4. Start Selenium RC Server via command line.
  5. 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
  6. For IE ->
    IWebDriver driver = new InternetExlorerDriver();
  7. For Google Chrome ->
    IWebDriver driver = new ChromeDriver();
  8. See the Result
Scripts:save as CrossTest.java
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:

1 comment:

  1. 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.

    Done with the changes the script having Selenium2

    ReplyDelete