生活随笔
收集整理的這篇文章主要介紹了
功能测试——Selenium自动化功能测试
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實驗目的
(1)學習使用了解Selenium自動化功能測試工具,了解Selenium測試工具的測試模式和過程;
(2)掌握使用Selenium錄制測試腳本、執行并分析測試腳本。
實驗設備
主流PC機一套,要求安裝windows操作系統、Selenium最新版本、OFFICE工具;
實驗內容
(1)使用Selenium對MercuryTours網站進行功能測試。要求錄制預訂機票的完整過程,然后執行測試腳本并分析結果。
(2)設置至少三個檢驗點,觀測檢測結果。
實驗要求
(1)撰寫實驗報告,主要填寫本人測試步驟和自己的實驗體會。
(2)提交錄制的測試腳本。
(3)提交檢測結果。
實驗過程
錄制測試腳本前的準備
在測試前需要先確認網站以及Selenium是否符合測試需求。確認你已經知道如何對應用程序進行測試
例如要測那些功能、操作步驟、輸入的數據、預期的結果等。打開Selenium IDE后,選擇“在新項目中錄制一個新的測試”,輸入項目名以及需要測試的網址后,開始進行錄制。
錄制測試腳本之前,需要完成以下兩步:
Mtours航空網站本地服務開啟;
瀏覽器輸入http://localhost:8080/Mtours,創建一個用戶。
錄制測試腳本
根據想要測試的功能模塊,對Web應用進行操作,Selenium會錄制用戶的一系列操作,并將這些操作轉換為指令集存儲下來。
加強測試腳本
在測試腳本中加入檢查點,可以檢查網頁超級鏈接、對象屬性或是字符串,以驗證應用程序的功能是否正確。Selenium提供了三種驗證方法,assert、verify以及waitfor,其區別如下:
Assert:失敗時,該測試將終止;
Verify:失敗時,該測試繼續執行,并將錯誤日志記錄在日顯示屏;
Waitfor:等待某些條件變為真,一般使用在Ajax應用程序的測試。
調試腳本(對測試腳本除錯)
在修改過測試腳本之后,需要調試測試腳本,檢查腳本是否存在錯誤,以確保測試腳本能正常且流暢的執行。
執行測試腳本
點擊執行測試按鈕后,Selenium將執行腳本中的指令集,包括用戶添加的驗證類指令集。
分析測試結果
查看日志輸出中的運行結果,分析測試執行的結果,若出現問題,則記錄問題,并找出應用程序的問題所在。
導出測試用例(選做)
可以嘗試將測試腳本導出成代碼的形式并運行,如導出成JUnit。
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import static org.junit.Assert.*
;
import static org.hamcrest.CoreMatchers.is
;
import static org.hamcrest.core.IsNot.not
;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Keys;
import java.util.*;
import java.net.MalformedURLException;
import java.net.URL
;
public class LoginTest {private WebDriver driver
;private Map<String, Object> vars
;JavascriptExecutor js
;@Beforepublic void setUp() {driver
= new ChromeDriver();js
= (JavascriptExecutor) driver
;vars
= new HashMap<String, Object>();}@Afterpublic void tearDown() {driver
.quit();}@Testpublic void login() {driver
.get("http://localhost:8080/mtours/servlet/WelcomeServlet");driver
.findElement(By.name("userName")).click();driver
.findElement(By.name("userName")).sendKeys("STZG");driver
.findElement(By.name("password")).click();driver
.findElement(By.name("password")).sendKeys("123456");driver
.findElement(By.name("login")).click();}
}
參考文章
總結
以上是生活随笔為你收集整理的功能测试——Selenium自动化功能测试的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。