0

I am using protractor 5.2.2. and cucumber 3.2.0.I am getting an error "browser is not defined" when i am run cucumber-js.

Feature: Login page test
Scenario: Verify whether the user is able to navigating to the login page

When I go to "https://in.linkedin.com/" 

and my step code is

var {defineSupportCode} = require('cucumber');
defineSupportCode(function ({ setDefaultTimeout, Given, When, Then }) {
setDefaultTimeout(60 * 1000);

When(/^I go to "(.*)"$/, function (url, callback) { 
browser.get(url).then(callback); 
});
)};

It looks like cucumber is not catching the global browser variable.

Devleena
  • 453
  • 9
  • 25

1 Answers1

1

To run protractor script, you need to use command like protractor conf.js no matter which test framework(jasmine, cucumber) you used.

When use cmd protractor to start running, it will load browser into Nodejs runtime's global variable.

After protractor complete load browser into global, the package protractor-cucumber-framework will generate and execute another command line which will use cucumber-js to run cucumber feature files, but now in the Nodejs runtime, global variable has browser this property and its value is not null/undefined.

That's why we have to need more two packages: cucumber and protractor-cucumber-framework

yong
  • 13,357
  • 1
  • 16
  • 27
  • Thanks for the reply.Actually i am trying to generate cucumber html report.The document which am following tells that CucumberJS module converts Cucumber's JSON format to HTML reports.So i have tried with below command. "cucumberjs test/features/ -f json:test/report/cucumber_report.json".it is generating the json report but scenario fails with browser is not defined. – Devleena Jan 22 '18 at 04:59
  • cucumberjs not supply any options for generating html report. it means you can't generate html report by cucumberjs cmmand line. The command you used actually to execute script and generate a JSON report after all scenarios run done. For most user, they choose to use another package `cucumber-html-reporter` to genereate HTML report after the JSON report generated. https://www.npmjs.com/package/cucumber-html-reporter – yong Jan 22 '18 at 07:26
  • Thanks for your guidance. I have installed cucumber-html-reporter and run "node_modules\.bin\cucumber-js F:\automation\features -f json:F:\automation\test\report\cucumber_report.json" in the command prompt ,the json file created but scenario fails with an error 'ReferenceError: browser is not defined'.And when i am executing node.js script,its generate a report of failed scenarios. – Devleena Jan 22 '18 at 09:03
  • As I had mentioned, you can't use `cucumberjs` command to execute Protractor script, that's the reason you get the ReferenceError. You need to use Cucumber.Listener.JsonFormater to generate JSON report file, and use Cucumber AfterFeatures hook to generate HTML report based on the JSON report. Please see this post: https://stackoverflow.com/questions/46695234/how-to-create-selenium-cucumber-html-reports-with-node-js/48021064#48021064 – yong Jan 22 '18 at 09:04
  • 1
    I upload a example project which run protractor script, generate Cucumber JSON and HTML report at github: https://github.com/yongzoux0229/study/tree/master/protractor-cucumber2, clone code to your local and `cd protractor-cucumber2`, then execute `npm install`, then execute `npm run test`, you will get HTML report in reports/report folder – yong Jan 22 '18 at 10:18
  • Great thanks yong..its working in cucumber 2.3.0.but not in cucumber 3.2.0. Currently cucumber 3.2.0 not supporting this? – Devleena Jan 23 '18 at 04:34
  • 1
    Yes, cucumber 3 has a big change in design: `defineSupportCode is deprecated. Require/import the individual methods instead`, detail at:https://github.com/cucumber/cucumber-js/blob/master/CHANGELOG.md#breaking-changes – yong Jan 23 '18 at 05:00
  • And cucucmber 3 removed the registerHandler and registerListener. You can pratice to change for cucumber 3, here is the Cucumber 3 Hook API: https://github.cohttps://stackoverflow.com/users/8096045/devleenam/cucumber/cucumber-js/blob/master/docs/support_files/hooks.md – yong Jan 23 '18 at 05:06
  • Thanks for mentioning https://github.com/cucumber/cucumber-js/blob/master/docs/support_files/hooks.md – Devleena Jan 23 '18 at 06:39