新建excel表 输入如下数据:
自动化 | 测试 | selenium |
蝙蝠侠 | 主演 | 迈克尔 |
超人 | 导演 | 圆谷英二 |
生化危机 | 编剧 | 安德森 |
文件->另存为->保存类型为CSV文件,保存至D盘下,用记事本编辑, 另存为TestData.csv, 编码格式采用UTF-8
实现源码;
新建TestByCVS .java文件:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
stng.annotations.Test;
stng.annotations.DataProvider;
stng.annotations.BeforeMethod;
stng.annotations.AfterMethod;
import urrent.TimeUnit;
import org.junit.Assert;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;public class TestByCVS {public static WebDriver driver;//定义当前方法中的返回对象作为测试脚本的测试数据集,命名为searchWords@DataProvider(name="TestData")public static Object[][] words() throws IOException{return getTestData("d:\TestData.csv");}@Test(dataProvider="TestData")public void testSearch(String words1, String words2, String result){driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);("");driver.findElement(By.id("kw")).sendKeys(words1+ " "+words2);driver.findElement(By.id("su")).click();try {Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}Assert.PageSource().contains(result)); }@BeforeMethodpublic void BeforeMethod(){driver =new FirefoxDriver();}@AfterMethodpublic void AfterMethod(){driver.quit();}public static Object[][] getTestData(String fileName) throws IOException{List <Object[]> records = new ArrayList<Object[]>();String record;BufferedReader file = new BufferedReader (new InputStreamReader(new FileInputStream(fileName),"UTF-8"));//忽略第一行adLine();//遍历 将内容存到records数组while((recordadLine())!=null){String fields[] = record.split(",");records.add(fields);}file.close();Object[][] results = new Object[records.size()][];for (int i=0; i<records.size();i++){results[i] = (i);}return results;}//stng.TestNGException: //The data provider is trying to pass 1 parameters but the method TestByCVS#testSearch takes 3 and TestNG is unable in inject a suitable object}
(PS :编码运行过程中 出现最后注释的TestNG不能识别对象问题 ,是我CSV文件的问题。TestNG的日志不支持中文,更改eclipse.ini的语句后,火狐浏览器依旧乱码,Google浏览器则正常)
运行结束后Results of running class TestByCVS会出现中文乱码,在eclipse.ini加入如下语句
-ding=utf-8
-ding=utf-8
-Duser.language=en_US
(Results of running class TestByCVS如下图所示)
(TestNG Report如下图所示:)
TestNG reports:
本文发布于:2024-02-02 06:01:20,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170682487941837.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |