软件测试技术第二次上机

Selenium上机实验说明

  1. 安装SeleniumIDE插件
  2. 学会使用SeleniumIDE录制脚本和导出脚本
  3. 访问(http://121.193.130.195:8080/)使用学号登陆系统,进入系统后能够看到该同窗的git地址。
  4. 编写Selenium Java WebDriver程序,测试inputgit.csv表格中的学号和git地址的对应关系是否正确

安装SeleniumIDE插件

首先咱们要下载一个火狐浏览器,亲测最新版本并不能支持完成本次实验。因此下载Firefox43版本,下载后打开菜单栏点击附加组件,找到并添加SeleniumIDE,重启浏览器后便可使用。css

使用SeleniumIDE录制脚本和导出脚本

录制脚本

在工具栏中找到咱们下载后的插件图标,点击:

点击红色按钮即开始录制,以实验说明中的学号登陆系统为例:

对页面元素点击右键,如图所示可找到对应的assertText,也可用到程序中找到相应的元素。
结束录制:
java

导出脚本

Optinons-->Options-->General,选中Enable experimental features。经过Options-->Format选择要导出成的相应格式。为了Java使用,选择Java/JUnit4/WebDriver。
获得java文件代码:git

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 FirefoxTest {
  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 testFirefox() throws Exception {
    driver.get(baseUrl + "/");
    driver.findElement(By.id("name")).clear();
    driver.findElement(By.id("name")).sendKeys("3014218072");
    driver.findElement(By.id("pwd")).clear();
    driver.findElement(By.id("pwd")).sendKeys("218072");
    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;
    }
  }
}

编写Selenium Java WebDriver程序

@Test
  public void testFirefox() throws IOException{
      CsvReader r = new CsvReader("F://大学//软件测试技术//inputgit.csv", ',',Charset.forName("utf-8"));
        r.readHeaders();
        while (r.readRecord()) {
            String id = r.get("id");
            String password = id.substring(4);
            String address = r.get("address");
            driver.get(baseUrl + "/");
           
            driver.findElement(By.id("name")).clear();
            driver.findElement(By.id("name")).sendKeys(id);
            driver.findElement(By.id("pwd")).clear();
            driver.findElement(By.id("pwd")).sendKeys(password);
            driver.findElement(By.id("submit")).sendKeys(Keys.ENTER);
            
            WebElement text = driver.findElement(By.cssSelector("#table-main tr:last-child td:last-child"));
            String address0 = text.getText();
            assertEquals(address,address0);
        }
        r.close();
      }

添加的包有:
github

具体代码地址

相关文章
相关标签/搜索