selenium+testN数据驱动
来源:互联网 发布:知乎关键指标kpi 编辑:程序博客网 时间:2024/06/11 22:54
我用testng自带的数据驱动功能做参数化,发现这种方法也很方便,但是数据要写在代码中,不免有点不好维护,于是我继承FeedTest这个类,然后把数据写入到Excel,直接读Excel,发现脚本和数据分离,而且
也不需要用JAVA写读取Excel的代码,相当方便,两个代码如下:
用Testng自带的做参数化:
public class testSchoolRoom {
public WebDriver driver;
public String baseUrl = "http://wuhan.eduyun.cn";
public String LinkTest;
public String LinkTestPassWord;
public void startUrl() throws Exception {
driver = new FirefoxDriver();
driver.get(baseUrl);
// driver.manage().window().maximize();
}
@DataProvider(name = "testData")
public Object[][] testNetHomeWrok_data() {
return new Object[][] { { "300522624", "1234", "f","密码长度在6-20之间" },
{ "300522624", "123456789","f","密码输入有误" },
{ "300522624", "1234qwer", "t","王丽娟的工作空间" }};
}
@Test(dataProvider = "testData")
public void testTcShortPasswordLg(String userName, String passWord,String flag,
String excepted) throws Exception {
startUrl();
driver.findElement(By.id("info_username")).clear();
driver.findElement(By.id("info_username")).sendKeys(userName);
driver.findElement(By.id("info_password")).clear();
driver.findElement(By.id("info_password")).sendKeys(passWord);
driver.findElement(By.id("info_submit")).click();
if(flag == "t"){
LinkTest = driver.findElement(By.xpath("//li[contains(concat(' ', @class, ' '), ' jykj_blue ')]//strong")).getText().trim();
assertEquals(excepted, LinkTest);
LinkTest = null;
Thread.sleep(2000);
}else{
LinkTest = driver.findElement(By.xpath("//p[contains(concat(' ', @class, ' '), ' warnmsg ')] "))
.getText().trim();
assertEquals(excepted, LinkTest);
LinkTest = null;
Thread.sleep(2000);
}
driver.quit();
}
}
用读取Excel的方式 :
import org.testng.annotations.Test;
import org.testng.annotations.BeforeSuite;
import static org.testng.AssertJUnit.assertEquals;
import java.io.File;
import java.util.Vector;
import org.databene.benerator.anno.Source;
import org.databene.feed4testng.FeedTest;
import org.junit.AfterClass;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.opera.core.systems.scope.protos.ExecProtos.ActionList.Action;
public class testSchoolRoom extends FeedTest {
public WebDriver driver;
private String LinkTest;
public String baseUrl = "http://wuhan.eduyun.cn";
public void startUrl() throws Exception {
driver = new FirefoxDriver();
driver.get(baseUrl);
// driver.manage().window().maximize();
}
@Test(dataProvider = "feeder")
@Source("test.xls")
public void testTcShortPasswordLg(String userName, String passWord,boolean flag,
String excepted) throws Exception {
startUrl();
driver.findElement(By.id("info_username")).clear();
driver.findElement(By.id("info_username")).sendKeys(userName);
driver.findElement(By.id("info_password")).clear();
driver.findElement(By.id("info_password")).sendKeys(passWord);
driver.findElement(By.id("info_submit")).click();
if(flag){
LinkTest = driver.findElement(By.xpath("//li[contains(concat(' ', @class, ' '), ' jykj_blue ')]//strong")).getText().trim();
assertEquals(excepted, LinkTest);
LinkTest = null;
Thread.sleep(2000);
}else{
LinkTest = driver.findElement(By.xpath("//p[contains(concat(' ', @class, ' '), ' warnmsg ')] "))
.getText().trim();
assertEquals(excepted, LinkTest);
LinkTest = null;
Thread.sleep(2000);
}
driver.quit();
}
}
不过,下面一种 方式需要导入Feed4testng相关的包
也不需要用JAVA写读取Excel的代码,相当方便,两个代码如下:
用Testng自带的做参数化:
public class testSchoolRoom {
public WebDriver driver;
public String baseUrl = "http://wuhan.eduyun.cn";
public String LinkTest;
public String LinkTestPassWord;
public void startUrl() throws Exception {
driver = new FirefoxDriver();
driver.get(baseUrl);
// driver.manage().window().maximize();
}
@DataProvider(name = "testData")
public Object[][] testNetHomeWrok_data() {
return new Object[][] { { "300522624", "1234", "f","密码长度在6-20之间" },
{ "300522624", "123456789","f","密码输入有误" },
{ "300522624", "1234qwer", "t","王丽娟的工作空间" }};
}
@Test(dataProvider = "testData")
public void testTcShortPasswordLg(String userName, String passWord,String flag,
String excepted) throws Exception {
startUrl();
driver.findElement(By.id("info_username")).clear();
driver.findElement(By.id("info_username")).sendKeys(userName);
driver.findElement(By.id("info_password")).clear();
driver.findElement(By.id("info_password")).sendKeys(passWord);
driver.findElement(By.id("info_submit")).click();
if(flag == "t"){
LinkTest = driver.findElement(By.xpath("//li[contains(concat(' ', @class, ' '), ' jykj_blue ')]//strong")).getText().trim();
assertEquals(excepted, LinkTest);
LinkTest = null;
Thread.sleep(2000);
}else{
LinkTest = driver.findElement(By.xpath("//p[contains(concat(' ', @class, ' '), ' warnmsg ')] "))
.getText().trim();
assertEquals(excepted, LinkTest);
LinkTest = null;
Thread.sleep(2000);
}
driver.quit();
}
}
用读取Excel的方式 :
import org.testng.annotations.Test;
import org.testng.annotations.BeforeSuite;
import static org.testng.AssertJUnit.assertEquals;
import java.io.File;
import java.util.Vector;
import org.databene.benerator.anno.Source;
import org.databene.feed4testng.FeedTest;
import org.junit.AfterClass;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.opera.core.systems.scope.protos.ExecProtos.ActionList.Action;
public class testSchoolRoom extends FeedTest {
public WebDriver driver;
private String LinkTest;
public String baseUrl = "http://wuhan.eduyun.cn";
public void startUrl() throws Exception {
driver = new FirefoxDriver();
driver.get(baseUrl);
// driver.manage().window().maximize();
}
@Test(dataProvider = "feeder")
@Source("test.xls")
public void testTcShortPasswordLg(String userName, String passWord,boolean flag,
String excepted) throws Exception {
startUrl();
driver.findElement(By.id("info_username")).clear();
driver.findElement(By.id("info_username")).sendKeys(userName);
driver.findElement(By.id("info_password")).clear();
driver.findElement(By.id("info_password")).sendKeys(passWord);
driver.findElement(By.id("info_submit")).click();
if(flag){
LinkTest = driver.findElement(By.xpath("//li[contains(concat(' ', @class, ' '), ' jykj_blue ')]//strong")).getText().trim();
assertEquals(excepted, LinkTest);
LinkTest = null;
Thread.sleep(2000);
}else{
LinkTest = driver.findElement(By.xpath("//p[contains(concat(' ', @class, ' '), ' warnmsg ')] "))
.getText().trim();
assertEquals(excepted, LinkTest);
LinkTest = null;
Thread.sleep(2000);
}
driver.quit();
}
}
不过,下面一种 方式需要导入Feed4testng相关的包
0 0
- selenium+testN数据驱动
- selenium 数据驱动
- Selenium数据驱动之EXCEL
- Selenium数据驱动之EXCEL
- selenium+junit实现关键字数据驱动
- Selenium学习12--数据驱动csv
- selenium+python3-数据驱动测试示例
- selenium 数据驱动 (基于TestNG CSV)
- selenium webdriver 学习总结-数据驱动(六)
- selenium webdriver 学习总结-数据驱动(六)
- TestNG+Selenium Webdriver 数据(Excel)驱动的方法
- TestNG+Selenium Webdriver 数据(Excel)驱动的方法
- Selenium WebDriver使用Junit创建数据驱动测试
- TestNG+Selenium Webdriver 数据(Excel)驱动的方法
- selenium webdriver 学习总结-数据驱动(六)
- selenium-Java-使用csv文件进行数据驱动-中文乱码
- selenium webdriver学习--利用POI实现数据驱动
- selenium Excel数据驱动测试(基于 TestNG Apache POI)
- PHP Fatal error: Class 'DOMDocument'未安装php-xml模块
- C++如何在派生类成员函数内调用基类的成员函数(包括构造函数)
- C++内存对齐
- 手游接入Facebook功能
- 润乾数据源连接报错
- selenium+testN数据驱动
- 如何在Unity中显示当前游戏运行帧数?
- .net服务器发布应用程序COM类工厂出错
- 黄金数据查询演示示例
- 网络编程学习(七)
- 网络编程学习(八)
- Android Activity生命周期
- 百度地图 Android SDK - 检索功能使用的简单示例
- 黄金数据查询演示示例