在 browser.get 之前设置 cookie [英] Setting cookies before browser.get
问题描述
我们的 (PHP) 应用程序需要设置某些 cookie 才能加载 Angular.js 客户端应用程序.如果未设置 cookie,则会引发异常并显示错误页面.
Our (PHP) application requires certain cookies to be set in order to load an Angular.js client app. If the cookies are not set an exception is thrown and error page is shown.
这意味着为了运行 E2E 测试,我们需要设置 cookie,但以下失败,因为 Protractor 试图在 browser.get
调用之后立即找到 Angular(它不存在,因为异常被抛出).
This means in order to run E2E tests we need to set the cookies, but the following fails because Protractor is trying to find Angular right after the browser.get
call (it's not there because the exception was thrown).
browser.get('http://' + domain + '/');
browser.manage().addCookie('foo', 'boo', '/', domain);
我尝试在设置 cookie 后调用 browser.get
:
I tried to call browser.get
after setting the cookies:
browser.manage().addCookie('foo', 'boo', '/', domain);
browser.get('http://' + domain + '/');
但这会产生以下错误:
未能在文档"上设置cookie"属性:Cookie 是在数据:"网址中禁用.
Failed to set the 'cookie' property on 'Document': Cookies are disabled inside 'data:' URLs.
有没有办法处理这种情况?也许是告诉 Protractor 在第一次 browser.get
调用时不要检查 Angular,或者在调用获取 URL 之前以某种方式为我们的域设置 cookie?
Is there a way how to handle this situation? Perhaps to tell Protractor not to check for Angular when doing the first browser.get
call or somehow set cookies for our domain before calling getting the URL?
推荐答案
我在量角器入门 doc:
browser.driver.get('http://' + domain + '/');
browser.manage().addCookie('foo', 'boo', '/', domain);
注意 browser.driver.get
而不是 browser.get
.这将阻止 Protractor 寻找 Angular 应用程序,并且可以设置 cookie.然后我在 it
语句中使用另一个 browser.get
.
Note the browser.driver.get
instead of browser.get
. This will prevent Protractor looking for the Angular app and cookies can be set. I then use another browser.get
inside of it
statement.
这篇关于在 browser.get 之前设置 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!