【问题标题】:Can I verify that an input field is masked or shows the text?我可以验证输入字段是否被屏蔽或显示文本?
【发布时间】:2021-09-16 19:54:03
【问题描述】:

使用 Java 和 webdriver,我正在尝试设置一个测试来验证我输入的密码是否被屏蔽。使用我的工具,我们有一个设置允许您隐藏密码或显示文本。我想知道是否有人曾经用 Selenium 验证过这样的事情。

这是我要检查的内容 设置关闭 - 在输入字段中键入并验证是否显示文本 设置 ON - 在输入字段中输入并验证我的输入是否被屏蔽

我相信浏览器会处理屏蔽,所以我不确定我是否可以做这个测试。输入字段的元素没有任何关于屏蔽的属性。

感谢您的帮助

【问题讨论】:

    标签: java selenium


    【解决方案1】:

    如果输入,检查type属性,如果是text,则文本没有被屏蔽,如果是password,则被屏蔽。

    【讨论】:

      【解决方案2】:

      所以假设您的输入字段如下

      <input id="pw" type="password" blah>
      

      您可以通过以下方式检查该字段是否被屏蔽

      WebElement password = driver.findElement(By.id("pwd"));
      if (password.getAttribute("type") == "password"){
          // if it is, do something
      }else {
          // not masked
      }
      

      【讨论】:

        【解决方案3】:

        (我的 GitHub 页面正在花时间更新)这是您可以尝试的 HTML:

        <input type="text" id="regular" />
        <input type="password" id="masked" />
        

        并且使用Getting Started with Selenium 框架,您的测试看起来像这样来验证以确保该字段确实被屏蔽了。

        @Config(browser = Browser.CHROME, url = "http://ddavison.github.io/tests/maskedtextbox.htm")
        public class MaskedText extends AutomationTest {
            @Test
            public void testMasked() {
                // validate that the regular text box is not masked.
                setText         (By.id("regular"), "Unmasked")
                .validateText   (By.id("regular"), "Unmasked") // validate that this isn't masked.
        
                .setText        (By.id("masked"),  "Masked")
                .validateTextNot(By.id("masked"),  "Masked")   // validate that the text is not "Masked" if it is, then it isn't masking correctly.
                ;
            }
        }
        

        validateTextNot被调用时,它会得到那个字段的文本,它会返回******,而Masked不会.equal那个。

        【讨论】:

          【解决方案4】:

          System.out.println(password.getAttribute("type").equals("password"));

          应用该代码。 它将检查您的类型是否为密码的属性。 if 等于 Password 则返回 true 否则返回 false。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-10-14
            • 2013-03-04
            相关资源
            最近更新 更多