量角器:browser.getCurrentUrl()。then((url)=> {return url;});总是返回undefined

时间:2017-01-13 18:06:13

标签: typescript protractor

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

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"

1 个答案:

答案 0 :(得分:4)

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

if (currentURL !== 'operator') {

相反,在回调中工作

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