加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何使我的硒测试不那么脆弱?

发布时间:2020-12-15 18:35:43 所属栏目:asp.Net 来源:网络整理
导读:我们使用Selenium来测试我们的ASP.NET应用程序的UI层。许多测试用例测试跨越几页的较长流程。 我发现测试非常脆弱,不仅仅是通过实际更改页面的代码更改,而且还通过无害重构(如重命名控件)(因为我需要将控件的clientID传递给Selenium的Click方法等)或替换具
我们使用Selenium来测试我们的ASP.NET应用程序的UI层。许多测试用例测试跨越几页的较长流程。

我发现测试非常脆弱,不仅仅是通过实际更改页面的代码更改,而且还通过无害重构(如重命名控件)(因为我需要将控件的clientID传递给Selenium的Click方法等)或替换具有中继器的gridview。因此,我发现自己在我的测试用例中“浪费”时间更新字符串值,以修复断开的测试。

有没有办法编写更可维护的硒测试?还是更好的网络UI测试工具?

编辑添加:
通常,第一稿是通过在IDE中记录测试来创建的。 (这个第一步可以由QA的工作人员执行。)然后我重构生成的C#代码(提取常量,重复代码的提取方法,可能会重复不同数据的测试用例等)。但是,每个测试用例的代码的一般流程仍然与原始生成的代码相当接近。

解决方法

我发现PageObject模式非常有帮助。

http://code.google.com/p/webdriver/wiki/PageObjects

更多信息:
– What’s the Point of Selenium?
– Selenium Critique

可能一个很好的方法就是逐步重构你的测试用例。

我用同样的场景你有硒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;
    }

}

希望这可以帮助。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读