有没有一种不用在代码中写入实际密码就可以登录网站的方法。例如,我创建了一个登录函数:
var isAlreadyLogIn = false;
function LogIn (userId, password) {
if (!isAlreadyLogIn) {
driver.findElement(By.xpath("//*[@id='Email']")).sendKeys(userId);
driver.findElement(By.xpath("//*[@id='Password']")).sendKeys(password);
driver.findElement(By.xpath("//input[@value='Login']")).click();
isAlreadyLogIn = true;
}
}
it('Should login', function(done) {
LogIn("username", "password");
});
最佳答案
您的代码将需要访问凭据。我认为解决这个问题的最常见方法是将凭据放入配置文件并从那里读取。如果您不希望密码包含在代码中,您可以不将配置文件提交到存储库,而是以不同的方式共享它。或者,您可以将用户名和密码作为 command-line arguments 传递到你的测试。
Here are NodeJS examples how to store the credentials in different ways .这些例子是针对数据库的,但思想是一样的。
关于node.js - Selenium WebDriver : Login to a website,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46584101/