Asp.net
如何使我的 Selenium 測試不那麼脆弱?
我們使用 Selenium 來測試 ASP.NET 應用程序的 UI 層。許多測試案例測試跨越多個頁面的較長流程。
我發現測試非常脆弱,不僅被實際更改頁面的程式碼更改破壞,而且還被無害的重構所破壞,例如重命名控制項(因為我需要將控制項的 clientID 傳遞給 Selenium 的 Click 方法等)或替換帶有中繼器的網格視圖。結果,我發現自己在“浪費”時間更新測試案例中的字元串值以修復損壞的測試。
有沒有辦法編寫更易維護的 Selenium 測試?還是更好的 Web UI 測試工具?
編輯添加: 通常,第一稿是通過在 IDE 中記錄測試來創建的。(這第一步可能由 QA 人員執行。)然後我重構生成的 C# 程式碼(提取常量、提取重複程式碼的方法、可能用不同的數據重複測試案例等)。但是每個測試案例的一般程式碼流仍然相當接近最初生成的程式碼。
我發現 PageObject 模式非常有用。
<http://code.google.com/p/webdriver/wiki/PageObjects>
也許一個好的開始方法是逐步重構您的測試案例。
我使用與 selenium + c# 相同的場景
這是我的程式碼的樣子:
測試方法看起來像這樣
[TestMethod] public void RegisterSpecialist(UserInfo usrInfo, CompanyInfo companyInfo) { var RegistrationPage = new PublicRegistrationPage(selenium) .FillUserInfo(usrInfo) .ContinueSecondStep(); RegistrationPage.FillCompanyInfo(companyInfo).ContinueLastStep(); RegistrationPage.FillSecurityInformation(usrInfo).ContinueFinishLastStep(); Assert.IsTrue(RegistrationPage.VerifySpecialistRegistrationMessagePayPal()); selenium.WaitForPageToLoad(Resources.GlobalResources.TimeOut); paypal.LoginSandboxPage(usrInfo.sandboxaccount, usrInfo.sandboxpwd); Assert.IsTrue(paypal.VerifyAmount(usrInfo)); paypal.SubmitPayment(); RegistrationPage.GetSpecialistInformation(usrInfo); var bphome = new BPHomePage(selenium, string.Format(Resources.GlobalResources.LoginBPHomePage, usrInfo.AccountName, usrInfo.Password)); Assert.IsTrue(bphome.VerifyPageWasLoaded(usrInfo)); Assert.IsTrue(bphome.VerifySpecialistProfile()); bphome.Logout(); }頁面對象將是這樣的
public class PublicRegistrationPage { public ISelenium selenium { get; set; } #region Constructors public PublicRegistrationPage(ISelenium sel) { selenium = sel; selenium.Open(Resources.GlobalResources.PublicRegisterURL); } #endregion #region Methods public PublicRegistrationPage FillUserInfo(UserInfo usr) { selenium.Type("ctl00_cphComponent_ctlContent_wizRegister_tUserFirstName", usr.FirstName); selenium.Type("ctl00_cphComponent_ctlContent_wizRegister_tUserLastName", usr.LastName); selenium.Select("ctl00_cphComponent_ctlContent_wizRegister_ddlUserCountry", string.Format("label={0}",usr.Country )); selenium.WaitForPageToLoad(Resources.GlobalResources.TimeOut); selenium.Type("ctl00_cphComponent_ctlContent_wizRegister_tUserEmail", usr.Email ); selenium.Type("ctl00_cphComponent_ctlContent_wizRegister_tUserDirectTel", usr.DirectTel); selenium.Type("ctl00_cphComponent_ctlContent_wizRegister_tUserMobile", usr.Mobile); return this; }}
希望這可以幫助。