[軟體測試基礎2]基於selenium的自動化測試

來源:互聯網
上載者:User

標籤:

這次上機我們主要使用Selenium進行自動化測試首先我們需要下載selenium-java的依賴項。

若使用maven管理項目,則在.pom檔案中加入如下依賴項:

1 <dependency>2 <groupId>org.seleniumhq.selenium</groupId>3 <artifactId>selenium-java</artifactId>4 <version>2.53.0</version>5 </dependency>

 

也可以手動下載jar包,具體可以上官網下載,在此不再贅述。

 

下面是我們實驗內容:

一、實驗目的

  • 安裝SeleniumIDE外掛程式
  • 學會使用SeleniumIDE錄製指令碼和匯出指令碼
  • 訪問http://www.ncfxy.com使用學號登入系統(賬戶名為學號,密碼為學號後6位),進入系統後可以看到該使用者的郵箱。
  • 編寫Selenium Java WebDriver程式,測試info.csv表格中的學號和郵箱的對應關係是否正確。

二、Java WebDriver代碼:

   
package lesson1; import static org.junit.Assert.*; import java.util.concurrent.TimeUnit; import org.junit.*;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.support.ui.ExpectedCondition;import org.openqa.selenium.support.ui.WebDriverWait; import java.io.File;  import java.io.FileInputStream;  import java.io.IOException;import java.io.InputStream;     import jxl.Cell;    import jxl.CellType;   import jxl.Sheet;   import jxl.Workbook;   import jxl.write.Label;    public class ExampleForFireFox  {            private WebDriver driver;      private String baseUrl;         @Before      public void setUp() throws Exception {        driver = new FirefoxDriver();        baseUrl = "http://www.ncfxy.com/";        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);      }          @Test    public void Test1() throws Exception {        jxl.Workbook readwb = null;          //構建Workbook對象, 唯讀Workbook對象          //直接從本地檔案建立Workbook          InputStream instream = new FileInputStream("D:/info.xls");                   readwb = Workbook.getWorkbook(instream);        //System.out.println("jjjjjjj ");        //Sheet的下標是從0開始          //擷取第一張Sheet表          Sheet readsheet = readwb.getSheet(0);          //擷取Sheet表中所包含的總列數          int rsColumns = readsheet.getColumns();          //擷取Sheet表中所包含的總行數          int rsRows = readsheet.getRows();          //擷取指定儲存格的對象引用          for (int i = 0; i <rsRows; i++)          {              driver.get(baseUrl);            Cell cell = readsheet.getCell(0, i);             String username = cell.getContents();            String password = username.substring(4, 10);            // 通過 id 找到 input 的 DOM            WebElement element = driver.findElement(By.id("name"));            WebElement element1 = driver.findElement(By.id("pwd"));             //System.out.println(element.getSize());              // 輸入關鍵字            element.sendKeys(username);            element1.sendKeys(password);             // 提交 input 所在的  form            element.submit();                         //擷取得到的郵箱            WebElement element2 = driver.findElement(By.xpath("//td[2]"));                         String mailByWeb = element2.getText();            String mailByInfo = readsheet.getCell(1,i).getContents();                         assertEquals(mailByInfo,mailByWeb);                         //System.out.println(element2.getText());                          // System.out.println();          }                 /* //利用已經建立的Excel工作薄,建立新的可寫入的Excel工作薄          jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(                  "D:/info.xls"), readwb);          //讀取第一張工作表          jxl.write.WritableSheet ws = wwb.getSheet(0);          //獲得第一個儲存格對象          jxl.write.WritableCell wc = ws.getWritableCell(0, 0);          //判斷儲存格的類型, 做出相應的轉化          if (wc.getType() == CellType.LABEL)           {              Label l = (Label) wc;              l.setString("新姓名");          }          //寫入Excel對象          wwb.write();          wwb.close();   */       //關閉讀入流        readwb.close();                             }}

 

三、心得

通過本次實驗我們學會了如何用Selenium IDE實現簡單的自動化測試,其中包括測試的錄製,編寫及運行,讓我們更加深入的瞭解了測試的方法和測試的重要性。

[軟體測試基礎2]基於selenium的自動化測試

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.