使用到的技术:POI对excel的解析、selenium自动化测试、junit

测试用例:登陆www.1905.com执行登陆-退出的操作

执行步骤:

1、首先创建一个excel,里面有用户名和密码列

2、新建 一个解析excel的java类

package com.m1905.java;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* 读取excel文件中的测试数据,将数据分为用户名和密码两组
* */
public class ExcelWorkBook{ String username;
String password;
/**
* 读取excel文件中的用户名列,即第一列
* @throws IOException
* */
public List<String> readUsername(String filesrc) throws IOException{
List<String> userList = new ArrayList<String>();
//读取excel文件
InputStream is = new FileInputStream(filesrc);
POIFSFileSystem fs = new POIFSFileSystem(is);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
if(sheet==null){
System.out.println("暂无数据,请输入测试数据");
}
//获取文件行数
int rows = sheet.getLastRowNum();
//获取文件列数
/*int cols = sheet.getRow(0).getPhysicalNumberOfCells();
//获取第一行的数据,一般第一行为属性值,所以这里可以忽略
String colValue1 = sheet.getRow(0).toString();
String colValues2 = sheet.getRow(1).toString();*/
//取出第一列的用户名,去除掉第一行中的标题
for(int i =1;i<rows+1;i++){
username = sheet.getRow(i).getCell(0).toString();
System.out.println(username);
userList.add(username);
}
System.out.println(userList);
return userList;
} /**
* 获取第二列,得到密码list
* @throws IOException
* */
public List<String> readPassword(String filesrc) throws IOException{
List<String> passwordList = new ArrayList<String>();
//读取excel文件
InputStream is = new FileInputStream(filesrc);
POIFSFileSystem fs = new POIFSFileSystem(is);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
if(sheet==null){
System.out.println("暂无数据,请输入测试数据");
}
//取出第二列的密码值,去掉第一行中的标题
int rows=sheet.getLastRowNum();
for(int i=1;i<rows+1;i++){
password = sheet.getRow(i).getCell(1).toString();
System.out.println(password);
passwordList.add(password);
}
System.out.println(passwordList);
return passwordList;
}
}

3、创建一个junit测试类,对登陆过程进行测试

package com.m1905.junit;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit; import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait; import com.m1905.java.ExcelWorkBook; public class LoginTest {
private static WebDriver driver;
private static Navigation navigate;
private static String url="http://www.1905.com";
private static String filesrc = "UserAndPassword.xls"; @BeforeClass
public static void setUpBeforeClass() throws Exception {
//加载浏览器
driver = new FirefoxDriver();
navigate = driver.navigate();
navigate.to(url);
driver.manage().window().maximize();
} @AfterClass
public static void tearDownAfterClass() throws Exception {
if(driver!=null){
driver.close();
driver.quit();
}
} @Test
public void test() throws IOException {
//初始化ExcelWorkBook Class
ExcelWorkBook excelBook = new ExcelWorkBook();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//取出username放在userlist集合里面
List<String> userList = excelBook.readUsername(filesrc);
//取出password放在passwordList集合里面
List<String> passwordList = excelBook.readPassword(filesrc); //把取出来的数据输入到界面中的用户名和密码的输入框中
int usersize = userList.size();
for(int i=0;i<usersize;i++){
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//点击登陆/注册按钮
WebElement LogAndReg = driver.findElement(By.xpath(".//*[@id='site_nav_md']/ul/li[2]/a"));
LogAndReg.click();
//通过xpath定位到username输入框
WebElement username = driver.findElement(By.xpath(".//*[@id='inputUsername']"));
//通过xpath定位到password输入框
WebElement password = driver.findElement(By.xpath(".//*[@id='inputPassword']"));
//通过xpath定位到登陆按钮
WebElement login = driver.findElement(By.xpath(".//*[@id='loginreg']/div/div[1]/form/p/button"));
//清除username输入框中的内容
username.clear();
//把usernamelist中的数据取出来,写入
String name = userList.get(i);
username.sendKeys(name);
//输入对应的password值
for(int j=0;j<passwordList.size();j++){
password.clear();
String pass = passwordList.get(j);
password.sendKeys(pass);
}
//点击登陆
login.click();
//driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver,10);
WebElement e1 = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='site_nav_md']/ul/li[3]/a[2]")));
//找到退出登陆按钮
e1.click();
// WebElement logoutButton = driver.findElement(By.xpath(""));
// logoutButton.click();
}
} }

