【问题标题】:Setting cookies before browser.get在 browser.get 之前设置 cookie
【发布时间】:2014-06-10 10:43:08
【问题描述】:

我们的 (PHP) 应用程序需要设置某些 cookie 才能加载 Angular.js 客户端应用程序。如果未设置 cookie,则会引发异常并显示错误页面。

这意味着为了运行 E2E 测试,我们需要设置 cookie,但以下失败,因为 Protractor 试图在 browser.get 调用之后立即找到 Angular(它不存在,因为引发了异常)。

browser.get('http://' + domain + '/');
browser.manage().addCookie('foo', 'boo', '/', domain);

我在设置cookies后尝试拨打browser.get

browser.manage().addCookie('foo', 'boo', '/', domain);
browser.get('http://' + domain + '/');

但这会产生以下错误:

未能在“文档”上设置“cookie”属性:Cookie 是 在“数据:”网址中禁用。

有没有办法处理这种情况?也许是告诉 Protractor 在第一次调用 browser.get 时不要检查 Angular,或者在调用获取 URL 之前以某种方式为我们的域设置 cookie?

【问题讨论】:

    标签: angularjs protractor angularjs-e2e


    【解决方案1】:

    我在Protractor Getting Started 文档中找到了解决方案:

    browser.driver.get('http://' + domain + '/');
    browser.manage().addCookie('foo', 'boo', '/', domain);
    

    注意browser.driver.get 而不是browser.get。这将阻止 Protractor 寻找 Angular 应用程序,并且可以设置 cookie。然后我在it 语句中使用另一个browser.get

    【讨论】:

    • 感谢您通过致电.manage() 编写有关如何使用 webdriver.WebDriver.Options 的示例。因为您提供的链接没有提供有关如何使用该特定 API 的任何线索。
    • 我发现量角器文档通常不完整或错误,这些示例非常有帮助。谢谢。
    • 对于那些在 2019 年看到这个的人,browser.manage().addCookie() 现在期待一个对象。 browser.manage().addCookie({name: 'foo', value: 'bar'})
    【解决方案2】:

    addCookie 使用较新的 Protractor 版本(使用 5.4.2 测试)采用 IWebDriverOptionsCookie 类型的对象。

    注意:要获取 cookie,请使用 getCookies

    示例:

    browser.manage().addCookie({ name: 'foo', value: 'bar' });
    

    IWebDriverOptionsCookie 文档:

    interface IWebDriverOptionsCookie {
      /** The name of the cookie. */
      name: string;
    
      /** The cookie value. */
      value: string;
    
      /** The cookie path. Defaults to "/" when adding a cookie. */
      path?: string;
    
      /**
       * The domain the cookie is visible to. Defaults to the current browsing
       * context's document's URL when adding a cookie.
       */
      domain?: string;
    
      /**
       * Whether the cookie is a secure cookie. Defaults to false when adding a new
       * cookie.
       */
      secure?: boolean;
    
      /**
       * Whether the cookie is an HTTP only cookie. Defaults to false when adding a
       * new cookie.
       */
      httpOnly?: boolean;
    
      /**
       * When the cookie expires.
       *
       * When adding a cookie, this may be specified in seconds since Unix epoch (January 1, 1970).
       * The expiry will default to 20 years in the future if omitted.
       *
       * The expiry is always returned in seconds since epoch when
       * retrieving cookies from the browser.
       *
       * @type {(!Date|number|undefined)}
       */
      expiry?: number | Date;
    }
    

    【讨论】:

      【解决方案3】:

      至于browser.driver.get() 返回一个承诺,你应该考虑到它:

      browser.driver.get('http://' + domain + '/').then(() => {
        browser.manage().addCookie({name: 'foo', value: 'boo'});
      });
      

      另请注意,在这种情况下,您不需要将域值传递给addCookie,因为量角器默认使用当前域。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-09
        • 2010-11-02
        • 1970-01-01
        • 2021-08-27
        • 2019-07-30
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多