javascript 如何使用量角器处理基本身份验证?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27234785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:12:00  来源:igfitidea点击:

How to handle basic authentication with protractor?

javascriptjasmineprotractor

提问by andrepm

I'm trying protractor to write a few tests in a non angular application. I have to login in a page trough basic authentication in google chrome, but i have no idea how.

我正在尝试量角器在非角度应用程序中编写一些测试。我必须在谷歌浏览器中通过基本身份验证登录页面,但我不知道如何。

Authentication box

认证箱

I already tried baseUrl: 'https://username:password@url'and capabilities: { 'browserName': 'chrome', 'chromeOptions' : { args: ['--login-user=foo', '--login-password=bar'] } }

我已经尝试过baseUrl: 'https://username:password@url'capabilities: { 'browserName': 'chrome', 'chromeOptions' : { args: ['--login-user=foo', '--login-password=bar'] } }

But none if these worked for me. Anyone knows how to do it? I'm having some hard time on it.

但如果这些对我有用,那就没有了。有谁知道怎么做?我对此有些困难。

采纳答案by hankduan

The short answer is there is no easy way of doing it on chrome because they do not support modifying request headers -- see https://code.google.com/p/selenium/issues/detail?id=141(title says response headers, but if you read it, it's for all headers).

简短的回答是在 chrome 上没有简单的方法,因为它们不支持修改请求标头 - 请参阅https://code.google.com/p/selenium/issues/detail?id=141(标题表示响应标头,但如果您阅读它,它适用于所有标头)。

That being said, there are ways to do it, albeit difficult.

话虽如此,但还是有办法做到的,尽管很困难。

1) Find a chrome extension/plugin that allows you to modify header. A simple search bring up many of them: https://chrome.google.com/webstore/search/modify%20header. You'll need to add the plugin to webdriver: see Is it possible to add a plugin to chromedriver under a protractor test?.

1) 找到允许您修改标题的 chrome 扩展/插件。一个简单的搜索就可以找到其中的许多:https: //chrome.google.com/webstore/search/modify%20header。您需要将插件添加到 webdriver:请参阅是否可以在量角器测试下向 chromedriver 添加插件?.

2) You can use browsermob-proxy (https://github.com/lightbody/browsermob-proxy); this way you route your traffic through the proxy, which would add the headers for you. From the docs:

2) 您可以使用 browsermob-proxy ( https://github.com/lightbody/browsermob-proxy);通过这种方式,您可以通过代理路由流量,代理会为您添加标头。从文档:

POST /proxy/[port]/auth/basic/[domain] - Sets automatic basic authentication for the specified domain
Payload data should be json encoded username and password name/value pairs (ex: {"username": "myUsername", "password": "myPassword"}

There's a node project that may help you, https://github.com/zzo/browsermob-node, but you would still need to set up your proxy server yourself.

有一个节点项目可以帮助您,https://github.com/zzo/browsermob-node,但您仍然需要自己设置代理服务器。

Both ways for chrome are complex, but would get you what you want. (or you can stick with firefox and follow Robert's answer)

chrome 的两种方式都很复杂,但都能满足您的需求。(或者你可以坚持使用 Firefox 并遵循罗伯特的回答)

回答by Nuno Ferreira

In fact you can just set the url as http://username:[email protected]. Chrome will handle it!

事实上,您可以将 url 设置为http://username:[email protected]。Chrome会处理它!

回答by Robert

It's because Firefox doesn't trust any site by default with sending the windows auth info over. Even if you change it in the configurations manually, it won't affect protractor because it opens Firefox with an isolated configuration each time you run your end to end tests.

这是因为 Firefox 默认不信任任何发送 Windows 身份验证信息的站点。即使您在配置中手动更改它,它也不会影响量角器,因为每次运行端到端测试时,它都会以隔离的配置打开 Firefox。

You'll need to programatically set up a Firefox profile and set its preferences such that it would trust localhost (or some other website, depending where the pages are loaded from)

您需要以编程方式设置 Firefox 配置文件并设置其首选项,以便它信任本地主机(或其他网站,具体取决于页面加载的位置)

First, check out this example. It shows how you can set up the profile and how you can set preferences.

首先,看看这个例子。它显示了如何设置配置文件以及如何设置首选项。

https://github.com/juliemr/protractor-demo/tree/master/howtos/setFirefoxProfile

https://github.com/juliemr/protractor-demo/tree/master/howtos/setFirefoxProfile

What it does is that it modifies the homepage for each new tab. In the same manner (with the firefoxProfile.setPreference method) you can change the preferences responsible for trusting websites. They're called "network.automatic-ntlm-auth.trusted-uris" and "network.negotiate-auth.delegation-uris". You'll need to set them both to "localhost". (Again, if they're at some other place, it's obviously that url)

它的作用是修改每个新标签的主页。以同样的方式(使用 firefoxProfile.setPreference 方法)您可以更改负责信任网站的首选项。它们被称为“network.automatic-ntlm-auth.trusted-uris”和“network.negotiate-auth.delegation-uris”。您需要将它们都设置为“localhost”。(同样,如果他们在其他地方,显然是那个网址)

That should fix it :)

那应该解决它:)

回答by Jan Molak

As of version 59 Chrome no longer supportsURLs with embedded credentials.

从 59 版开始,Chrome不再支持带有嵌入式凭据的 URL。

To work around this I wrote the authenticator-browser-extensionNode module, which might be useful if you're using Protractor, WebDriver.io or similar test runners.

为了解决这个问题,我编写了authenticator-browser-extensionNode 模块,如果您使用 Protractor、WebDriver.io 或类似的测试运行程序,它可能会很有用。

To use the module install it from npm:

要使用模块从 npm 安装它:

npm install --save-dev authenticator-browser-extension

And import in the protractor.conf.js:

并导入protractor.conf.js

const { Authenticator } = require('authenticator-browser-extension');

exports.config = {
    capabilities: {
        browserName: 'chrome',

        chromeOptions: {
            extensions: [
                Authenticator.for('username', 'password').asBase64()
            ]
        }
    },
}

Pro tip: remember not to commit your credentials with your code, consider using env variables instead.

专业提示:记住不要在代码中提交凭据,请考虑使用 env 变量。

Hope this helps!

希望这可以帮助!

Jan

回答by AsGoodAsItGets

If you're reading this in 2019, with Angular 7/8, consider this:

如果您在 2019 年使用 Angular 7/8 阅读本文,请考虑:

https://www.npmjs.com/package/authenticator-browser-extension

https://www.npmjs.com/package/authenticator-browser-extension

I find it much easier than the solutions suggested above.

我发现它比上面建议的解决方案容易得多。

回答by tkell

hankduan's browsermob-proxy solution worked for me on Chrome - but the latest revisions of browsermob are using a thing called littleproxy which does not support auth headers. Thusly I had to do browsermob-proxy -port 9090 --use-littleproxy false, which got things working.

hankduan 的 browsermob-proxy 解决方案在 Chrome 上对我有用 - 但 browsermob 的最新版本使用了一种叫做 littleproxy 的东西,它不支持身份验证标头。因此,我不得不做browsermob-proxy -port 9090 --use-littleproxy false,这让事情发挥作用。

回答by RamanS

You may use Windows Credentials Manager to avoid this pop-up being constantly shown on every attempt to log in.

您可以使用 Windows 凭据管理器来避免每次尝试登录时不断显示此弹出窗口。

Add your credentials to the 'Generic' category there, restart browser (including background apps running).

将您的凭据添加到那里的“通用”类别,重新启动浏览器(包括正在运行的后台应用程序)。

Some explanation I currently have: this pop-up is not 'browser' specific, it is 'in the middle', between browser and domain credentials verification. Thus browser features (save password, autofill) do not work completely. By the same reason Protractor / Selenium etc. do not have complete control over that pop-up - it is by design of the domain authentication.

我目前有一些解释:这个弹出窗口不是“浏览器”特定的,它是“中间”,介于浏览器和域凭据验证之间。因此浏览器功能(保存密码、自动填充)不能完全工作。出于同样的原因,量角器/硒等无法完全控制该弹出窗口 - 这是域身份验证的设计。

As not completely sure if it is the only reason there are some other hints: - you may also need to add your site to the IE (IE, not Chrome) list of trusted sites (Chrome grabs information from there); - check "Automatic logon with current user name and password" in IE (not Chrome) - may not work if credentials you are using for the site are different from those you use to login to the machine.

由于不完全确定这是否是其他提示的唯一原因: - 您可能还需要将您的站点添加到 IE(IE,而不是 Chrome)受信任站点列表中(Chrome 从那里获取信息);- 在 IE(不是 Chrome)中选中“使用当前用户名和密码自动登录” - 如果您用于站点的凭据与用于登录计算机的凭据不同,则可能无法使用。