【发布时间】:2023-04-08 08:49:01
【问题描述】:
我是测试环境的新手。有一个基本的表格,如姓名、地址等。当我点击保存按钮并显示必填字段错误消息时,如何测试失败?我能够使用 XPath 找到错误消息。这是我尝试过的代码,但反过来。当显示错误消息时,我尝试过的代码通过了测试。如果显示错误消息,请告知测试失败的方法。
try
{
//Clear the value of first name
IWebElement firstname = driver.FindElement(By.Id("FirstName"));
firstname.Clear();
//Click on save button
IWebElement save_profile = driver.FindElement(By.XPath("//div[@class='form-group buttons']/div/input"));
save_profile.Click();
//Locate Error Message and Compare the text wit the displayed error message.
IWebElement FirstNameError = driver.FindElement(By.XPath("//form[@class='default form-horizontal']/fieldset/div[4]/div[2]/span/div"));
Assert.AreEqual("Please, enter 'First Name'.", FirstNameError.Text);
}
catch
{
//Fails the test if error message is not displayed
Assert.Fail();
}
如果找到元素,有什么方法可以使测试失败?提前致谢。
【问题讨论】:
标签: c# unit-testing selenium-webdriver