2

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.

Dan Hartley
  • 21
  • 1
  • 4

2 Answers2

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); 
  });
})
lgersman
  • 2,178
  • 2
  • 19
  • 21
0

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');.)

gkalpak
  • 47,844
  • 8
  • 105
  • 118
  • Thank you for your response. This has pointed me in the right direction. However, I now get the following error: jasmine is not defined at this line at protractor/jasminewd/index.js: `jasmine.getEnv().addReporter(new OnTimeoutReporter(function() { flow.reset(); }));` – Dan Hartley Jan 08 '14 at 08:29
  • HAve you added `require('protractor/jasminewd');` **after** `self.protractor = require('protractor');` ? – gkalpak Jan 08 '14 at 08:34
  • Is `expect` defined in the global scope ? (E.g. does adding `expect(true).toBe(true);` at the top of the specs file throw an error or not ?) – gkalpak Jan 08 '14 at 09:11
  • That's exactly my problem, expect is not defined in the global scope (nor any other jasmine method). As shown in the code example, 'expect is not defined'. This is also true for `expect(true).toBe(true);expect(true).toBe(true);`. Thank you again for your feedback. – Dan Hartley Jan 08 '14 at 09:22
  • Your example does **not** indicate `expect()` is not defined in the global scope (you are calling is a method of some other object, not the global object (e.g. `window`)). My question is if it is defined as a method of the global object. – gkalpak Jan 08 '14 at 09:31
  • Thanks again. I have updated the code. The global object is Object (this). There is no reference to window. However, your question seems to be on the right track - jasmine does need to be added to the global scope. – Dan Hartley Jan 08 '14 at 10:07
  • Try inserting `require('protractor/node_modules/minijasminenode');` before requiring `jasminewd`. – gkalpak Jan 08 '14 at 10:30
  • Still no joy! I have updated the code to reflect your comments to date. If I find a solution I will update again. Thanks – Dan Hartley Jan 08 '14 at 11:44
  • I believe your comments `Cannot call method 'expect' of null` are not true in the updated code. Nonetheless, if I understand correctly, even after requiring `minijasminenode` you get an error at `require('protractor/jasminewd');` ? – gkalpak Jan 08 '14 at 11:49
  • No, not quite, I do still receive the error `Cannot call method 'expect' of null` after requiring both `jasminewd` and `minijasminenode`. If I do not require `minijasminenode` (or `minijasminenode` and `jasminewd`), the error is `jasmine is undefined` – Dan Hartley Jan 08 '14 at 12:00
  • If you require both `minijasminenode` and `jasminewd` and remove my `expect(true).toBe(true);` line (just do you get an error at `expect(url).toContain('angular');`) ? _Cannot call method 'expect' of null_ ? – gkalpak Jan 08 '14 at 12:10
  • Yes, that is exactly what happens. – Dan Hartley Jan 08 '14 at 12:12
  • Sorry for the many question, but it doesn't make sense. In the updated code you call `expect()` on the global object. Getting an error like the one you say means that the global object is null (or if you are executing this inside a function, that the function's `this` object is set to null). This is strange... – gkalpak Jan 08 '14 at 12:16
  • I agree! And you're right, the this object is null inside the function. I've posted an issue [https://github.com/angular/protractor/issues/394] (https://github.com/angular/protractor/issues/394). Meanwhile, I'm going to start over. – Dan Hartley Jan 08 '14 at 12:28
  • Just a last attempt at it: What happens if you call `self.expect(...` instead ? – gkalpak Jan 08 '14 at 12:41
  • I tried that too ... no luck. I'll update when I have a solution. – Dan Hartley Jan 08 '14 at 13:48
  • @DanHartley: I noticed you closed your GitHub issue. Did you find a solution to this ? – gkalpak Jan 17 '14 at 18:50
  • No, I didn't and in fact in my case it is not required. If I do come across an answer I will update. – Dan Hartley Jan 18 '14 at 19:22
  • Hi Dan, I am getting issues when using Protractor as a library. I am getting connection timed-out issues. Please let me know you can help – nirvanastack Jun 11 '14 at 19:21