2

使用Protractor 作为库

无法要求引用 Jasmine。引用该expect方法会返回 output Cannot call method 'expect' of null

更新代码以反映评论:

var protractor = require('protractor');
require('protractor/node_modules/minijasminenode');
require('protractor/jasminewd'); // output: jasmine is undefined (this error can only be seen if the above line is commented out)
//expect(true).toBe(true); // output: Cannot call method 'expect' of null

var driver = new protractor.Builder()
    .usingServer('http://localhost:4444/wd/hub')
    .withCapabilities(protractor.Capabilities
    .chrome()).build();

var ptor = protractor.wrapDriver(driver);

ptor.get('http://www.angularjs.org').then(function(){
    ptor.element(protractor.By.model('yourName')).sendKeys('test')
        .then(console.log('success')); // output: success
        ptor.getCurrentUrl().then(function(url){
            console.log(url); // output: http://www.angularjs.org
            expect(url).toContain('angular'); // output: Cannot call method 'expect' of null
        });
});

有关相关信息,请参阅https://github.com/angular/protractor/issues/21

4

2 回答 2

1

每次进入 jasmine 的 it 函数上下文时,都会新生成全局 jasmine expect 函数。

这对您的代码意味着什么? 您不能在 it 函数之外调用 expect 。

例子:

...
  // somewhere in your code
...
  // function expect doesn’t exist here
describe( 'your case', function() {
    // expect doesn’t exist here either
  it( 'should work', function() {
    // horray - the global expect is available !!
    // note : the expect function is generated before running your callback 
    // function to collect the expect'ed results for exactly this 'it' case
      expect( true).toBe( true); 
  });
})
于 2014-05-05T17:22:17.580 回答
0

引用朱莉的这篇文章

为了让 Jasmine 自动理解异步测试,你需要 jasmine-wd 适配器:

require('protractor/jasminewd');

(只需在 . 之后添加上述行... = require('protractor');。)

于 2014-01-07T19:15:24.853 回答