Discussion:
[Cucumber] How to pass values form the example table to the hook before method in nightwatch + cucumber?
Add Reply
Felipe Nava Covarrubias
2018-10-12 00:30:21 UTC
Reply
Permalink
Hey guys,

I have been working with cucumber + ruby a long time and now we are
migrating to nightwatch + cucumber. But I have been searching in a lot of
places how can I pass the values that are in my example table through my
hook before method.

So, the reason for this is because before of each execution I wants to do
some logic and I need to get data from my example table to be able to do
that.

Let's put this as an example:

*test.feature*

@something
Scenario Outline: Verify user can go all sites
Given A user visiting <site>

Examples:
| site |
| google.com |

| gmail.com |



*hooks.js*

const { Before, After } = require('cucumber');


Before(function (scenario, callback) {
console.log('Here I wants to know what are the values of my example table for this specific execution...');
});


As I put in the console.log, I would like to get in that place the value in
each execution of the parameter "sites". So that in the first execution
somehow I need to know that we are runing the test scenarios with the valur
"google.com" in the variable "sites", then in the next execution that would
be "gmail.com", and so on...

I have been checking in all the parameters that comes in the scenario but
not luck. If someone knows how to do this or knows any document/blog/post
that already found the answer for this, please just let know.

Thanks and appreciate your help!
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Andrew Premdas
2018-10-20 01:07:51 UTC
Reply
Permalink
Do you realise that all that example table does is create two separate
scenarios one with google.com and one with gmail.com. So you could just
write two scenarios and your problem would disappear

Scenario: Verify user can go to google
Scenario: Verify user can go to gmail

I've happily cuked without using example tables or outlines for many years
now. IMO all they do is complicate things.

All best

Andrew


On Wed, 17 Oct 2018 at 14:55, Felipe Nava Covarrubias <
Post by Felipe Nava Covarrubias
Hey guys,
I have been working with cucumber + ruby a long time and now we are
migrating to nightwatch + cucumber. But I have been searching in a lot of
places how can I pass the values that are in my example table through my
hook before method.
So, the reason for this is because before of each execution I wants to do
some logic and I need to get data from my example table to be able to do
that.
*test.feature*
@something
Scenario Outline: Verify user can go all sites
Given A user visiting <site>
| site |
| google.com |
| gmail.com |
*hooks.js*
const { Before, After } = require('cucumber');
Before(function (scenario, callback) {
console.log('Here I wants to know what are the values of my example table for this specific execution...');
});
As I put in the console.log, I would like to get in that place the value
in each execution of the parameter "sites". So that in the first execution
somehow I need to know that we are runing the test scenarios with the valur
"google.com" in the variable "sites", then in the next execution that
would be "gmail.com", and so on...
I have been checking in all the parameters that comes in the scenario but
not luck. If someone knows how to do this or knows any document/blog/post
that already found the answer for this, please just let know.
Thanks and appreciate your help!
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
------------------------
Andrew Premdas
blog.andrew.premdas.org
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Felipe Nava Covarrubias
2018-10-23 18:46:29 UTC
Reply
Permalink
Hey Andrew,

First, Thanks for your response.

So, Yeah I know what is actually doing and implement your approach is not
what Im looking for. Because that was just put a simple example of what Im
going to do. The reality is that I have to validate a bunch of different
domains which shares features between them. So, if I add one scenario per
site I'll end up with a lot of test scenarios, a lot of duplicated steps,
etc.

I have more than 30 different sites with different locales each of them
that I need to validate features that are shared across all of them. This
is a more realistic example:

log_in.feature

@something
Scenario Outline: Verify user can log in
Given A user visiting <site> with locale <locale>

Then the user can log in

And user verifies his profile picture

Examples:
| site | locale |
| google.com | en_US |

| google.com | es_MX |

| gmail.com | en_US |

| gmail.com | es_MX |



Common_definitions.js:


Given(/^A user visiting (.*) with locale (.*)$/, (site, locale) => {
process.env.siteUrl = site; //I do this so that other steps know in which url I am and can navigate to any specific url inside the same site. I'm using this env variable because my tests are in different files

//depending the feature, common shared steps, etc...


browser.navigate(site + '/clearSession.html');

.setLocale(locale); //here Im going to set a cookie to specify the locale of the page, So that any step that navigate to any page in the same siteUrl will have the same locale.


return browser;
});




So, this step is going to happen in all my test no matter if I have to
verify log in, log out or whatever other feature. And that's why I'm trying
to use the before method or maybe use a background step but I would need
also to get the value of my example table for the current execution.

Hooks.js

const { Before, After } = require('cucumber');


Before(function (scenario, callback) {
//what i want to do here is do the same than in the 1st step but for that I need to value of each row in the current execution.
});




This is and example of how my "real tests" are working. I have a step at
the begging of each scenario to set up everything needed to test my
features but as i said earlier I think the best approach to handle it is
using a before hook or a background step.

