testng和junit搭配selenium

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();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值