軟件測試技術第二次上機

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

具體代碼地址

相關文章
相關標籤/搜索