此代码用于登录 Gmail
public void login(User user) {
WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement emailTextBox = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("identifierId")));
emailTextBox.sendKeys(user.email);
WebElement nextButton = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(text(), 'Next')]")));
nextButton.click();
WebElement passwordTextBox = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@type='password']")));
passwordTextBox.sendKeys(user.password);
nextButton = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(text(), 'Next')]")));
nextButton.click();
}
Problem
我们有一个正在测试的 Web 应用程序,用户可以使用 google (oAuth2) 登录,但是 gmail 会通过用户验证(重置密码或验证码或电话号码)捕获自动化脚本。
Q:
有什么办法可以避免gmail用户验证吗?
(我并不是要求解决谷歌验证挑战,在用户手动运行的正常浏览器中,此验证挑战不会被触发(大多数时候),但使用硒时,有时会发生这种情况并使我的测试失败。)
更新 19.08.2018
这是一个死胡同,绕过谷歌验证并不简单,经过更多搜索,我发现服务虚拟化是解决这个问题的正确方法,可能Hoverfly.
使用 cookie 解决了我的问题
public HomePage googleAutoLogin(String cookieKey, String cookieValue) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 1);
Date afterOneHour = cal.getTime();
driver.manage().deleteAllCookies();
Cookie cookie = new Cookie.Builder(cookieKey, cookieValue)
.domain("qreflect.com")
.path("/")
.expiresOn(afterOneHour)
.isHttpOnly(true)
.build();
driver.manage().addCookie(cookie);
driver.navigate().refresh();
logger.log(Level.INFO, "Finished adding cookie");
return this;
}
您必须手动登录一次,然后检查以获取与您的应用程序会话相关的 cookie,将它们存储在某处并将键/值对传递给此方法才能登录。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)