如何通过量角器处理 Chrome 上的 HTTP 基本身份验证?

How to handle HTTP Basic authentication on Chrome by Protractor?

谁能帮我处理“HTTP basic

Chrome 上的身份验证提示?

我无法通过常规

访问我们的暂存环境的身份验证

方法 (http://user:password@domain.com).

你知道怎么处理吗?
. . .

技术信息:

编程语言:JavaScript

框架:Jasmine JS

浏览器:Google Chrome(最新)

平台:Selenium Webdriver,Node.JS

跑步者:量角器


我的代码如下:

//First Demo for E2E Automation testing by Protractor for AngularJS

describe ('Do this before every test case', function() {
beforeEach(function() {
    browser.get("http://user:password@domain.com');
    expect(browser.getCurrentUrl()).toEqual("http://wwww.meet2know.com/");
});

var login = require ('../login.js');
});

似乎没有简单的解决方法:

How to handle basic authentication with protractor?.

我建议禁用用于您 运行 测试的 IP 地址的基本身份验证。这是有道理的,因为基本的身份验证登录不需要测试,也不会出现在实时环境中。

可以按照其他答案中的建议使用代理(BrowserMob GitHub,带指南)处理此问题。

或者,您可以尝试处理警告框并插入您的凭据信息(这是 Java 代码示例,您应该在 JS 中有类似的东西):

WebDriverWait wait = new WebDriverWait(driver, 3);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.authenticateUsing(new UserAndPassword("USER", "PASS"));

BrowserMob Proxy 可能会有帮助。过去在处理基本身份验证时我很幸运。

所以我 运行 进入了这个问题。问题是您的代码和您所做的期望永远不会解决同一件事。

browser.get("http://user:password@domain.com');

expect(browser.getCurrentUrl()).toEqual("http://wwww.meet2know.com/");

出于某种原因(我不知道为什么)当 browser.get() HTTP Auth 解析时,它不再认为 URL 有效。

要处理此问题,最简单的解决方案是设置前一步,点击 Auth URL,然后在每个场景的开头使用 Given I navigate to the "/" url 步骤定义。下面的代码是小黄瓜、黄瓜和网络驱动程序,但您将能够看到它在您的案例中是如何工作的。

Gherkin .feature 文件

  Scenario: Open the homepage
    Given I navigate to the url "/"

Cucumber/Webdriver 步骤定义

  // Set the two url's as constants to use in the steps below 
  const authUrl = 'http://user:password@domain.com'
  const url = 'http://wwww.meet2know.com'

  this.Before(() => {
    // resolve the HTTP authentication
    browser.url(authUrl)
  })

  this.Given(/^I am on the url "([^"]*)"$/, function (path) {
    // use the non auth URL to test normally
    browser.url(url + path)
    expect(browser.getUrl()).toEqual(url + path)
  })

这里的好处是如果你突然需要放弃对 HTTP 身份验证的要求,你可以只注释掉之前的步骤。此外,最好先在场景或功能中定义路径,因为它为您提供了有关功能或场景位置的上下文。

如果你在 2021 年阅读这篇文章并且你不想引入 BrowserMob 或其他软件只是为了做一些像基本身份验证这样微不足道的事情,我明白了。我花了一些时间研究这个,虽然 Chrome 确实不再允许 URL 中的 username:password@domain,但 它仍然支持它。实施简单,需要零附加软件。您需要做的就是添加一个 Chrome 命令行开关来禁用网络钓鱼检测:

const caps = Capabilities.chrome();

caps.set('goog:chromeOptions', {
  args: ['--disable-client-side-phishing-detection']
});

const driver = await new Builder().forBrowser('chrome').withCapabilities(caps).build();

现在您可以在 URL 中添加您的凭据:

await driver.get('https://username:password@domain.com/');

这正是 Katalon - 也许还有其他测试框架 - 实现它的方式。