软件测试:Lab 2 Selenium
Lab 2 Selenium:
一 、安裝SeleniumIDE插件
? ? ?1.下載火狐V42.0(下載地址:http://ftp.mozilla.org/pub/firefox/releases/42.0/win64/zh-CN/ )。
? ? ?2.打開火狐瀏覽器,百度打開??https://addons.mozilla.org/zh-CN/firefox/addon/selenium-ide/versions/ ,選擇Selenium IDE v2.9.1 ,在彈出窗口中選擇安裝-->立即重啟即可。
?
?二 、學會使用SeleniumIDE錄制腳本和導出腳本
1.打開Firefox,按Ctl+ Alt + S打開SeleniumIDE,或者直接點擊firework菜單欄--->selenium圖標。
2.確保SeleniumIDE處于錄制狀態
3.用firework訪問https://psych.liebes.top/st,使用學號登錄系統(賬戶名為學號,密碼為學號后6位),進入系統后可以看到你的git地址,再次點擊紅色按鈕完成錄制,并保存測試用例(文件—>Save?Test?Case) 。
4.導出為Java代碼。
文件—>Export—>Java/ JUnit 4 /WebDriver
代碼如下:
accessWebsite.java
package com.example.tests;import java.util.regex.Pattern;public class AccessWebsite {private WebDriver driver;private String baseUrl;private boolean acceptNextAlert = true;private StringBuffer verificationErrors = new StringBuffer();@Beforepublic void setUp() throws Exception {driver = new FirefoxDriver();baseUrl = "https://psych.liebes.top/";driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);}@Testpublic void testAccessWebsite() throws Exception {driver.get(baseUrl + "/st");driver.findElement(By.id("username")).clear();driver.findElement(By.id("username")).sendKeys("3015218070");driver.findElement(By.id("password")).clear();driver.findElement(By.id("password")).sendKeys("218070");driver.findElement(By.id("submitButton")).click();driver.findElement(By.id("submitButton")).click();driver.findElement(By.cssSelector("p.login-box-msg")).click();}@Afterpublic 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程序,測試input.xlsx表格中的學號和git地址的對應關系是否正確。
1.下載Selenium Java 2.53.1(下載地址:http://selenium-release.storage.googleapis.com/index.html?path=2.53/?)。
2.?打開eclipse,新建項目lab2,導入selenium-java-2.53.1.jar,selenium-java-2.53.1-srcs.jar?和?libs文件夾里的所有jar包。
項目目錄右鍵-->Build Path--> config build path-->Java Build Path-->Libraries-->Add?External JARs-->OK。
3.?下載jxl-2.6.10.jar(下載地址:http://maven.ibiblio.org/maven2/net/sourceforge/jexcelapi/jxl/ ),用于讀取input.xlsx(excel文件),并導入項目lab2,同時導入hamcrest-all-1.3.jar(下載地址見上一篇博客),導入方法與步驟2一樣。
4.?編寫代碼。
注意點:(1)excel表格中有的github地址后面帶有"/",建議都去掉"/"再比較
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?if(info.endsWith("/")) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?info = info.substring(0, info.length()-1);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(address.endsWith("/")) {? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? address = address.substring(0, address.length()-1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ?(2)從excel表格中獲取的github地址以及從頁面獲得的地址,都用trim()去掉首尾空格后再比較是否一致。
代碼如下:
?Selenium.java?
package selenium;import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.concurrent.TimeUnit; import org.junit.*; import static org.junit.Assert.*; import org.openqa.selenium.*;import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException;public class Selenium {private WebDriver driver;private String baseUrl;private boolean acceptNextAlert = true;private StringBuffer verificationErrors = new StringBuffer();String info=new String();String address=new String();@Beforepublic void setUp() throws Exception {System.setProperty("webdriver.chrome.driver","E:/大三下/軟件測試/chromedriver.exe"); // 此處PATH替換為你的chromedriver所在路徑driver = new ChromeDriver();baseUrl = "https://psych.liebes.top/";driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);}@Testpublic void test1() throws Exception { //讀取excel文件File file = new File("E:/大三下/軟件測試/input.xls"); try { // 創建輸入流,讀取Excel InputStream is = new FileInputStream(file.getAbsolutePath()); // jxl提供的Workbook類 Workbook wb = Workbook.getWorkbook(is);// 為頁簽sheet 1創建一個Sheet對象 Sheet sheet = wb.getSheet(0);for (int i = 0; i < sheet.getRows(); i++) { //按列名讀取這條記錄的值String number = sheet.getCell(0, i).getContents(); //讀取學號String pwd = number.substring(number.length()-6,number.length()); //讀取密碼address = sheet.getCell(1, i).getContents().trim(); //讀取github地址,記得用trim去掉首尾空格//訪問給定網址driver.get(baseUrl + "/st");//輸入用戶名driver.findElement(By.id("username")).clear();driver.findElement(By.id("username")).sendKeys(number);//輸入密碼driver.findElement(By.id("password")).clear();driver.findElement(By.id("password")).sendKeys(pwd);//點擊登錄按鈕driver.findElement(By.id("submitButton")).click();//登錄成功之后,獲得當前頁面的用戶信息info = driver.findElement(By.tagName("p")).getText().trim();//excel表格中有的github地址后面帶有"/",建議都去掉"/"再比較if(info.endsWith("/")) {info = info.substring(0, info.length()-1);}if(address.endsWith("/")) {address = address.substring(0, address.length()-1);}//比較查詢信息 if(info.equals(address)) {assertEquals(info,address);System.out.println(number+"的信息一致.");}else {System.out.println(number+"的信息不一致.");}}driver.close();} catch (FileNotFoundException e) { e.printStackTrace(); } catch (BiffException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }@Afterpublic 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;}}}5.?運行結果截圖
總結
以上是生活随笔為你收集整理的软件测试:Lab 2 Selenium的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [剑指offer][JAVA][面试第4
- 下一篇: 《移动App测试的22条军规》—App测