testngとjunitの組み合わせselenium

4523 ワード

testngとjunitはseleniumを組み合わせて動的にデータソースを提供するテストを行うことができます.まずjunitを見て、みんなのシーンは異なるキーワードを提供して、googleを検索します.
junit
  


import static org.junit.Assert.fail;
 
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
 
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;
 
import java.util.Arrays;
import java.util.List;
 
@RunWith(Parameterized.class)
public class JunitGoogleBase {
  public Selenium selenium;
  WebDriver driver;
  private String testData;
   
  public JunitGoogleBase(String testData){
   this.testData=testData;
  }
   
  @Parameters
   public static List< Object[]> data() {
    return Arrays.asList(new Object[][]{{"testing"},{"Software testing"}});
   }
 
  @Before
  public void setUp() throws Exception {
   driver= new FirefoxDriver();
   selenium = new WebDriverBackedSelenium(driver, "http://www.google.com"); 
   selenium.open("http://www.google.com");
  }
 
  @Test
  public void testSearch() throws Exception {
   selenium.open("/");
   selenium.type("id=lst-ib", testData);
   selenium.click("//input[@value='Google Search']");
   for (int second = 0;; second++) {
    if (second >= 60) fail("timeout");
    try { if (selenium.isElementPresent("link=Software testing - Wikipedia, the free encyclopedia")) break; } catch (Exception e) {}
    Thread.sleep(1000);
   }
   selenium.click("link=Software testing - Wikipedia, the free encyclopedia");
   for (int second = 0;; second++) {
    if (second >= 60) fail("timeout");
    try { if (selenium.isTextPresent("Software testing")) break; } catch (Exception e) {}
    Thread.sleep(1000);
   }
    
  }
 
  @After
  public void tearDown() throws Exception {
   selenium.stop();
    
  }
}

TESTNG
  

import com.thoughtworks.selenium.*;
 
 
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
 
 
public class TestNGGoogleBase {
  public Selenium selenium;
  WebDriver driver;
   
  @DataProvider(name="parameter")
   public static Object[][] data() {
    return new Object[][]{{"testing"},{"Software testing"}};
   }
 
  @BeforeMethod
  public void setUp() throws Exception {
   driver= new FirefoxDriver();
   selenium = new WebDriverBackedSelenium(driver, "http://www.google.com"); 
   selenium.open("http://www.google.com");
  }
 
  @Test(dataProvider="parameter")
  public void testSearch(String testData) throws Exception {
   selenium.open("/");
   selenium.type("id=lst-ib", testData);
   selenium.click("//input[@value='Google Search']");
   for (int second = 0;; second++) {
    if (second >= 60) Assert.fail("timeout");
    try { if (selenium.isElementPresent("link=Software testing - Wikipedia, the free encyclopedia")) break; } catch (Exception e) {}
    Thread.sleep(1000);
   }
   selenium.click("link=Software testing - Wikipedia, the free encyclopedia");
   for (int second = 0;; second++) {
    if (second >= 60) Assert.fail("timeout");
    try { if (selenium.isTextPresent("Software testing")) break; } catch (Exception e) {}
    Thread.sleep(1000);
   }
  }
 
  @AfterMethod
  public void tearDown() throws Exception {
   selenium.stop();
  }