【发布时间】:2021-06-03 13:21:19
【问题描述】:
我正在使用 C# 使用 selenium 测试表单验证。我有一个更新按钮,单击该按钮会弹出一个用户先前输入信息的表单。我正在尝试通过查找元素并清除字段来测试尝试提交空更新表单的用户。提交具有空值的表单时,我有应该显示在该字段下的验证错误。这在手动运行我的程序时有效。运行测试时,我可以看到字段正在被清除,但是当我点击提交时,我的表单正在重新填充以前存在的数据。我也尝试清除表单,然后在一个空字符串中作为键,但是当我点击提交时它仍然会重新填充表单。我还尝试清除表单,然后使用特殊字符填写以提示不同的错误消息。表单正在清除,但随后它会采用之前输入的值并将特殊字符放在该值的末尾。这是我的代码的一部分:
var user = new Users(driver);
user.EditUserByEmail("pinocchio@aol.com");
driver.FindElement(By.Name("firstName")).Clear();
driver.FindElement(By.Name("lastName")).Clear();
driver.FindElement(By.Name("email")).Clear();
user.EnterFirstName("$");
user.EnterLastName("$");
user.EnterEmail("$");
var modal = new Modal(driver);
modal.SubmitForm();
这些是我在 Users 类中使用的方法:
public void EnterFirstName(string firstName)
{
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(30));
var firstNameInput = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.Name("firstName")));
firstNameInput.SendKeys(firstName);
}
public void EnterLastName(string lastName)
{
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(30));
var lastNameInput = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.Name("lastName")));
lastNameInput.SendKeys(lastName);
}
public void EnterEmail(string email)
{
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(30));
var emailInput = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.Name("email")));
emailInput.SendKeys(email);
}
根据我的代码,我希望每个字段只输入一个 $。我不知道以前的数据如何或为什么会重新填充。
【问题讨论】:
-
尝试使用
clear method清除该字段,然后使用SendKeys(),看看有什么不同。 -
@KunduK 是的,这确实有效,但前提是我在清除后和提交表单之前添加了 thread.sleep()。
标签: c# forms selenium validation testing