【问题标题】:Protractor: browser.getCurrentUrl().then ( ( url ) => { return url; } ); always returning undefined量角器:browser.getCurrentUrl().then ( ( url ) => { return url; } );总是返回未定义
【发布时间】:2017-01-14 02:09:42
【问题描述】:

我正在尝试获取当前 url 并希望将其存储为字符串,但 .then 始终返回未定义。以下是功能代码:

navigateToLandingPage() {
          let EC = protractor.ExpectedConditions;
          let currentURL: string;
          browser.getCurrentUrl().then ( (url) => {
            currentURL = url;
          } );
          if (currentURL !== 'operator') {
           browser.get('localhost:3000/#/operator');
           browser.wait(EC.urlContains('/operator'), 10000);
      }

我这样称呼它形式规范:

describe('Checking Module', () => {
  let page: CalendarModule;
  let EC = protractor.ExpectedConditions;
  beforeEach(() => {
    page = new CalendarModule();
  });
  it('Clicking on Calendar should redirect to Calendar Module', () => {
      page.commonMethodObj.navigateToLandingPage();
      let calendarLink = page.getCalendarLink();
       calendarLink.click().then(() => {
       browser.wait(EC.urlContains('/calendar'), 50000);
       expect(browser.getCurrentUrl()).toMatch('/calendar');
      });
  });
});

我正在使用以下版本的依赖项:

"@angular/core": "2.3.1",
"jasmine-core": "2.5.2",
"jasmine-spec-reporter": "2.5.0",
"protractor": "~4.0.13"

【问题讨论】:

  • calendarLink.click() 方法即可;但是,由于 Protractor 使这些异步调用看起来与 jasminewd 同步,因此不需要它。

标签: typescript protractor


【解决方案1】:

代码执行的整个行为和流程是异步的,并由WebDriver Control Flow 编排。换句话说,你不能指望currentURL 变量实际上会在这一行获得当前的 URL 值:

if (currentURL !== 'operator') {

相反,在回调中工作

browser.getCurrentUrl().then ( (currentURL) => {  
    if (currentURL !== 'operator') {
        browser.get('localhost:3000/#/operator');
        browser.wait(EC.urlContains('/operator'), 10000);
    }
});

【讨论】:

  • 是的,正确!但是如果我想从承诺中返回一些东西并在以后使用它怎么办。那怎么可能实现呢??
  • @ArjunSingh 太棒了,您可以使用延迟对象 (example)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 2019-05-21
  • 2016-02-15
相关资源
最近更新 更多