e2e 使用量角器测试 angular2 应用程序时出错

Errors when e2e testing angular2 app with protractor

我正在尝试 运行 使用量角器对我的 Angular2 应用程序进行一些简单测试。我在一个命令提示符中 运行 ng serve,在另一个命令提示符中 ng e2e。我收到几个与会话异常相关的 errors/comments。这是其中的一部分:

ERROR:child_process_launcher.cc(536)] Failed to launch child process
...
I/direct - Using ChromeDriver directly...
I/launcher - Running 1 instances of WebDriver
E/launcher - session not created exception from tab crashed
(Session info: chrome=55.0.2883.87)
(Driver info: chromedriver=2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa
),platform=Windows NT 6.1.7601 SP1 x86_64)
  at WebDriverError (C:\development\SolarUI11\node_modules\protractor\node_mod
es\selenium-webdriver\lib\error.js:27:5)
  at SessionNotCreatedError (C:\development\SolarUI11\node_modules\protractor\
de_modules\selenium-webdriver\lib\error.js:308:5)
  at Object.checkLegacyResponse (C:\development\SolarUI11\node_modules\protrac
tor\node_modules\selenium-webdriver\lib\error.js:639:15)

我对 Angular2 和 e2e 测试还很陌生,所以我完全不知道我在这里看到的东西和我应该期待的东西。

app.po.ts:

import { browser, element, by } from 'protractor';

export class SolarUi4Page {
  navigateTo() {
    return browser.get('http://localhost:4200/');
  }

  getTitle() {
    return element(by.css('h1')).getText();
  }

  getAddButton() {
    return element(by.css('.icon-plus'));
  }

  getAddButtonDp() {
    return element(by.binding('dPoint.tDataPoint'));
  }

  getAddButtonIccp() {
    return element(by.binding('dPoint.tICCP'));
  }
  getAddButtonStartD() {
    return element(by.binding('dPoint.tStartDate'));
  }

  getAddButtonEndD() {
    return element(by.binding('dPoint.tEndDate'));
  }

  getAddButtonSubmit() {
    return element(by.id('addSubmit'));
  }

  getStartDate() {
    return element(by.id('calStartTest'));
  }

  getEndDate() {
    return element(by.id('calEndTest'));
  }

  getSearchButton() {
    return element(by.css('.icon-search'));
  }

  getTable() {
    return element(by.id('myTable'));
  }
}

app.e2e-spec.ts:

import { SolarUi4Page } from './app.po';
import { by } from 'protractor';

describe('solar-ui4 main page', function() {
  let page: SolarUi4Page;

  beforeEach(() => {
    page = new SolarUi4Page();
  });

  it('should display title Solar Project', () => {
    page.navigateTo();
    expect(page.getTitle()).toEqual('Solar Project');
  });

  it('should add new value to array/table and display it', () => {
    page.navigateTo();
    let addButton = page.getAddButton();
    addButton.click();
    let addDp = page.getAddButtonDp();
    let addIccp = page.getAddButtonIccp();
    let addSd = page.getAddButtonStartD();
    let addEd = page.getAddButtonEndD();
    addDp.sendKeys('newDp');
    addIccp.sendKeys('newIccp');
    addSd.sendKeys('11/12/2016');
    addEd.sendKeys('11/20/2016');
    let addSubmit = page.getAddButtonSubmit();
    addSubmit.click();
    let startFormVal = page.getStartDate();
    let endFormVal = page.getEndDate();
    startFormVal.sendKeys('11/02/2016');
    endFormVal.sendKeys('12/20/2016');
    let searchButton = page.getSearchButton();
    searchButton.click();
    let table = page.getTable();
    let lastRow = table.all(by.tagName('tr')).last();
    expect(lastRow.get(0).getText()).toEqual('newDp');
    expect(lastRow.get(1).getText()).toEqual('newIccp');
    expect(lastRow.get(2).getText()).toEqual('11/02/2016');
    expect(lastRow.get(3).getText()).toEqual('12/20/2016');
  })

protractor.conf.js:

// Protractor configuration file, see link for more information
// https://github.com/angular/protractor/blob/master/lib/config.ts

/*global jasmine */
var SpecReporter = require('jasmine-spec-reporter');

exports.config = {
  allScriptsTimeout: 11000,
  specs: [
    './e2e/**/*.e2e-spec.ts'
  ],
  capabilities: {
    'browserName': 'chrome'
  },
  directConnect: true,
  baseUrl: 'http://localhost:4200/',
  framework: 'jasmine',
  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 30000,
    print: function() {}
  },
  useAllAngular2AppRoots: true,
  beforeLaunch: function() {
    require('ts-node').register({
      project: 'e2e'
    });
  },
  onPrepare: function() {
    jasmine.getEnv().addReporter(new SpecReporter());
  }
};

如果我需要显示更多 errors/add 更多代码请告诉我。

这并不能解决 Chrome/chromedriver/webdriver-manager 的问题,但至少要使 运行 的端到端测试正确,请在 protractor.conf.js:

中更改浏览器
'browserName': 'firefox'

那么 Firefox 中的 e2e 测试至少会 运行。