1. 程式人生 > >Software Testing Lab2

Software Testing Lab2

我們 mode erro ror switchto 自己 AR rtp 技術分享

實驗內容:

1、安裝SeleniumIDE插件

2、學會使用SeleniumIDE錄制腳本和導出腳本

3、訪問https://psych.liebes.top/st使用學號登錄系統(賬戶名為學號,密碼為學號後6位),進入系統後可以看到該同學的git地址。

4、編寫Selenium Java WebDriver程序,測試input.xlsx表格中的學號和git地址的對應關系是否正確。

5、將測試代碼提交到github上(4月15日23:59:59前)。

實驗過程:

(1) 由於本機FireFox版本過新,故安裝 FireFox 瀏覽器,版本為 42.0。並且關閉FireFox的自動更新。

(2) 安裝Selenium IDE 插件

技術分享圖片

(3) 出現技術分享圖片,表明安裝成功

Selenium IDE界面如圖

技術分享圖片

(4) 打開 Selenium IDE 插件,並開啟錄制。在瀏覽器中輸入地址 https://psych.liebes.top/st 並進行訪問,輸入自己的學號和對應的密碼後跳轉到包含個人信息的頁面,關閉錄制

技術分享圖片

技術分享圖片

技術分享圖片

(5) 點擊 “文件->Export Test Case As->Java/Junit4->WebDriver”導出腳本

技術分享圖片

(6) 編寫Selenium Java WebDriver程序,測試input.xlsx表格中的學號和git地址的對應關系是否正確。

用ideaIDE新建java項目,將Selenium.java 復制到src目錄中。添加相關jar包

代碼如下:

package cn.tju.selenium;

import static org.junit.Assert.assertEquals;

import static org.junit.Assert.fail;

import java.io.File;

import java.io.FileInputStream;

import java.util.Arrays;

import java.util.Collection;

import java.util.NoSuchElementException;

import java.util.concurrent.TimeUnit;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

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.Alert;

import org.openqa.selenium.By;

import org.openqa.selenium.NoAlertPresentException;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

@RunWith(Parameterized.class)

public class Script {

private WebDriver driver;

private String baseUrl;

private boolean acceptNextAlert = true;

private StringBuffer verificationErrors = new StringBuffer();

private String testName;

private String testPwd;

private String gitHubUrl;

private Script sc;

public Script(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 = "https://psych.liebes.top";

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

}

@Parameterized.Parameters

public static Collection<Object[]> getData() {

Object[][] obj = new Object[97][];

try {

// 指定excel的路徑

File src = new File("input.xlsx");

// 加載文件

FileInputStream fis = new FileInputStream(src);

// 加載workbook

@SuppressWarnings("resource")

XSSFWorkbook wb = new XSSFWorkbook(fis);

// 加載sheet,這裏我們只有一個sheet,默認是sheet1

XSSFSheet sh1 = wb.getSheetAt(0);

for (int i = 0; i < sh1.getPhysicalNumberOfRows(); i++) {

obj[i] = new Object[] {

sh1.getRow(i).getCell(0).getStringCellValue(),

sh1.getRow(i).getCell(0).getStringCellValue()

.substring(4),

sh1.getRow(i).getCell(1).getStringCellValue() };

}

} catch (Exception e) {

e.printStackTrace();

}

return Arrays.asList(obj);

}

@Test

public void testUntitledTestCase() throws Exception {

driver.get(baseUrl + "/st");

driver.findElement(By.id("username")).clear();

driver.findElement(By.id("username")).sendKeys(this.testName);

driver.findElement(By.id("password")).clear();

driver.findElement(By.id("password")).sendKeys(this.testPwd);

driver.findElement(By.id("submitButton")).click();

assertEquals(this.gitHubUrl.trim(),

driver.findElement(By.cssSelector("p.login-box-msg")).getText()

.trim());

}

@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;

}

}

}

運行結果:

技術分享圖片

技術分享圖片

Software Testing Lab2