Reputation: 1035
So I have this feature in cucumberjs
Scenario: Upload a valid pcf file into gpe
Given that the user uploads a valid pcf file
Then the user should see an upload success indicator
Scenario: Upload an invalid pcf file
Given that the user uploads an invalid pcf file
Then the user should see an upload error message
As you can see that the then are almost the same except for the string after upload. So I wrote a my then like this:
this.Then(/^that the user uploads [a-zA-Z]+/, ( option ) => {
console.log( option );
} );
But option displays function: finish. How can I get the string after the uploads word?
Upvotes: 2
Views: 2953
Reputation: 195
You don't need RegEx anymore! woo! See below.
Scenario: Upload an "valid pcf file into gpe"
Given that the user uploads a valid pcf file
Then the user should see an upload success indicator
Scenario: Upload an "invalid pcf file"
Given that the user uploads an invalid pcf file
Then the user should see an upload error message
Use this for your scenarios, when you wrap text in "" it is now a string ready to be passed into your JS
this.Then(Upload an {string}, ( stringSwitcher ,option ) => {
This is how your line starting your JS will work, you can name stringSwitcher anything you want but that is what stores your unique part of the scenario, essentially Upload an "blablabla" in the scenario and it will be able to use the javascript for that passing it through.
I hope this makes sense, but i've went down the route of using regEx and you really don't need to anymore.
Upvotes: 4
Reputation: 3057
Why not try something like this:
this.Then(/^that the user uploads an? (valid|invalid) (\w+) file/, (validity, filetype) => {
if (validity == "valid"){
console.log("this " + filetype + " is valid");
} else {
console.log("this " + filetype + " is invalid");
}
return true;
} );
A breakdown:
an?
- capture a
or an
(?
makes the n
optional - for grammatical purposes)(valid|invalid)
- capture the words invalid and valid(\w+)
- capture any characters a-z
, A-Z
, 0-9
This will mean you can do things depending on the validity of the file and the filetype within the if statements.
Alternatively, using a switch statement would also work.
this.Then(/^that the user uploads an? (valid|invalid) (\w+) file/, (validity, filetype) => {
switch(filetype){
case "pcf":
if (validity == "valid"){
// Do the stuff for valid
} else {
// Do the stuff for invalid
}
break;
default:
throw new Error("Filetype: '" + filetype + "' is not recognised");
}
return true;
});
Upvotes: 3