所以我正在使用browsermob代理登录硒测试以通过Google Cloud的IAP。但这只会让用户进入该网站,他们仍然需要使用一些 firebase 登录表单登录该网站。IAP让我通过browsermob添加授权标头,以便您可以访问网站本身,但是当您尝试通过Firebase表单登录时,您会message: "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential.
401错误。
我以为我可以使用白名单或黑名单功能来解决这个问题,只是不将这些标头应用于与 firebase 登录相关的网址,但似乎白名单和黑名单只返回状态代码和与正则表达式匹配的呼叫的空响应。
有没有办法只传递基于主机的某些呼叫?或者万一我做错了什么,请告诉我。代码如下:
class ExampleTest(unittest.TestCase):
def setUp(self):
server = Server("env/bin/browsermob-proxy/bin/browsermob-proxy")
server.start()
proxy = server.create_proxy()
bearer_header = {}
bearer_header['Authorization'] = 'Bearer xxxxxxxxexamplexxxxxxxx'
proxy.headers({"Authorization": bearer_header["Authorization"]})
profile = webdriver.FirefoxProfile()
proxy_info = proxy.selenium_proxy()
profile.set_proxy(proxy_info)
proxy.whitelist('.*googleapis.*, .*google.com.*', 200) # This fakes 200 from urls on regex match
# proxy.blacklist('.*googleapis.*', 200) # This fakes 200 from urls if not regex match
self.driver = webdriver.Firefox(firefox_profile=profile)
proxy.new_har("file-example")
def test_wait(self):
self.driver.get("https://example.com/login/")
time.sleep(3)
def tearDown(self):
self.driver.close()
稍后想通了。BrowserMob 代理/客户端中没有任何内置的东西来执行此操作。但是您可以通过网络驱动程序的代理设置来实现它。
铬
self.chrome_options = webdriver.ChromeOptions()
proxy_address = '{}:{}'.format(server.host, proxy.port)
self.chrome_options.add_argument('--proxy-server=%s' % proxy_address)
no_proxy_string = ''
for item in no_proxy:
no_proxy_string += '*' + item + ';'
self.chrome_options.add_argument('--proxy-bypass-list=%s' % no_proxy_string)
self.desired_capabilities = webdriver.DesiredCapabilities.CHROME
self.desired_capabilities['acceptInsecureCerts'] = True
火狐
self.desired_capabilities = webdriver.DesiredCapabilities.FIREFOX
proxy_address = '{}:{}'.format(server.host, proxy.port)
self.desired_capabilities['proxy'] = {
'proxyType': "MANUAL",
'httpProxy': proxy_address,
'sslProxy': proxy_address,
'noProxy': ['google.com', 'example.com']
}
self.desired_capabilities['acceptInsecureCerts'] = True