硒使用页面对象模型验证元素值C#
问题描述:
我使用页面对象模型和页面工厂构建了一个包含Selenium(Nunit)C#的测试套件。我试图找出测试中验证页面上某些字段(元素)值的最佳方法。硒使用页面对象模型验证元素值C#
例如
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...
}
我已经简化代码,但基本上是订单页面已经包含文本数据的多个文本字段。 [Test]正在读取一些数据(请参阅字符串username和toDate),并且测试需要检查页面上的文本是否与读入Test的数据相匹配。
我需要了解使用页面对象模型时的正确方法,例如我应该用硬编码的webelements编写一种方法来验证所有必要的字段吗?应该在OrderPage.validateField方法中进行验证并返回true/false,还是应该返回值并且[Test]进行验证?
这是一个面向对象的方法问题 - 你会怎么做?谢谢。
答
我所做的是我有一个Validations对象,它包含与验证有关的方法,然后是一个包含与报告相关的方法的Report对象(这与在测试运行结束时生成html报告有关)。
我的验证对象是这样的:
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
{
}
}
}
感谢您花时间发布,非常有用。 –