找不到定位器量角器

时间:2017-09-19 19:12:20

标签: javascript angular protractor e2e-testing

我有问题。在app.e2e-spec.ts中,它将我移动到登录页面。文件中有方法, 它称之为。当您单击SignIn按钮时,它将返回仪表板页面的标题。好像登录失败了。但是,他收到了消息:

should be able to login
- Failed: Timed out waiting for asynchronous Angular tasks to finish after 11 seconds. This may be because the current page is not an Angular application. Please see the FAQ for more details: https://github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular
While waiting for element with locator - Locator: By(css selector, h2)

在此页面上,元素如下所示:

<h2 _ngcontent-c5 = ""> Dashboard API </h2>

app.e2e-spec.ts

it('should show login page', () => {
page.navigateToHome();
page.maximizeWindow();
expect(page.getLoginBoxTitle()).toEqual('Sign In');
});

it('should be able to login', () => {
page.navigateToHome();
page.login();
expect(page.getPageTitle()).toEqual('Dashboard API');
page.navigateToDashboard();
});

app.po.ts

login() {
element(by.name('username')).sendKeys('test@test.en');
element(by.name('password')).sendKeys('test123');
element(by.buttonText('SignIn')).click();
}

loginIfNotLoggedIn() {
if (element.all(by.css('.login-box')).length > 0) {
  this.login();
} else {
  console.info('Already logged in');
 }
}

getLoginBoxTitle() {
 return element(by.css('h2')).getText();
}

getPageTitle() {
return element
  .all(by.css('h2'))
  .last()
  .getText();
}

dashboard.e2e-spec.ts

it('should get api status', () => {
 login.browserSleep();
 page.navigateToDashboard();
 login.loginIfNotLoggedIn();
 page.getStatusAPI();
 page.navigateToApplications();
});

1 个答案:

答案 0 :(得分:0)

在量角器5和角度2/4之后,量角器中的异步行为发生了变化。

您需要使用async ans await表单告诉angular在开始测试之前等到角度加载到浏览器中

async login(name: string, pwd: string) {
    await this.username.sendKeys(name);
    await this.password.sendKeys(pwd);
    await this.loginButton.click();
    return browser.wait(protractor.ExpectedConditions.not(protractor.ExpectedConditions.urlContains('returnUrl')));
}

然后在调用login方法时,您应该说await login()