我怎样才能让我的Selenium测试不易碎? [英] How can I make my Selenium tests less brittle?

查看:388
本文介绍了我怎样才能让我的Selenium测试不易碎?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用Selenium测试我们的ASP.NET应用程序的UI层。许多测试用例测试跨几个页面的长流。

We use Selenium to test the UI layer of our ASP.NET application. Many of the test cases test longer flows that span several pages.

我发现,测试是很脆,不只是通过实际更改页面code的变化,但也无害重构,如重命名控制(碎因为我需要控制的ClientID传递给硒的Click法等),或具有转发器代替一个gridview。结果,我发现自己浪费时间在我的测试情况下,为了破镜重圆测试更新的字符串值。

I've found that the tests are very brittle, broken not just by code changes that actually change the pages but also by innocuous refactorings such as renaming a control (since I need to pass the control's clientID to Selenium's Click method, etc) or replacing a gridview with a repeater. As a result I find myself "wasting" time updating string values in my test cases in order to fix broken tests.

有没有写更多的维护Selenium测试的方法吗?或者更好的web用户界面测试工具?

Is there a way to write more maintainable Selenium tests? Or a better web UI testing tool?

编辑补充:
一般来说,第一稿是由记录在IDE中创建测试。 (这第一步可以由QA人员来执行。)然后我重构生成的C#code(提取常数,反复code提取方法,也许不同的数据重复测试的情况下,等)。但是,code每个测试用例的一般流程仍然相当接近最初生成code。

Edited to add: Generally the first draft is created by recording a test in the IDE. (This first step may be performed by QA staff.) Then I refactor the generated C# code (extract constants, extract methods for repeated code, maybe repeat the test case with different data, etc). But the general flow of code for each test case remains reasonably close to the originally generated code.

推荐答案

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

I've found PageObject pattern very helpful.

<一个href=\"http://$c$c.google.com/p/webdriver/wiki/PageObjects\">http://$c$c.google.com/p/webdriver/wiki/PageObjects

更多信息:
- <一个href=\"http://stackoverflow.com/questions/616810/whats-the-point-of-selenium/616887\">http://stackoverflow.com/questions/616810/whats-the-point-of-selenium/616887
- <一个href=\"http://stackoverflow.com/questions/99876/selenium-critique/211452\">http://stackoverflow.com/questions/99876/selenium-critique/211452

也许开始的好办法是逐步重构测试用例。

maybe a good way to start is to incrementally refactor your test cases.

我用同样的情况下,您有硒+ C#

I use the same scenario you have selenium + c#

下面是我的code的样子:

Here is how my code looks like:

    [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;
    }

}

希望这有助于。

这篇关于我怎样才能让我的Selenium测试不易碎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