今天的實驗是使用Selenium進行Web的測試。首先須要在Firefox中安裝相關插件Selenium。安裝好後,在Firefox右上角會出現一個標誌:。出現這個標誌就表明安裝好了。java
接下來點擊這個按鈕,就會出現Selenium IDE的界面:web
單擊右側紅色的錄製按鈕開始錄製。錄製的步驟即爲測試一個用例要進行的全部步驟,包括填入網址,輸入學號密碼等。最後選中結果界面中的郵箱,郵件選擇assertText.測試
以後在Selenium IDE中輸出相應的結果,注意在輸出錢要在option中勾選Enable experimental features選項。ui
輸出後倒入Selenium和csv文件讀取的相關jar包,編寫程序以下:this
package com.example.tests; import java.io.IOException; import java.nio.charset.Charset; import java.util.Arrays; import java.util.Collection; import java.util.regex.Pattern; import java.util.concurrent.TimeUnit; import org.junit.*; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; 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; import com.csvreader.CsvReader; @RunWith(Parameterized.class) public class SeleniumTest { private WebDriver driver; private String baseUrl; private boolean acceptNextAlert = true; private StringBuffer verificationErrors = new StringBuffer(); private String id, pwd,email; public SeleniumTest(String id, String email) { this.id = id; this.pwd = id.substring(4); this.email = email; } @Before public void setUp() throws Exception { System.setProperty("webdriver.firefox.bin", "C:/Program Files/Mozilla Firefox/firefox.exe"); driver = new FirefoxDriver(); baseUrl = "http://www.ncfxy.com/"; driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @Parameters public static Collection<Object[]> getData() throws IOException { Object[][] obj = new Object[109][]; CsvReader r = new CsvReader("C:/Users/暢/Downloads/info.csv", ',', Charset.forName("GBK")); int count = 0; r.readHeaders(); while(r.readRecord()){ obj[count] = new Object[]{r.get("id"), r.get("email")}; count++; } return Arrays.asList(obj); } @Test public void testUntitled() throws Exception { driver.get(baseUrl); driver.findElement(By.id("name")).clear(); driver.findElement(By.id("name")).sendKeys(this.id); driver.findElement(By.id("pwd")).clear(); driver.findElement(By.id("pwd")).sendKeys(this.pwd); driver.findElement(By.id("submit")).click(); assertEquals(this.email, driver.findElement(By.xpath("//tbody[@id='table-main']/tr/td[2]")).getText()); } @After public void tearDown() throws Exception { driver.close(); String verificationErrorString = verificationErrors.toString(); if (!"".equals(verificationErrorString)) { fail(verificationErrorString); } } }
可是在我嘗試運行時,老是出現以下錯誤:spa
上午查詢後發現是由於Firefox的版本較高的緣由,以後到其餘同窗電腦上運行問題就解決了。獲得結果以下:firefox