zoukankan      html  css  js  c++  java
  • [软件测试_LAB2]使用Selenium进行自动化测试

    一、使用Selenium进行脚本录制和导出

    1.打开Selenium并确认开启录制

    2.在Firefox浏览器地址栏输入www.ncfxy.com,进入网址,并输入学号和密码(密码为学号后6位)登录,登录后跳转到学号和邮箱显示的页面,以上过程为一次测试过程,完成后在SeleniumIDE中点击停止录制,完成一次录制过程

    3.选择文件-Export Test Case As-Java/JUnit 4/WebDriver进行导出

    4.查看导出的java文件

    二、编写代码进行测试

    1.导入WebDriver和JUnit的jar包,本次实验使用maven进行管理,使用以下代码添加依赖

     1 <dependency>
     2     <groupId>junit</groupId>
     3     <artifactId>junit</artifactId>
     4     <version>4.11</version>
     5 </dependency>
     6 
     7 <!--Selenium WebDriver for Java-->
     8 <dependency>
     9     <groupId>org.seleniumhq.selenium</groupId>
    10     <artifactId>selenium-java</artifactId>
    11     <version>2.53.0</version>
    12 </dependency>

    2.将之前生成的test.java拷贝进IDE,对其setUp()和test()函数进行改写

     1 @Before
     2     public void setUp() throws Exception {
     3         driver = new FirefoxDriver();
     4         baseUrl = "http://www.ncfxy.com";
     5         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
     6         System.out.println(CSVUtil.class.getClassLoader().getResource("info.csv").getPath());
     7         dataList = CSVUtil.readCSV(CSVUtil.class.getClassLoader().getResource("info.csv").getPath());
     8     }
     9 
    10     @Test
    11     public void test() throws Exception {
    12         for (String[] data : dataList) {
    13             driver.get(baseUrl + "/");
    14             driver.findElement(By.id("name")).clear();
    15             driver.findElement(By.id("name")).sendKeys(data[0]);
    16             driver.findElement(By.id("pwd")).clear();
    17             driver.findElement(By.id("pwd")).sendKeys(data[0].substring(4));
    18             new Select(driver.findElement(By.id("gender"))).selectByVisibleText("女");
    19             driver.findElement(By.id("submit")).click();
    20             WebElement tbody = driver.findElement(By.id("table-main"));
    21             List<WebElement> tds = tbody.findElements(By.tagName("td"));
    22             assertEquals(tds.get(1).getText(),data[1]);
    23         }
    24     }

    该段代码实现读入info.csv文件中的信息,然后通过学号自动登录,并且将info.csv中的信息和网页显示的信息进行对比,检查是否一致

    3.运行查看结果

    通过测试,即学号和邮箱全部对应

  • 相关阅读:
    爬虫中动态的POST参数
    Sublime3注册码和安装中文包
    [pytorch][进阶之路]pytorch学习笔记二-自动求导和module
    [pytorch][进阶之路]pytorch学习笔记一
    [python][进阶之路]理解python中的深复制和浅复制
    [python][matlab]在python36上安装matlab2015b引擎
    [python][进阶之路]list复制引发的问题
    [python][pandas]DataFrame的基本操作
    [Algorithm]ADMM简明理解
    [python][cpp]对浮点数进行n位翻转
  • 原文地址:https://www.cnblogs.com/gaomengya/p/5394620.html
Copyright © 2011-2022 走看看