selenium 数据驱动 (基于TestNG CSV)

阅读: 评论:0

selenium 数据驱动 (基于TestNG CSV)

selenium 数据驱动 (基于TestNG CSV)

新建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((record&#adLine())!=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小时内删除。

标签:数据   selenium   CSV   TestNG
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23