【问题标题】:How to handle HTTP Basic authentication on Chrome by Protractor?如何通过 Protractor 在 Chrome 上处理 HTTP Basic 身份验证?
【发布时间】:2015-06-11 22:53:44
【问题描述】:

谁能帮我处理“HTTP 基础”

Chrome 上的身份验证提示?

我无法通过常规访问我们的暂存环境的身份验证

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

你知道如何处理它吗?
. . .

技术信息:

编程语言:JavaScript

框架:Jasmine JS

浏览器:谷歌浏览器(最新)

平台: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');
});

【问题讨论】:

标签: node.js google-chrome selenium-webdriver automation http-authentication


【解决方案1】:

似乎没有一个简单的方法:

How to handle basic authentication with protractor?.

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 谢谢,但我听说 JS 不支持原生浏览器操作。在 FireFox 中,可以通过安装 autoAuth 插件来解决这个问题。但是,我也需要谷歌浏览器的类似解决方案..
    • 这是 Selenium 库,你应该在 JS Binding of Selenium 中也有类似的东西(IMO)。
    【解决方案3】:

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

    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 步骤定义。下面的代码是小黄瓜、黄瓜和 webdriver,但您将能够看到它在您的情况下是如何工作的。

    小黄瓜.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 身份验证的要求,您可以将前面的步骤注释掉。此外,最好先在场景或功能中定义路径,因为它可以为您提供有关功能或场景位置的上下文。

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        如果您在 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 - 可能还有其他测试框架 - 实现它的方式。

        【讨论】:

          猜你喜欢
          • 2016-05-14
          • 1970-01-01
          • 2013-10-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多