如何通过webdriver测试输入字段function的屏蔽

您好我想测试密码字段是否屏蔽了密码字段中输入的字符串。 我怎么能通过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")); } } 

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

我不知道是否有办法做到这一点,但我对此表示怀疑。 因为这对我来说是一个浏览器级别的东西,这意味着浏览器应该处理

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

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

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

请注意, password.getAttribute("value")将为您提供您输入的字符。 (就像我说的,密码屏蔽是浏览器的能力,文本将作为值存在,浏览器将其隐藏起来)

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