使用页面对象模式验证元素的状态



假设我想检查我的密码是否已成功更改。哪种方法更适合使用?

[FindsBy(How = How.XPath, Using = "//span[@id='confirmation' and text()='Success!']")]
public IWebElement PasswordChangedSuccessfullyConfirmationElement { get; protected set; }
public bool IsPasswordChanged()
{
return  PasswordChangedSuccessfullyConfirmationElement != null && 
PasswordChangedSuccessfullyConfirmationElement.Displayed;
}

[FindsBy(How = How.Id, Using = "confirmation")]
public IWebElement PasswordChangedSuccessfullyConfirmationElement { get; protected set; }
public bool IsPasswordChanged()
{
return  PasswordChangedSuccessfullyConfirmationElement != null && 
PasswordChangedSuccessfullyConfirmationElement.Text == "Success!" &&
PasswordChangedSuccessfullyConfirmationElement.Displayed;
}

我更喜欢第一种方法,因为它会等待元素文本更改(直到隐式超时(。第二种方法不会等待元素文本更改成功。如果元素不是新的,它有时可能会失败。在第二种方法中,您必须添加等待语句,以便更改文本。

最新更新