(1)安裝 SeleniumIDE 插件java
(2)學會使用 SeleniumIDE 錄製腳本和導出腳本git
(3)訪問 http://121.193.130.195:8080/ 使用學號登陸系統(帳戶名爲學號,密碼爲學號後6位),進入系統後能夠看到該同窗的 git 地址。github
(4)編寫 Selenium Java WebDriver 程序,測試 inputgit.csv 表格中的學號和 git 地址的對應關係是否正確。瀏覽器
(1)安裝 FireFox 瀏覽器,版本爲 44.0。ide
(2)打開安裝的 FireFox 瀏覽器,並進入 Selenium IDE 的官網,下載最新版本的 Selenium IDE 插件並添加到 FireFox 的組件庫中。測試
(3)打開 Selenium IDE 插件,並開啓錄製。在瀏覽器中輸入地址 http://121.193.130.195:8080/ 並進行訪問,輸入本身的學號和對應的密碼後跳轉到包含我的信息的頁面。關閉錄製,點擊 「Options->Format->Java/Junit4/
WebDriver」 進行格式轉換。ui
(4)而後點擊 「文件->Export Test Case As->Java/Junit4->WebDriver」 將該測試用例導出,獲得Java示例代碼。this
(5)在idea中新建一個 lab_2 項目,目錄結構以下所示:idea
(6)下載 Java 版本的 selenium,並將對應的 jar 包導入到項目中去:firefox
(7)編寫 Selenium 測試代碼:
package com.selenium; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; import java.io.BufferedReader; import java.io.FileReader; import java.util.Arrays; import java.util.Collection; import java.util.concurrent.TimeUnit; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; @RunWith(Parameterized.class) public class SeleniumTest { private WebDriver driver; private String baseUrl; private boolean acceptNextAlert = true; private StringBuffer verificationErrors = new StringBuffer(); private String testName; private String testPwd; private String gitHubUrl; public SeleniumTest(String testName, String testPwd, String githubUrl) { this.testName = testName; this.testPwd = testPwd; this.gitHubUrl = githubUrl; } @Before public void setUp() throws Exception { driver = new FirefoxDriver(); baseUrl = "http://121.193.130.195:8080"; driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @Parameterized.Parameters public static Collection<Object[]> getData() { Object[][] obj = new Object[117][]; try { BufferedReader reader = new BufferedReader(new FileReader("G://文件夾//天大文件//課程//大三下//軟件測試技術//實驗//第2次實驗//inputgit.csv")); reader.readLine(); String line; int count = 0; while ((line = reader.readLine()) != null) { String item[] = line.split(","); String stuNum = item[0]; String pwd = stuNum.substring(4); String githubUrl = item[2]; obj[count] = new Object[]{stuNum, pwd, githubUrl}; count++; } } catch (Exception e) { e.printStackTrace(); } return Arrays.asList(obj); } @Test public void testMain() throws Exception { driver.get(baseUrl + "/"); driver.findElement(By.id("name")).clear(); driver.findElement(By.id("name")).sendKeys(testName); driver.findElement(By.id("pwd")).clear(); driver.findElement(By.id("pwd")).sendKeys(testPwd); driver.findElement(By.id("submit")).click(); assertEquals(this.gitHubUrl, driver.findElement(By.xpath("//tbody[@id='table-main']/tr[3]/td[2]")).getText()); } @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; } } }
117個測試用例所有經過: