如何用量角器处理基本认证? [英] How to handle basic authentication with protractor?
问题描述
我正在尝试量角器在非角度应用程序中编写一些测试。我必须通过谷歌浏览器中的基本身份验证登录页面,但我不知道如何。
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.
我已经尝试过 baseUrl:'https:// username:password @ url '
和功能:{
'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.
推荐答案
简短的回答是没有简单的方法在Chrome上做这件事,因为他们不支持修改请求标头 - 请参阅 https:// code。 google.com/p/selenium/issues/detail?id=141 (标题显示响应标题,但如果您阅读它,那么它适用于所有标题)。
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).
话虽如此,有办法做到这一点,虽然很难。
That being said, there are ways to do it, albeit difficult.
1)找一个允许你修改标题的chrome扩展/插件。一个简单的搜索带来了很多: https://chrome.google.com/webstore /搜索/修改%20header 。您需要将插件添加到webdriver:请参阅是否可以在量角器测试下向chromedriver添加插件?。
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?.
2)您可以使用browsermob -proxy( https://github.com/lightbody/browsermob-proxy );这样您就可以通过代理路由流量,这会为您添加标头。
来自文档:
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:
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"}
有一个节点项目可以帮助你, https://github.com/zzo/browsermob-node ,但您仍需要自己设置代理服务器。
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.
chrome的两种方式都很复杂,但可以得到你想要的东西。(或者你可以坚持使用firefox并遵循Robert的回答)
Both ways for chrome are complex, but would get you what you want. (or you can stick with firefox and follow Robert's answer)
这篇关于如何用量角器处理基本认证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!