问题描述
我想测试密码字段是否掩盖了在密码字段中输入的字符串.我如何通过webdriver测试它.我已经尝试过以下方法:
Hi i want to test whether the password field is masking the entered string in password field. how can i test it throught webdriver. i have tried below thing :
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!
Here i am entering the some value in the password field and trying to get the text entered in that field so that i will check whether it is masked or not.TIA!!
推荐答案
我不知道有没有办法做到这一点,但我对此表示怀疑.因为这对我来说是浏览器级别的事情,这意味着浏览器应注意<input type="password">
.
I don't know if there is a way to do that, but I doubt it. Because this is a browser-level thing to me, which means browsers should take care of <input type="password">
.
一种解决方案是使用某种屏幕截图比较工具.
One solution is to use some kind of screenshot comparing tool.
另一种解决方案是检查输入是否具有属性type="password"
(如果具有),则可以假定您的网站是正确生成的,但这并不意味着浏览器可以正确处理它.
One other solution is to check if the input has attribute type="password"
, if it has, then you can assume your site is generated correctly, but this doesn't mean that browser handles it right.
System.out.println(password.getAttribute("type").equals("password"));
请注意,无论如何password.getAttribute("value")
都会为您提供您键入的字符. (就像我说的那样,密码屏蔽是浏览器的功能,文本将作为值存在,浏览器向用户隐藏它)
Note that password.getAttribute("value")
will get you the characters you type in anyway. (which is like I said, password masking is browser's ability, the text will be there as value, browser hides it from user)
侧面说明:不要将By.id("textfield-1014-inputEl")
用于ExtJS.尝试使用有意义的类名,例如By.cssSelector(.x-form-type-password:nth-of-type(1) input[type="password"])
.
Side note: Don't use By.id("textfield-1014-inputEl")
for ExtJS. Try use meaningful class names like By.cssSelector(.x-form-type-password:nth-of-type(1) input[type="password"])
.
这篇关于如何通过WebDriver测试输入字段功能的掩盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!