Protractor 量角器+;Mocha在浏览器加载SUT之前导致套件失败,出现TypeError 上下文

Protractor 量角器+;Mocha在浏览器加载SUT之前导致套件失败,出现TypeError 上下文,protractor,mocha.js,integration-testing,angular-cli,Protractor,Mocha.js,Integration Testing,Angular Cli,我正在探索angular2+AngularCLI+typescript。我的目标是确保如果我在typescript中使用angular应用程序或节点应用程序,我将使用与使用mocha的遗留节点应用程序相同的测试技术。为此,我正在尝试重新配置angular cli生成的dragrator.conf.js以使用mocha而不是jasmine 问题: 如何正确集成angular cli+mocha+量角器,以便使用量角器执行测试,量角器实际上提供了mocha规格有用的浏览器/元素/组件? 我已经将量角

我正在探索angular2+AngularCLI+typescript。我的目标是确保如果我在typescript中使用angular应用程序或节点应用程序,我将使用与使用mocha的遗留节点应用程序相同的测试技术。为此,我正在尝试重新配置angular cli生成的
dragrator.conf.js
以使用mocha而不是jasmine

问题: 如何正确集成angular cli+mocha+量角器,以便使用量角器执行测试,量角器实际上提供了mocha规格有用的浏览器/元素/组件?

我已经将
量角器.conf.js
更改为使用mocha和chai,测试完成,但是与量角器组件的所有交互都失败,即
元素(by.css('app-root h1')).getText()

转换的量角器.conf.js
exports.config={
allScriptsTimeout:11000,//在浏览器上运行脚本的超时。
规格:[
'./e2e/***.e2e-spec.ts'//测试规范的模式
],
baseUrl:'http://localhost:4200/“,//SUT的基本url
能力:{
'browserName':'chrome'//要使用的浏览器
},
directConnect:true,//selenium不需要服务器,直接连接到chrome
框架:'摩卡',//用摩卡代替茉莉花
mochaOpts:{//Mocha特定选项
记者:“规格”,
慢:3000,
用户界面:“bdd”,
超时:30000
},
启动前:函数(){//执行所有类型脚本
需要('ts-node')。寄存器({
项目:“e2e/tsconfig.e2e.json”
});
},
onPrepare:function(){}
};
怀疑 感觉问题在于,在浏览器加载应用程序之前,套件正在执行(并且失败)。这可能是量角器/浏览器交互,据我所知,它与所使用的规范无关。也许这种理解是错误的

示例源 我有一个在GitHub上运行的示例,我正在使用它来测试和比较转换

到目前为止的调查 在命令行
node node\u modules\dragrator\bin\dragrator-dragrator.conf.js--stackTrace--trouble
上运行量角器(通过节点)显示套件配置并运行了
truthy
测试、跳过的测试和实际的应用程序测试

套件结果 我们看到truthy测试通过了,跳过的测试被跳过了,应用程序测试都失败了,出现了相同的错误

1 passing 
1 pending
5 failing

 1) A descriptive test name that is irrelevant to the error:
     TypeError: obj.indexOf is not a function
      at include (node_modules\chai\lib\chai\core\assertions.js:228:45)
      at doAsserterAsyncAndAddThen (node_modules\chai-as-promised\lib\chai-as-promised.js:293:29)
      at .<anonymous> (node_modules\chai-as-promised\lib\chai-as-promised.js:271:25)
      at chainableBehavior.method (node_modules\chai\lib\chai\utils\overwriteChainableMethod.js:51:34)
      at assert (node_modules\chai\lib\chai\utils\addChainableMethod.js:84:49)
      at Context.it (e2e\search\search.e2e-spec.ts:14:40)
      at runTest (node_modules\selenium-webdriver\testing\index.js:166:22)
      at node_modules\selenium-webdriver\testing\index.js:187:16
      at new ManagedPromise (node_modules\selenium-webdriver\lib\promise.js:1067:7)
      at controlFlowExecute (node_modules\selenium-webdriver\testing\index.js:186:14)
  From: Task: A descriptive test name that is irrelevant to the error
      at Context.ret (node_modules\selenium-webdriver\testing\index.js:185:10)
附加信息
有趣的是,如果您使用elementExplorer
node node\u modules\dragrator\bin\dragrator-dragrator-gragrator.conf.js--stackTrace--trouble--elementExplorer
运行Gragrator,它将不会运行测试,但我们确实可以在启动的浏览器中看到SUT解析。这我无法解释

首先,我对摩卡咖啡不太熟悉。不过,我可以让你通过考试。这是您需要做的:

设置依赖项 有时使用最新和最大的依赖项是很好的。最新的
chai(如承诺的那样)
对我不起作用。我曾经尝试按照承诺和运行的问题,将量角器依赖项更新为最新版本的chai和chai。我不得不降低您的依赖性,并最终使用:

"chai": "~3.5.0",
"chai-as-promised": "~5.3.0",
这些版本与gragrator package.json的版本相同

使用onPrepare插件 在量角器运行测试之前,按照承诺设置chai:

onPrepare: function() {
  let chai = require('chai');
  let chaiAsPromised = require("chai-as-promised");
  chai.use(chaiAsPromised);
  global.chai = chai;
}
编辑测试: 添加或修改以下内容