Selenium+excel实现参数化自动化测试的更多相关文章

  1. selenium+junit4实现参数化自动化测试

    业务场景:在www.1905.com电影网中实现两个用户的登陆操作. 代码如下: package com.m1905.junit; import java.util.Arrays; import ja ...

  2. Selenium之利用Excel实现参数化

    Selenium之利用Excel实现参数化 说明:我是通过Workbook方式来读取excel文件的,这次以登陆界面为例 备注:使用Workbook读取excel文件,前提是excel需要2003版本 ...

  3. Selenium 2.0 WebDriver 自动化测试 使用教程 实例教程 API快速参考

    Selenium 2.0 WebDriver 自动化测试 使用教程 实例教程 API快速参考 //System.setProperty("webdriver.firefox.bin" ...

  4. 基于Selenium+Python的web自动化测试框架

    一.什么是Selenium? Selenium是一个基于浏览器的自动化测试工具,它提供了一种跨平台.跨浏览器的端到端的web自动化解决方案.Selenium主要包括三部分:Selenium IDE.S ...

  5. 爬虫(五)—— selenium模块启动浏览器自动化测试

    目录 selenium模块 一.selenium介绍 二.环境搭建 三.使用selenium模块 1.使用chrome并设置为无GUI模式 2.使用chrome有GUI模式 3.查找元素 4.获取标签 ...

  6. Selenium(Webdriver)自动化测试常问问题

    http://blog.sina.com.cn/s/blog_c189e2590102w3bv.html Selenium(Webdriver)自动化测试常问问题 (1)selenium中如何保证操作 ...

  7. 自动化测试基础篇--Selenium中数据参数化之TXT

    摘自https://www.cnblogs.com/sanzangTst/p/7722594.html 一.搜索参数化 在TXT文件中保存需要搜索的内容: 测试代码: 1 #!/usr/bin/env ...

  8. selenium利用Excel进行参数化(简单示例)

    上篇搭建好环境后,正真开始我的自动化之旅了.... 开始之前特别说明一下testNG的版本,不能直接使用Eclipse直接线上下载的版本,线上版本太高,不能兼容,运行程序会报以下错误,需要自行下载低一 ...

  9. 基于excel的接口自动化测试框架:支持参数化、关联等

    1. 框架结构说明 2. 框架代码实现 action 包  case_action.py business_process 包 main_process.py util 包 global_var.py ...

随机推荐

  1. Apache Shiro 10分钟之旅!

    欢迎来到Apache Shiro 10分钟之旅! 希望通过这个简单.快速的示例,可以让你对应用程序中使用Shiro有个深入的了解.嗯,10分钟你应该可以搞定它. 概述 Apache Shiro是什么? ...

  2. 关于提交表单时添加自定义值的方式:data中值可绑定function

    表单提交时新增自定义值: $.ajaxForm(){ data:{aaa:"12"} } 但是这个是在初始化的时候就绑定进去的,所以值是固定的初始化时候的值,若想添加动态值,可以这 ...

  3. python的requests库详解

    快速上手 迫不及待了吗?本页内容为如何入门 Requests 提供了很好的指引.其假设你已经安装了 Requests.如果还没有,去安装一节看看吧. 首先,确认一下: Requests 已安装 Req ...

  4. SVN检出忽略文件夹文件

    具体实现:1.在解决方案目录上点右键2.在乌龟SVN菜单中找到"属性"点开 3.在弹出窗中点  新建--其他 4.在弹出窗中的"属性"中选择"svn: ...

  5. 微软 eshop 数据存储之sqlserver

    微软的eshop项目写的很牛,学起来也比较吃力,最近公司刚好有一本书,说的就是.NET微服务,记下来. 因为微软对性能的要求,docker里面要有内存要求 安装dokcer,拉镜象 : docker ...

  6. 工作流2013 assign to问题

    根据您的确认, 该问题已经通过我们所提供的方案进行修改后测试通过, 问题解决. 以下为该问题的产生原因: SharePoint 2013使用的默认认证机制与2007不一样,  2007使用的是Wind ...

  7. [agc007f] Shik and Copying String 模拟神题

    Description ​ "全"在十分愉快打工,第0天,给了他一个仅有小写字母构成的长度为N的字符串S0,在之后的第i天里,"全"的工作是将Si−1复制一份到 ...

  8. CodeChefSeries Sum (伯努利数+生成函数+FFT)

    题面 传送门 给定\(a_1,..,a_n\),定义\(f(x,k)=\sum_{i=1}^n(x+a_i)^k,g(t,k)=\sum_{x=0}^tf(x,k)\),给定\(T,K\),请你对\( ...

  9. Linux下的find查找命令

    Linux下的查找命令有:which(查看可执行文件的位置),whereis(查看指定文件的位置),locate(配合数据库查找指定文件的内容).find(在磁盘上查找文件).grep(使用正则表达式 ...

  10. JavaScript Debug 之 Console

    简评:只知道 console.log ?是时候提升一下对 console 的认知了. JavaScript console 是现代浏览器的一种内置功能,它允许开发者: 查看网页上的错误和警告日志. 使 ...