【问题标题】:Why when clearing the fields in my form with Selenium C# is my form repopulating with data when I hit submit?为什么使用 Selenium C# 清除表单中的字段时,当我点击提交时,我的表单会重新填充数据?
【发布时间】: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


【解决方案1】:

有时你只需要暴力破解这种事情。扩展方法可以清理内容,同时为您提供重试失败操作的方法:

namespace OpenQA.Selenium
{
    public static class WebElementExtensions
    {
        public static void EnterText(this IWebElement element, string text)
        {
            element.Clear();

            if (!string.IsNullOrEmpty(element.GetProperty("value")))
            {
                System.Threading.Thread.Sleep(500);
                element.Clear();
            }

            element.SendKeys(text);
        }

        public static void EnterTextRobustly(this IWebElement element, string text)
        {
            element.EnterText(text);

            if (element.GetProperty("value") == text)
                return;

            System.Threading.Thread.Sleep(500);
            element.EnterText(text);

            if (element.GetProperty("value") == text)
                return;

            System.Threading.Thread.Sleep(500);
            element.EnterText(text);

            if (element.GetProperty("value") != text)
                throw new WebDriverException($"Failed to enter text into a text field. Expected value: '{text}'; Actual value: '{element.GetProperty("value")}'");
        }
    }
}

并使用它:

// for example, in EnterLastName(...)
lastNameInput.EnterTextRobustly(lastName);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 1970-01-01
    • 2017-04-21
    • 1970-01-01
    相关资源
    最近更新 更多