附录e2e-spec.ts

import {RootPage} from './root/root.po';
let expect = global["chai"].expect;

  // change the following lines to have "eventually"
  expect(page.getParagraphText()).to.eventually.contain('Car search POC');

  // if you don't want to use "eventually"
  page.getParagraphText().then(paragraph => {
    expect(paragraph).to.contain('Car search POC');
  });
import {SearchPage} from './search.po';
let expect = global["chai"].expect;

  // change the following lines to have "eventually"
  expect(page.getParagraphText()).to.eventually.contain('Car search POC');
root.e2e-spec.ts:

let expect = global["chai"].expect;

describe('Home page', () => {

  // change the following lines to have "eventually"
  expect(page.getParagraphText()).to.eventually.include('Car search POC');
  expect(browser.getCurrentUrl()).to.eventually.include(homePage.uri());
import {RootPage} from './root.po';
import {HomePage} from '../home/home.po';
import {WaitCondition} from '../wait.conditions';
let expect = global["chai"].expect;

  // change the following lines to have "eventually"
  expect(page.getParagraphText()).to.eventually.equal('Car search POC');

  // This line will not work. getInnerHtml has been deprecated by both
  // Protractor and selenium-webdriver.
  //
  // If you want to use something similar, do something like:
  // let i = browser.executeScript("return arguments[0].innerHTML;", element(locator));
  // This is noted in the CHANGELOG under the Protractor 5.0.0 release
  expect(page.getListingContent()).to.exist;
home.e2e-spec.ts:

let expect = global["chai"].expect;

describe('Home page', () => {

  // change the following lines to have "eventually"
  expect(page.getParagraphText()).to.eventually.include('Car search POC');
  expect(browser.getCurrentUrl()).to.eventually.include(homePage.uri());
import {RootPage} from './root.po';
import {HomePage} from '../home/home.po';
import {WaitCondition} from '../wait.conditions';
let expect = global["chai"].expect;

  // change the following lines to have "eventually"
  expect(page.getParagraphText()).to.eventually.equal('Car search POC');

  // This line will not work. getInnerHtml has been deprecated by both
  // Protractor and selenium-webdriver.
  //
  // If you want to use something similar, do something like:
  // let i = browser.executeScript("return arguments[0].innerHTML;", element(locator));
  // This is noted in the CHANGELOG under the Protractor 5.0.0 release
  expect(page.getListingContent()).to.exist;
search.e2e-spec.ts

import {RootPage} from './root/root.po';
let expect = global["chai"].expect;

  // change the following lines to have "eventually"
  expect(page.getParagraphText()).to.eventually.contain('Car search POC');

  // if you don't want to use "eventually"
  page.getParagraphText().then(paragraph => {
    expect(paragraph).to.contain('Car search POC');
  });
import {SearchPage} from './search.po';
let expect = global["chai"].expect;

  // change the following lines to have "eventually"
  expect(page.getParagraphText()).to.eventually.contain('Car search POC');
控制台输出 这是我运行测试的结果。请注意,“将包含内容”失败,因为
getInnerHtml()
不是有效的

angular-cli-seed App
  ✓ will do normal tests
  ✓ will display its title

Home page
  ✓ will display its title
  - will have content

root page
  ✓ will display its title
  ✓ will redirect the URL to the home page

search page
  ✓ will display its title


6 passing (5s)
1 pending

这是一个有趣的问题。这很容易回答,因为你包括了一个分支什么是不工作的。快乐测试

你为什么要特别使用摩卡量角器?我自己也遇到过这种情况,但后来我意识到我试图一次做得太多了。Mocha非常适合进行单元测试和API测试,而量角器应该仅限于“用户体验”测试。我同意最简单的事情就是不要麻烦,把它留给Jasmine。然而,在mocha中测试node+typescript中的应用程序,在mocha中测试angular2应用程序和单元测试,然后在e2e中使用Jasmine会很奇怪。理想情况下,我希望能够保持所有数据的一致性,而且显然它应该是有效的。因此,我有一些(很少)使用Digrator+mocha的用例。我的用例是当我需要在测试环境中将某些令牌添加到我的头中时。我仍然把我的测试分开。在我的package.json中,我有一行用于
“scripts”:{“test”:“grandor-grandor-conf.js&&mocha”}
。然后我可以运行
npm test
(顺便说一句,不要试图设置环境变量并期望它们被共享……每个测试都会产生一个新的proc id)@LostJon interest。看起来您已将
ng test
调用分别替换为对量角器和摩卡的调用。您是否曾经能够让量角器使用摩卡规范运行整个过程,或者仅使用量角器和摩卡独立运行?它认为这种方法将打破
nge2e
调用。我是否正确理解了片段?不确定
ng test
来自何处,但是
npm test
只是运行测试用例的默认设置。您可以定义脚本,如
{“grandor-test”:“grandor-grandor-conf.js”,“mocha-test”:“mocha”
然后运行
npm运行量角器测试
npm运行摩卡测试
。所有偏好都是真的,但在我上面的评论中,量角器执行我的摩卡套件。为了在两者之间共享数据,我在量角器中创建了一个tmp文件,并在摩卡中清理它。谢谢!我不认为我很高兴看到套件通过测试很长时间了。我已经将您的更改应用为