自动 gmail 登录 [在 oAuth 期间] 被用户验证阻止 [英] Automating gmail login [During oAuth] gets blocked with user verification

查看:27
本文介绍了自动 gmail 登录 [在 oAuth 期间] 被用户验证阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码可用于登录 gmail

This code works to login to 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();
}

问题

我们有一个正在测试的网络应用程序,用户可以在其中使用 google (oAuth2) 登录,但是 gmail 会通过用户验证(重置密码或验证码或电话号码)捕获自动化脚本.

We have a web application under test where users can login with google (oAuth2), however gmail traps automation script with a user verification (reset password or captcha or phone number).

问:

有什么办法可以避免 gmail 用户验证?

Is there any way to avoid gmail user verification ?

(我不是要求解决谷歌验证挑战,在用户手动运行的普通浏览器中,此验证挑战不会被触发(大多数情况下),但使用 selenium 有时会发生并且我的测试失败.)

(I am not asking to solve google verification challenge, in normal browser run by user manually this verification challenge doesn't get triggered (most of times), but with selenium it sometimes happens and fails my tests.)

2018 年 8 月 19 日更新

这是一个死胡同,绕过谷歌验证不是小事,搜索更多我发现服务虚拟化是解决这个问题的正确方法,可能Hoverfly.

This is a dead end, bypassing google verification is not trivial, upon searching more I found that service virtualization is the correct way to solve this issue, possibly Hoverfly.

推荐答案

使用 cookie 解决了我的问题

Using cookies solved my issue

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,将它们存储在某处并将键/值对传递给此方法以登录.

you have to login manually once then inspect to get cookies related to your app session, store them somewhere and pass the key/value pair to this method to get logged in.

这篇关于自动 gmail 登录 [在 oAuth 期间] 被用户验证阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