我正在尝试获取当前URL并将其存储为字符串,但是。然后总是返回未定义的URL。以下是功能代码:

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"

最佳答案

整个行为和代码执行流是异步的,由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);
    }
});

关于typescript - Protractor :browser.getCurrentUrl()。then((url)=> {return url;});总是返回未定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41640846/

10-11 23:45