zoukankan      html  css  js  c++  java
  • [ST2017] Lab2: Test with SeleniumIDE

    1. 安装 Selenium IDE 插件

    从FireFox 直接安装组件 Selenium IDE: 菜单 -> 附加组件 -> 获取附加组件 -> 查看更多附加组件! -> 搜索并选择 Selenium IDE

    安装并重启.

    2. 使用 Selenium IDE 录制脚本 和 导出脚本

    菜单 -> 定制 -> 显示/隐藏工具栏 -> 菜单栏

    菜单栏: 工具 -> Selenium IDE

    Base Url: 录制的开始连接地址

    Test Case: 测试用例

    右上角的红色圆,空心的时候表示的正在录制,实心表示录制结束

    * 速度控制: 对于变化快的网页可以控制其运行速度.

    录制脚本 和 导出脚本 见 3.

    3. 访问[软件测试课程登陆网址]使用学号登录系统

    (账户名为学号,密码为学号后6位),进入系统后可以看到该同学的git地址。

    打开 Selenium IDE (录制) -> 进行登陆操作 -> 按下录制键停止录制

    点击运行按钮即可进行自动测试

    导出脚本: IDE菜单栏.文件 -> Export Test Case As... -> Java / JUnit4 / WebDriver -> 选择Java文件

    保存的代码如下:

    package com.example.tests;
    
    import java.util.regex.Pattern;
    import java.util.concurrent.TimeUnit;
    import org.junit.*;
    import static org.junit.Assert.*;
    import static org.hamcrest.CoreMatchers.*;
    import org.openqa.selenium.*;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.Select;
    
    public class IsGitTest {
      private WebDriver driver;
      private String baseUrl;
      private boolean acceptNextAlert = true;
      private StringBuffer verificationErrors = new StringBuffer();
    
      @Before
      public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "http://121.193.130.195:8080/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
      }
    
      @Test
      public void testIsGit() throws Exception {
        driver.get(baseUrl + "/");
        driver.findElement(By.id("name")).clear();
        driver.findElement(By.id("name")).sendKeys("3014218071");
        driver.findElement(By.id("pwd")).clear();
        driver.findElement(By.id("pwd")).sendKeys("218071");
        driver.findElement(By.id("submit")).click();
      }
    
      @After
      public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
          fail(verificationErrorString);
        }
      }
    
      private boolean isElementPresent(By by) {
        try {
          driver.findElement(by);
          return true;
        } catch (NoSuchElementException e) {
          return false;
        }
      }
    
      private boolean isAlertPresent() {
        try {
          driver.switchTo().alert();
          return true;
        } catch (NoAlertPresentException e) {
          return false;
        }
      }
    
      private String closeAlertAndGetItsText() {
        try {
          Alert alert = driver.switchTo().alert();
          String alertText = alert.getText();
          if (acceptNextAlert) {
            alert.accept();
          } else {
            alert.dismiss();
          }
          return alertText;
        } finally {
          acceptNextAlert = true;
        }
      }
    }

    4. 编写 Selenium Java WebDriver 程序 测试 inputgit.csv 表格中的 学号 和 git 地址的对应关系是否正确

    建李一个Java项目

    下载 selenium java, 并将jar包导入到项目中。

    (2)     编写代码

    首先导入的javacsv.jar来使用CsvReader。导入方法与(1)中相同。然后使用CsvReader来读取数据,并用WebDriver来登陆网站获取网上数据。最后比较两者看是否相同。

    代码如下:

    package isGit;

    import java.io.IOException;
    import java.nio.charset.Charset;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.*;
    import com.csvreader.CsvReader;

    public class isGit {
    public static void main(String[] args) throws IOException {
    CsvReader r = new CsvReader("D://inputgit.csv", ',', Charset.forName("GBK"));
    r.readHeaders();
    r.readRecord();
    String number_csv = r.get("学号");
    String name_csv = r.get("姓名");
    String address_csv = r.get("github地址");
    String pwd_csv = number_csv.substring(number_csv.length()-6,number_csv.length());

    System.setProperty("webdriver.firefox.bin", "D:/火狐浏览器/firefox.exe");
    WebDriver driver = new FirefoxDriver();
    driver.get("http://121.193.130.195:8080/");
    driver.manage().window().maximize();

    WebElement input_name = driver.findElement(By.id("name"));
    input_name.clear();
    input_name.sendKeys(number_csv);

    WebElement input_pwd = driver.findElement(By.id("pwd"));
    input_pwd.clear();
    input_pwd.sendKeys(pwd_csv);

    WebElement btn = driver.findElement(By.id("submit"));
    btn.click();

    String info_web = driver.findElement(By.xpath("//tbody[@id='table-main']")).getText();
    String name_web = info_web.substring(info_web.indexOf("名") + 2, info_web.indexOf("学") - 1);
    String number_web = info_web.substring(info_web.indexOf("号") + 2, info_web.indexOf("G") - 1);
    String address_web = info_web.substring(info_web.indexOf("址") + 2);

    if(name_csv.equals(name_web)&&number_csv.equals(number_web)&&address_csv.equals(address_web))
    {
    System.out.println("coincidence ^w^");
    }
    else
    {
    System.out.println(name_web+"has an Error!");
    }
    driver.close();
    }
    }

    5. 测试代码提交至github

     https://github.com/E-C-Ares/ST2017-Lab2

  • 相关阅读:
    Java之Map遍历方式性能分析:ketSet与entrySet
    Java之null保留字
    Java之&0xff用法解析以及原码、反码、补码相关知识
    Android之使用apt编写编译时注解
    Android之ViewPager.PageTransformer
    Android Studio利用javac导出Api文档
    06_Java多线程、线程间通信
    05_Java异常(Exception)
    04_Java面向对象特征之继承与多态
    03_Java面向对象特征: 封装性
  • 原文地址:https://www.cnblogs.com/cragoncanth/p/6623275.html
Copyright © 2011-2022 走看看