使用到的技术: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();
}
} }

最新文章

  1. vmware rdm
  2. Spring-编程式事物
  3. JDE报表开发笔记(Client端导出Excel乱码)
  4. php string转换为int
  5. stringlist
  6. Chp10: Scalability and Memory Limits
  7. 小白日记35:kali渗透测试之Web渗透-手动漏洞挖掘(一)-默认安装引发的漏洞
  8. Linux删除文件Argument list too long问题的解决方案
  9. storm简介[ZZ]
  10. OAuth2.0基本流程
  11. latex命令替换之\newcommand
  12. 【转】Java线程与Linux内核线程的映射关系
  13. Java CAS 原理分析
  14. SQL反模式学习笔记1 开篇
  15. hdu 1596 乘积的最大值
  16. Big Spatio temporal Data(R-tree Index and NN &amp; RNN &amp; Skyline)
  17. Eclipse中jar包关联JDK源代码
  18. IDEA.环境
  19. 1.4(java学习笔记) 面向对象内存分析
  20. 基于matlab的边缘提取方法的比较

热门文章

  1. 【Linux】GDB调试工具
  2. Mac OS X Yosemite &amp; Arduino安装CH340 USB转串口驱动
  3. duilib入门简明教程 -- XML配置界面(6)
  4. vs附加调试
  5. sh脚本
  6. [HAOI2010]计数(组合数学)(数位DP)
  7. 1.IPtable基础命令总结
  8. JS延时器 定时器 暂停器 中断器
  9. ArchLinux下十八岁的感悟
  10. css中设置background属性