【问题标题】:How to get query params from protractor APIs如何从量角器 API 获取查询参数
【发布时间】:2019-04-09 06:26:59
【问题描述】:

我正在我的 Angular 7 应用程序上执行 E2E 测试,其中我有一个测试用例,我从 UI 应用过滤器并且过滤器附加到作为查询参数

生成的 URL 如下所示:

http://localhost:4000/search?programme=1

但是当我使用browser.getCurrentUrl() 时,它只是返回http://localhost:4000/search 没有查询参数。

现在我正在使用browser.getLocationAbsUrl(),但量角器本身会抛出错误,它已不推荐使用

it('Select GV filter and check params', () => {
    searchPage.selectOnePrgram();
    expect<any>(browser.getLocationAbsUrl()).toContain('program=1'); 
   // [11:53:11] W/protractor - `browser.getLocationAbsUrl()` is deprecated, please use `browser.getCurrentUrl` instead.

});

我的问题是如何将查询参数放入我的.spec.ts 文件中?

它是在 E2E 范围内还是应该在单元测试中?

【问题讨论】:

    标签: angular protractor e2e-testing


    【解决方案1】:

    您只需使用从browser.getCurrentUrl() 获得的then() 管理承诺。

    示例代码:

      browser.getCurrentUrl().then(
        (res) => {
          console.log(res);
          // response will includes the url with query params
        }
      );
    

    【讨论】:

      【解决方案2】:

      使用 async / await 代替 Promise 链接。以下应该工作。 getCurrentUrl 返回一个承诺,需要解决。参考https://www.protractortest.org/#/api?view=webdriver.WebDriver.prototype.getCurrentUrl-

      it('Select GV filter and check params', async () => {
      await searchPage.selectOnePrgram();
      let currentLoc = await browser.getCurrentUrl();
      expect(currentLoc).toContain('program=1'); 
      

      });

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-24
        • 1970-01-01
        • 2017-10-28
        • 1970-01-01
        • 2018-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多