在使用页面对象的量角器测试中找不到使用定位器错误的元素

时间:2017-06-15 14:18:33

标签: user-interface typescript testing protractor

app.e2e.test.ts

import { browser } from 'protractor';
import { App } from '../pageobjects/App'
const BASE_URL = 'http://localhost:1344';

describe('App', () => {

  let app: App;

  beforeEach(() => {
    browser.get(BASE_URL).then(function() {
      app = new App();
    });
  });

  it('should verify text on page', () => {
    browser.sleep(5000);
    console.log('after 1st sleep');
    app.name.getText().then(function(text: any) {
      browser.sleep(5000);
      console.log('text: ' + text);
    });
  });
});

app.ts

import { $ } from 'protractor';
export class App {

  public name: any;

  constructor() {
    console.log('constructor');
    this.name = $('#name');
  }
}

app.component.html

<h1 id="name">text from element</h1>

输出:

[15:13:03] I/hosted - Using the selenium server at http://localhost:4444/wd/hub
Started
constructor
after 1st sleep
F

Failures:
1) App should verify text on page
  Message:
    Failed: No element found using locator: By(css selector, #name)
  Stack:
    NoSuchElementError: No element found using locator: By(css selector, #name)

1 个答案:

答案 0 :(得分:0)

由于您使用快捷方式$表示法来访问该元素,因此您需要通过css选择器访问它。你的定位器应该是这样的

import { $ } from 'protractor';
export class App {

   public name: any;

   constructor() {
     console.log('constructor');
     this.name = $('h1#name');
   }
  }