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

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

执行步骤:

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

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

  1. package com.m1905.java;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. import org.apache.poi.hssf.usermodel.HSSFSheet;
  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  12. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  13. /**
  14. * 读取excel文件中的测试数据,将数据分为用户名和密码两组
  15. * */
  16. public class ExcelWorkBook{
  17.  
  18. String username;
  19. String password;
  20. /**
  21. * 读取excel文件中的用户名列,即第一列
  22. * @throws IOException
  23. * */
  24. public List<String> readUsername(String filesrc) throws IOException{
  25. List<String> userList = new ArrayList<String>();
  26. //读取excel文件
  27. InputStream is = new FileInputStream(filesrc);
  28. POIFSFileSystem fs = new POIFSFileSystem(is);
  29. HSSFWorkbook wb = new HSSFWorkbook(fs);
  30. HSSFSheet sheet = wb.getSheetAt(0);
  31. if(sheet==null){
  32. System.out.println("暂无数据,请输入测试数据");
  33. }
  34. //获取文件行数
  35. int rows = sheet.getLastRowNum();
  36. //获取文件列数
  37. /*int cols = sheet.getRow(0).getPhysicalNumberOfCells();
  38. //获取第一行的数据,一般第一行为属性值,所以这里可以忽略
  39. String colValue1 = sheet.getRow(0).toString();
  40. String colValues2 = sheet.getRow(1).toString();*/
  41. //取出第一列的用户名,去除掉第一行中的标题
  42. for(int i =1;i<rows+1;i++){
  43. username = sheet.getRow(i).getCell(0).toString();
  44. System.out.println(username);
  45. userList.add(username);
  46. }
  47. System.out.println(userList);
  48. return userList;
  49. }
  50.  
  51. /**
  52. * 获取第二列,得到密码list
  53. * @throws IOException
  54. * */
  55. public List<String> readPassword(String filesrc) throws IOException{
  56. List<String> passwordList = new ArrayList<String>();
  57. //读取excel文件
  58. InputStream is = new FileInputStream(filesrc);
  59. POIFSFileSystem fs = new POIFSFileSystem(is);
  60. HSSFWorkbook wb = new HSSFWorkbook(fs);
  61. HSSFSheet sheet = wb.getSheetAt(0);
  62. if(sheet==null){
  63. System.out.println("暂无数据,请输入测试数据");
  64. }
  65. //取出第二列的密码值,去掉第一行中的标题
  66. int rows=sheet.getLastRowNum();
  67. for(int i=1;i<rows+1;i++){
  68. password = sheet.getRow(i).getCell(1).toString();
  69. System.out.println(password);
  70. passwordList.add(password);
  71. }
  72. System.out.println(passwordList);
  73. return passwordList;
  74. }
  75. }

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

  1. package com.m1905.junit;
  2.  
  3. import java.io.IOException;
  4. import java.util.List;
  5. import java.util.concurrent.TimeUnit;
  6.  
  7. import org.junit.AfterClass;
  8. import org.junit.BeforeClass;
  9. import org.junit.Test;
  10. import org.openqa.selenium.By;
  11. import org.openqa.selenium.WebDriver;
  12. import org.openqa.selenium.WebDriver.Navigation;
  13. import org.openqa.selenium.WebElement;
  14. import org.openqa.selenium.firefox.FirefoxDriver;
  15. import org.openqa.selenium.support.ui.ExpectedConditions;
  16. import org.openqa.selenium.support.ui.WebDriverWait;
  17.  
  18. import com.m1905.java.ExcelWorkBook;
  19.  
  20. public class LoginTest {
  21. private static WebDriver driver;
  22. private static Navigation navigate;
  23. private static String url="http://www.1905.com";
  24. private static String filesrc = "UserAndPassword.xls";
  25.  
  26. @BeforeClass
  27. public static void setUpBeforeClass() throws Exception {
  28. //加载浏览器
  29. driver = new FirefoxDriver();
  30. navigate = driver.navigate();
  31. navigate.to(url);
  32. driver.manage().window().maximize();
  33. }
  34.  
  35. @AfterClass
  36. public static void tearDownAfterClass() throws Exception {
  37. if(driver!=null){
  38. driver.close();
  39. driver.quit();
  40. }
  41. }
  42.  
  43. @Test
  44. public void test() throws IOException {
  45. //初始化ExcelWorkBook Class
  46. ExcelWorkBook excelBook = new ExcelWorkBook();
  47. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  48. //取出username放在userlist集合里面
  49. List<String> userList = excelBook.readUsername(filesrc);
  50. //取出password放在passwordList集合里面
  51. List<String> passwordList = excelBook.readPassword(filesrc);
  52.  
  53. //把取出来的数据输入到界面中的用户名和密码的输入框中
  54. int usersize = userList.size();
  55. for(int i=0;i<usersize;i++){
  56. driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  57. //点击登陆/注册按钮
  58. WebElement LogAndReg = driver.findElement(By.xpath(".//*[@id='site_nav_md']/ul/li[2]/a"));
  59. LogAndReg.click();
  60. //通过xpath定位到username输入框
  61. WebElement username = driver.findElement(By.xpath(".//*[@id='inputUsername']"));
  62. //通过xpath定位到password输入框
  63. WebElement password = driver.findElement(By.xpath(".//*[@id='inputPassword']"));
  64. //通过xpath定位到登陆按钮
  65. WebElement login = driver.findElement(By.xpath(".//*[@id='loginreg']/div/div[1]/form/p/button"));
  66. //清除username输入框中的内容
  67. username.clear();
  68. //把usernamelist中的数据取出来,写入
  69. String name = userList.get(i);
  70. username.sendKeys(name);
  71. //输入对应的password值
  72. for(int j=0;j<passwordList.size();j++){
  73. password.clear();
  74. String pass = passwordList.get(j);
  75. password.sendKeys(pass);
  76. }
  77. //点击登陆
  78. login.click();
  79. //driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  80. WebDriverWait wait = new WebDriverWait(driver,10);
  81. WebElement e1 = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='site_nav_md']/ul/li[3]/a[2]")));
  82. //找到退出登陆按钮
  83. e1.click();
  84. // WebElement logoutButton = driver.findElement(By.xpath(""));
  85. // logoutButton.click();
  86. }
  87. }
  88.  
  89. }

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. jQuery Mobile的使用方法

    安装:  <link rel="stylesheet" href="下载到本地的文件目录.min.css" media="screen" ...

  2. Solr: a custom Search RequestHandler

    As you know, I've been playing with Solr lately, trying to see how feasible it would be to customize ...

  3. myeclipse设置默认的jsp打开方式

  4. Android-ListView-CursorAdapter

    在上篇博客,Android-ListView-SimpleCursorAdapter,中介绍了SimpleCurosrAdapter的使用操作(SimpleCursorAdapter是简单便捷Curs ...

  5. centos7 安装git

    centos7下git的安装和配置   git的安装: yum 源仓库里的 Git 版本更新不及时,最新版本的 Git 是 1.8.3.1,但是官方最新版本已经到了 2.9.2.想要安装最新版本的的 ...

  6. python list的append()和extend()方法

    引用自:https://www.cnblogs.com/subic/p/6553187.html

  7. kali linux之免杀技术

    恶意软件: 病毒,木马.蠕虫,键盘记录,僵尸程序,流氓软件,勒索软件,广告程序 在用户非自愿的情况下安装 出于某种恶意的目的:控制,窃取,勒索,偷窥,推送,攻击 恶意程序最主要的防护手段:杀软 检测原 ...

  8. CSS3过渡效果 兼容IE6、IE7、IE8

    <style> .box{ width:120px;height:40px;background:yellowgreen;line-height:40px;transition:width ...

  9. php中签名公钥、私钥(SHA1withRSA签名)以及AES(AES/ECB/PKCS5Padding)加密解密详解

    由于http请求是无状态,所以我们不知道请求方到底是谁.于是就诞生了签名,接收方和请求方协商一种签名方式进行验证,来取得互相信任,进行下一步业务逻辑交流. 其中签名用得很多的就是公钥私钥,用私钥签名, ...

  10. 安装ubuntu server时候的多网卡问题

    安装的时候看到多个网卡,eth0,eth1,到系统中后只看见eth0 1.输入 ifconfig -a,这个时候如果能够看到多网卡,则在/etc/network/.interfaces中配置一下网卡就 ...