0

I am trying to access the data-hook attribute using protractor-helpers(var helpers = require('protractor-helpers'), but for some reason I am getting the below error:

[12:06:50] E/launcher - Error: ReferenceError: beforeEach is not defined
    at C:\Users\xyz\node_modules\protractor-helpers\dist\protractor-helpers.js:258:2
    at Object.<anonymous> (C:\Users\xyz\node_modules\protractor-helpers\dist\protractor-helpers.js:498:3)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\Users\xyz\ProtractorWorkspace\Protractor\pos\Test.js:11:15)

My conf.js file is as below

exports.config = {

  seleniumAddress: 'http://localhost:4444/wd/hub',

  specs: [
    'features/sample.feature'
  ],

  capabilities: {
    'browserName': 'chrome'
  },

  framework: 'custom',
  frameworkPath: require.resolve('protractor-cucumber-framework'),

  cucumberOpts: {
    require: 'features/*.js',
    format: 'pretty'
  }

};

Can somebody please help.

chandra
  • 1
  • 4
  • Looks like that package is made for Jasmine considering the source code has `beforeEach(function () { jasmine.addMatchers({ ...`. Doesn't look like it supports cucumber, but I'm not positive on that. https://github.com/wix/protractor-helpers/blob/master/src/matchers.js – Gunderson Mar 24 '17 at 17:13
  • Can you also show your test? – nilesh Mar 24 '17 at 17:30
  • it is failing in the require statement , below is code snippet. var clickFn = require('../common/ClickFunctions.js'); var SelectDropDown = require('../common/DropDownFunctions.js'); var helpers = require('protractor-helpers'); var startQuote = function(){ } – chandra Mar 24 '17 at 17:47

1 Answers1

0

If you are using protractor-jasmine framework(which is the default framework in protractor) you should use jasmine's hooks -beforeEach, beforeAll etc.

If you are using protractor-cucumber framework you should cucumberjs's hooks/event-handlers - beforeScenario, beforeFeature etc.

For protractor -jasmine hooks please have a look at this link - jasmine hooks

I see that you are using protractor-cucumber-framework, so I would be writing here on how to use the cucumberjs built in hooks.

Firstly you would need cucumberjs node module in your project along with protractor-cucumber-framework module. npm install -D cucumber(I prefer it as a dev-dependency)

Current version of cucumber is 2.0(rc-8), its syntax is totally different from older versions. So if you want to launch browser url before every scenario you would have to do something like this -

/* Cucumber 2.0 syntax **/

var {defineSupportCode} = require('cucumber');

defineSupportCode(function({Before}) {

Before(function () {
browser.get(url);
 });

// You can also specify cucumber tags for scenarios, This hook will be executed before scenarios tagged with @foo
Before({tags: "@foo"}, function () {
browser.get(url)
});
});

You could find whole list examples and hooks here - cucumber hooks

You could checkout my repo's for more examples-

Community
  • 1
  • 1
Ram Pasala
  • 4,931
  • 3
  • 16
  • 26
  • Thanks Ram for the help, but it was not reagarding cucumber hooks , it is regarding data-hook attribute in the dom, please see the below DOM content – chandra Mar 27 '17 at 11:45
  • Thanks Ram for the help, but it was not regarding cucumber hooks , it is regarding data-hook attribute in the dom, please see the below DOM content having select tag, i want to select as 'element(by.dataHook('sometext')).click()' for this we need to use npm package 'protractor-helpers' as briefed in the below link, but iam getting the error when 'var helpers' code is encountered. https://www.npmjs.com/package/protractor-helpers – chandra Mar 27 '17 at 11:52
  • There is no element finder with `by.dataHook` instead you should use css selectors in order to find such elements. try - `element(by.css('select[data-hook="quoting.finance.product"]')).click()` , you can also use `by.cssContainingText` checkout this link on how to deal with select attrubutes in protractor - http://stackoverflow.com/questions/19599450/how-to-select-option-in-drop-down-protractorjs-e2e-tests – Ram Pasala Mar 27 '17 at 12:16
  • Thanks very much for that ram , this was of very good help , by any chance we can use npm package 'protractor-helpers' https://www.npmjs.com/package/protractor-helpers for cucumber framework?, if yes then it would have been good. – chandra Mar 27 '17 at 13:57
  • I am not sure as I have not used them with cucumber! – Ram Pasala Mar 28 '17 at 08:22