gpt4 book ai didi

java - 如何通过 webdriver 测试输入字段功能的屏蔽

转载 作者:行者123 更新时间:2023-12-02 06:48:33 25 4
gpt4 key购买 nike

您好,我想测试密码字段是否掩盖了密码字段中输入的字符串。我如何通过 webdriver 测试它。我尝试过以下事情:

package unitTest.JUnitTestCases;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class PasswordTest
{
@Test
public void test()
{
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://docs.sencha.com/extjs/4.2.1/extjs-build/examples/form/adv-vtypes.html");
WebElement username=driver.findElement(By.id("textfield-1014-inputEl"));
username.sendKeys("admin");
System.out.println(username.getText());
WebElement password=driver.findElement(By.id("textfield-1015-inputEl"));
password.sendKeys("admin");
System.out.println(password.getAttribute("textContent"));
//WebElement login=driver.findElement(By.id("login"));
}
}

在这里,我在密码字段中输入一些值,并尝试获取在该字段中输入的文本,以便我检查它是否被屏蔽。蒂亚!!

最佳答案

我不知道是否有办法做到这一点,但我对此表示怀疑。因为这对我来说是浏览器级别的事情,这意味着浏览器应该照顾 <input type="password"> .

一种解决方案是使用某种屏幕截图比较工具。

另一种解决方案是检查输入是否具有属性 type="password" ,如果有,那么您可以假设您的网站已正确生成,但这并不意味着浏览器可以正确处理它。

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

请注意password.getAttribute("value")无论如何都会得到你输入的字符。 (就像我说的,密码屏蔽是浏览器的能力,文本将作为值存在,浏览器对用户隐藏它)

旁注:请勿使用 By.id("textfield-1014-inputEl")对于 ExtJS。尝试使用有意义的类名,例如 By.cssSelector(.x-form-type-password:nth-of-type(1) input[type="password"]) .

关于java - 如何通过 webdriver 测试输入字段功能的屏蔽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18332598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com