'no method expect' when using Protractor as a library

左心房为你撑大大i 提交于 2019-12-12 17:06:54

问题


Using Protractor as a library

Unable to require a reference to Jasmine. Referencing the expect method returns output Cannot call method 'expect' of null.

Code updated to reflect comments:

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
        });
});

See https://github.com/angular/protractor/issues/21 for related information.


回答1:


The global jasmine expect function is newly generated every time you step into jasmine's it function context.

What does that mean to your code ? You cannot call expect outside of the it function.

example:

...
  // 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); 
  });
})



回答2:


Quoting this post of Julie:

To make Jasmine automatically understand async testing, you'll need to require the jasmine-wd adapter with:

require('protractor/jasminewd');

(Just add the above line right after ... = require('protractor');.)



来源:https://stackoverflow.com/questions/20979627/no-method-expect-when-using-protractor-as-a-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!