使用selenium+Poi实现读取excel文档数据的自动测试
最近在做软件测试的实验,学习selenium的使用,发现最新的4.00+版本的selenium教程比较少,在这里码一下分享给大家,也希望大佬们可以指正。
1.安装firefox
这一步就不多说了,网站在这: https://www.firefox.com.cn/
2.安装seleniumIDE
- 打开firefox,打开右上角工具栏,选择附加组件(快捷键:ctrl + shift + A);
- 右上角寻找更多扩展内搜索:seleniumIDE(图片内这个就是,然后安装即可)
3.selenium的录制和导出
-
打开firefox,右上方会有一个selenium组件的按钮,点击即可打开selenium(图内右数第二个)
-
之后选择create a new object,创建自己的项目就可以了
-
点击右上角的红色的rec 就可以开始录制,录制之前,会需要你输入测试的网址,在这里我输入https://www.baidu.com,然后点start recording,会进入该网站。接下来你所有的操作都会被记录在selenium内,并用于自动测试
-
测试完毕后,右键你的项目,选择export,导出为一个文件即可。我选的是java Junit
4.使用Poi读取excel内数据
在这里我是用的是Junit4.12 和 Poi 4.1.2,需要提前建好eclipse项目,配置好Junit,在这里就不多说了。
- 关于依赖:我选择的是导入jar包,也可以选择maven项目导入依赖。依赖如下:Poi :https://poi.apache.org/
xmlbeans:http://xmlbeans.apache.org/
关于如何添加jar包到library:在项目下建一个文件夹,把需要的jar包扔进去。右键项目->build path->add libraries->userlibrary->new(自己新建个library)->add external libraries - 这次处理的是.xlsx文件,代码如下:
-
package test; import java.io.File; import java.io.FileInputStream; import java.util.HashMap; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;; public class readExcel { public HashMap<String,String> getList() { HashMap<String,String> list = new HashMap();//文件数据只有两列,这里选择以HashMap的形式存储 try{ // 指定excel的路径 File src = new File(".\\SeleniumLab2020.xlsx"); // 加载文件 FileInputStream fis = new FileInputStream(src); // 加载workbook,XSSF是处理xlsx,xls格式则是hssf XSSFWorkbook wb=new XSSFWorkbook(fis); Sheet sh1= wb.getSheetAt(0); //遍历每行的数据 for(int i=0;i<20;i++) { //将每行两个数据存入Map中 list.put(sh1.getRow(i).getCell(1).getStringCellValue(), sh1.getRow(i).getCell(2).getStringCellValue()); } wb.close(); } catch (Exception e){ System.out.println(e.getMessage()); } return list; } }
`
5.实现selenium自动测试
- 注意!4.12版本,junit不包括hamcrest,这个需要自己导入library.
hamcrest : http://hamcrest.org/
package test;
// Generated by Selenium IDE
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import static org.junit.Assert.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Keys;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.net.MalformedURLException;
import java.net.URL;
public class TestTest {
private WebDriver driver;
private Map<String, Object> vars;
JavascriptExecutor js;
@Before
public void setUp() {
driver = new FirefoxDriver();//启动firefoxDriver
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@After
public void tearDown() {
driver.quit();
}
@Test
public void test() {
driver.get("http://xxxxxxxx");//所需要测试的网址
driver.manage().window().setSize(new Dimension(1550, 838));//自动把网页最大化
readExcel readExcel = new readExcel();
HashMap list = readExcel.getList();
Iterator it = list.entrySet().iterator();
String key =null;
String value = null;
//遍历excel提取出来的数据并填入框内自动测试
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
key = (String) entry.getKey();
value = (String) entry.getValue();
System.out.println("key:" + key + "---" + "value:" + value);
driver.findElement(By.name("user_number")).click();//模拟点击
driver.findElement(By.name("user_number")).sendKeys(key);//模拟输入
driver.findElement(By.cssSelector(".col-lg-6")).click();
driver.findElement(By.name("password")).click();模拟点击
driver.findElement(By.name("password")).sendKeys(value);//模拟输入
driver.findElement(By.cssSelector(".btn")).click();
}
}
}
6.运行代码即可
注:运行时需要 run as junit Test