If you have any idea of how to pass those values to before method or to a
background step please let me know and again, thanks for your time of
checking an responding to my question.
Post by Andrew Premdas
Do you realise that all that example table does is create two separate
scenarios one with google.com and one with gmail.com. So you could just
write two scenarios and your problem would disappear
Scenario: Verify user can go to google
Scenario: Verify user can go to gmail
I've happily cuked without using example tables or outlines for many years
now. IMO all they do is complicate things.
All best
Andrew
On Wed, 17 Oct 2018 at 14:55, Felipe Nava Covarrubias <
Post by Felipe Nava Covarrubias
Hey guys,
I have been working with cucumber + ruby a long time and now we are
migrating to nightwatch + cucumber. But I have been searching in a lot of
places how can I pass the values that are in my example table through my
hook before method.
So, the reason for this is because before of each execution I wants to do
some logic and I need to get data from my example table to be able to do
that.
*test.feature*
@something
Scenario Outline: Verify user can go all sites
Given A user visiting <site>
| site |
| google.com |
| gmail.com |
*hooks.js*
const { Before, After } = require('cucumber');
Before(function (scenario, callback) {
console.log('Here I wants to know what are the values of my example table for this specific execution...');
});
As I put in the console.log, I would like to get in that place the value
in each execution of the parameter "sites". So that in the first execution
somehow I need to know that we are runing the test scenarios with the valur
"google.com" in the variable "sites", then in the next execution that
would be "gmail.com", and so on...
I have been checking in all the parameters that comes in the scenario but
not luck. If someone knows how to do this or knows any document/blog/post
that already found the answer for this, please just let know.
Thanks and appreciate your help!
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
------------------------
Andrew Premdas
blog.andrew.premdas.org
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Andrew Premdas
2018-10-24 11:33:36 UTC
Reply
Permalink
So then give your domains a single name e.g. domains. And write a scenario
something like

Scenario: Validate domains
Given I have my domains
When I validate my domains
Then I should see no errors

Write step defs like

Given 'I have my domains' do
@domains = load_domains
end

When 'I validate my domains' do
@errors = []
@domains.each do |d|
result = validate_domain(d)
errors << result unless result.success
end
end

Then 'I should have no errors' do
expect(@errors).to be_empty
...
end

load_domains could get the domains from the same your program does, which
would be DRY and reduce the cost of adding/removing domains.
The Then step can use the errors array to report which domain has failed
and why depending on what validate_domain passes back (an object would be
preferred)

The benefit you get with this approach is simplicity and conciseness. The
only detriment is that you don't have a long list of domains in your
features (for me this is a benefit). Note if you want to have your test
output list the domains you could also print something to stdout in
validate_domain.

Anyhow hope that helps.

All best

Andrew


On Tue, 23 Oct 2018 at 19:46, Felipe Nava Covarrubias <
Post by Felipe Nava Covarrubias
Hey Andrew,
First, Thanks for your response.
So, Yeah I know what is actually doing and implement your approach is not
what Im looking for. Because that was just put a simple example of what Im
going to do. The reality is that I have to validate a bunch of different
domains which shares features between them. So, if I add one scenario per
site I'll end up with a lot of test scenarios, a lot of duplicated steps,
etc.
I have more than 30 different sites with different locales each of them
that I need to validate features that are shared across all of them. This
log_in.feature
@something
Scenario Outline: Verify user can log in
Given A user visiting <site> with locale <locale>
Then the user can log in
And user verifies his profile picture
| site | locale |
| google.com | en_US |
| google.com | es_MX |
| gmail.com | en_US |
| gmail.com | es_MX |
Given(/^A user visiting (.*) with locale (.*)$/, (site, locale) => {
process.env.siteUrl = site; //I do this so that other steps know in which url I am and can navigate to any specific url inside the same site. I'm using this env variable because my tests are in different files
//depending the feature, common shared steps, etc...
browser.navigate(site + '/clearSession.html');
.setLocale(locale); //here Im going to set a cookie to specify the locale of the page, So that any step that navigate to any page in the same siteUrl will have the same locale.
return browser;
});
So, this step is going to happen in all my test no matter if I have to
verify log in, log out or whatever other feature. And that's why I'm trying
to use the before method or maybe use a background step but I would need
also to get the value of my example table for the current execution.
Hooks.js
const { Before, After } = require('cucumber');
Before(function (scenario, callback) {
//what i want to do here is do the same than in the 1st step but for that I need to value of each row in the current execution.
});
This is and example of how my "real tests" are working. I have a step at
the begging of each scenario to set up everything needed to test my
features but as i said earlier I think the best approach to handle it is
using a before hook or a background step.
If you have any idea of how to pass those values to before method or to a
background step please let me know and again, thanks for your time of
checking an responding to my question.
Post by Andrew Premdas
Do you realise that all that example table does is create two separate
scenarios one with google.com and one with gmail.com. So you could just
write two scenarios and your problem would disappear
Scenario: Verify user can go to google
Scenario: Verify user can go to gmail
I've happily cuked without using example tables or outlines for many
years now. IMO all they do is complicate things.
All best
Andrew
On Wed, 17 Oct 2018 at 14:55, Felipe Nava Covarrubias <
Post by Felipe Nava Covarrubias
Hey guys,
I have been working with cucumber + ruby a long time and now we are
migrating to nightwatch + cucumber. But I have been searching in a lot of
places how can I pass the values that are in my example table through my
hook before method.
So, the reason for this is because before of each execution I wants to
do some logic and I need to get data from my example table to be able to do
that.
*test.feature*
@something
Scenario Outline: Verify user can go all sites
Given A user visiting <site>
| site |
| google.com |
| gmail.com |
*hooks.js*
const { Before, After } = require('cucumber');
Before(function (scenario, callback) {
console.log('Here I wants to know what are the values of my example table for this specific execution...');
});
As I put in the console.log, I would like to get in that place the value
in each execution of the parameter "sites". So that in the first execution
somehow I need to know that we are runing the test scenarios with the valur
"google.com" in the variable "sites", then in the next execution that
would be "gmail.com", and so on...
I have been checking in all the parameters that comes in the scenario
but not luck. If someone knows how to do this or knows any
document/blog/post that already found the answer for this, please just let
know.
Thanks and appreciate your help!
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google
Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
--
------------------------
Andrew Premdas
blog.andrew.premdas.org
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
------------------------
Andrew Premdas
blog.andrew.premdas.org
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...