Selenium使用页面对象模型C#时验证元素值

时间:2017-06-14 13:07:55

标签: c# selenium selenium-webdriver

我正在使用Selenium(Nunit)C#使用页面对象模型和页面工厂构建测试套件。我试图找出测试中验证页面上某些字段(元素)中值的最佳方法。

e.g。

public class OrderPage
{
    [FindsBy(How = How.Name, Using = "Username")]
    private IWebElement usernameField;

    [FindsBy(How = How.Name, Using = "ValidToDateTime")]
    private IWebElement dateToField;


    public string validateField() //method to validate field
    {
        string field = usernameField.GetAttribute("text");

        return field;
    }
}

呼叫测试: -

[Test]
public void checkFields()
{

  string username = "Smith";
  string toDate = "14th June";

  // check the content of the Username field is correct
  string content = Pages.OP.validateField();
  Assert.IsTrue(content.Contains(username));

  // check the content of the ValidToDateTime field is correct ...etc...


}

我简化了代码,但基本上Order页面有多个包含文本数据的文本字段。 [Test]正在读取一些数据(参见string username和toDate),测试需要检查页面上的文本是否与读入Test的数据相匹配。

使用Page Object模型时,我需要了解正确的方法,例如我应该用硬编码的webelements编写一个方法来验证所有必要的字段吗?如果验证发生在OrderPage.validateField方法中并返回true / false,还是应该返回值并且[Test]进行验证?

这是一个OO方法问题 - 你会怎么做?感谢。

1 个答案:

答案 0 :(得分:0)

我所做的是我有一个Validations对象,它包含与验证相关的方法,然后是一个Report对象,它包含与报告相关的方法(这与在测试运行结束时生成html报告有关)。

我的Validations对象如下所示:

public class Validations
    {
        private HtmlReport htmlReport;
        private MsTESTReport msTestReport;

        public Validations(HtmlReport htmlReport, MsTESTReport msTestReport)
        {

            this.htmlReport = htmlReport;
            this.msTestReport = msTestReport;

        }

        public void assertionPass(string stepName, string message)
        {
            htmlReport.reportPassEvent(stepName, message);
        }

        public void assertionFailed(string stepName, string message)
        {
            AssertFailedException assertionFailedError = getAssertionFailedErrorObject(stepName, message);
            htmlReport.reportFailEvent(stepName, message);
            msTestReport.reportAssertionFailed(assertionFailedError);
        }

        private AssertFailedException getAssertionFailedErrorObject(string stepName, string errorMessage)
        {
            string message = "StepName: " + stepName + "\n ErrorMessage : " + errorMessage;
            AssertFailedException assertionFailedError = new AssertFailedException(message);
            return assertionFailedError;
        }
    }

注意:您不必像我一样包含所有报告,这只是我的验证对象的示例。您可以在测试通过时将结果输出到控制台,并在测试失败时抛出AssertionFailedException。

通常在页面对象类中的方法中调用它:

    public class OrderPage : Page
    {
        [FindsBy(How = How.Name, Using = "Username")]
        private IWebElement usernameField;

        [FindsBy(How = How.Name, Using = "ValidToDateTime")]
        private IWebElement dateToField;

public OffersPage(IWebDriver browser, Report report, Validations validations)
            : base(browser, report, validations)
        {
            //Constructor
        }


        public string validateField(string expectedText) //method to validate field
        {
            try
           {
               string actualText = usernameField.GetAttribute("text");
               if (actualText.ToLower().Trim().Equals(expectedText))
               {
                   validations.assertionPass("validateField", "Expected text displays as expected.");
                }
                else
                {
                    validations.assertionFailed("validateField", "The expected text does not display as expected " + actualText + " displays instead.");
                }
           }
           catch (Exception ex)
           {
              //this is typically where I put my report method
              //but I'm not including it in the rest of the example 
              //because you didn't ask how to generate reports and this 
              //is just a way to put the exception thrown in the html 
              //report. You could just as easily throw the exception 
               //here if need be
              report.reportException(ex, "validateField");
           }
    }

然后你会在测试方法中调用它,如下所示:

public class SomeTest : CommonSteps 
{
    public SomeTest()
       :base()
    {
       //Constructor
    }

    [Test]
    public void checkFields()
    {
        //Instantiate your OrderPage object in a CommonMethods class 
        //you would also instantiate and set up your Validation/Report objects 
        //so they could be passed down through the page objects
       //You would also open the browser, basically any before and after test 
       //set up in the CommonMethods class
       //Your test class would then inherit the CommonMethods class and it 
       //would start at the test itself.

        //oPage is declared in CommonMethods
        oPage.validateField("Smith");

    }
}

CommonMethod类的简短示例如下:

 public class CommonSteps
    {
        protected IWebDriver browser;
        protected HomePage homePage;
        protected Validations validations;

        [BeforeScenario]
        public void BeforeScenario()
        {
            //code to initializeBrowser();

             //code to initializevalidationsObject();

             //code to openApplication();

        }

        [AfterScenario]
        public void AfterScenario()
        {
            try
            {
                //code to close the application safely
            }
            catch (WebDriverException)
            {

            }

            finally
            {

            }
        }
}