Cucumber scenario outline input with quotes in the value - ruby

I am trying to input a value to a text field using fitnesse for appian with ruby functions.
I have a cucumber scenario:
Feature: create a quick decision
Scenario Outline: decision
When I populate the "Decision Title" field with <decTitle>
Examples:
| role | decTitle |
| "test" | "testinput "user1" titlepart2" |
it is imperative that I am able to input a value with quotes in to this field as well as select from a dropdown menu.
I have tried to escape the quotes by using the following:
triple quote on on same and multi line
"""
foo
"and"
bar
"""
" for both the starting and inner quotes
"foo "and" bar"
"foo "and" bar"
double quotes within and out side
"foo ""and"" bar"
all result in the same "step is undefined" error when running the test case
any and all help would be appreciated.

You can use regular expression (.*) to pass the value with double quotes. Like below,
Scenario:
#Regression
Scenario Outline: decision
When I populate the "Decision Title" field with <decTitle>
Examples:
| role | decTitle |
| "test" | "testinput "user1" titlepart2" |
Code:
#When("I populate the \"(.*)\" field with (.*)$")
public void populate(String message, String decTitle) {
System.out.println("Message: " + message);
System.out.println("Title: " + decTitle);
}
Output:
Message: Decision Title
Title: "testinput "user1" titlepart2"

Related

bash script+jq: contains(), should match an exact string

jq --arg NAME "John" '.[] | select(.group | contains ($NAME))
this should return only the name John, but it is displaying all values where John is included..
Eg: displays Johnny, Johnie
It's giving these values also as output.
I tried with including * and also tried with single and double quotes. If I use * its giving empty output. Can someone help me on this?
From the manual on contains
The filter contains(b) will produce true if b is completely contained within the input. A string B is contained in a string A if B is a substring of A.
Therefore, contains also matches on substrings. Use == for exact matches, i.e. comparing with the entire string only:
jq --arg NAME "John" '.[] | select(.group == $NAME)'

jq - not working as expected in Jenkins shell script

I have the following JSON named as my.json.
[
{
"action": "copy",
"artifact_location": "one foo one"
},
{
"action": "copy",
"artifact_location": "one bar one"
},
{
"action": "remove",
"artifact_location": "two foo two"
}
]
My goal is to delete all the objects in the root JSON array if the artifact_location property of the a object contains the string value "foo".
I'm using jq command line utility to accomplish this task.
Following is my jq command. It is working perfectly when I'm running it on my local machine (macOS and jq version is 1.6).
jq 'del(.[] | select(.artifact_location | test("foo")))' my.json
However, the above commands gives the following error when I try to run it as a shell script in a Jenkins job(Ubuntu and jq version is 1.3).
error: test is not defined
del(.[] | select(.artifact_location | test("foo")))
^^^^
1 compile error
What am I possibly doing wrong here?
test/0 is for testing if a string matches a particular regular expression, which is not available in jq 1.3 (as mentioned in the comments). contains/1 could be used in this case.
del(.[] | select(.artifact_location | contains("foo")))
I would rather approach this as filtering out the objects, rather than deleting them. Select objects that does not contain "foo".
map(select(.artifact_location | contains("foo") | not))

sed to replace list of subnets at nth position in a file

Adding "ip","ip","ip" to each IP address
tf_alist=$(for i in $alist; do echo -n "\"$i\"",; done | sed 's/,$//')
echo $tf_alist
gives:
"192.168.0.216/29","192.168.92.72/30","192.168.92.70/31"
Now, I've variable.tf as shown below:
variable "allowlisted_cidrs_prod" {
type = list(string)
description = "ip ranges - allowlisted - prod instances"
default = ["192.168.241.88/32", "192.168.128.222/32", "192.168.231.150/32"]
}
variable "allowlisted_cidrs_test" {
type = list(string)
description = "ip ranges - allowlisted - test instances"
default = ["192.168.58.61/32", "192.168.3.224/32"]
}
variable "elb_cipher" {
type = string
description = "ELB cipher"
default = "ELBSecurityPolicy-TLS-1-2-2017-01"
}
At variable "allowlisted_cidrs_prod" I want to replace the below list string:
From:
default = ["192.168.241.88/32", "192.168.128.222/32", "192.168.231.150/32"]
To (as per $tf_alist):
default = ["192.168.0.216/29","192.168.92.72/30","192.168.92.70/31"]
can you suggest a way to do it using sed? TYA!
To replace the string, I'm trying to use the below expression to capture the existing IP list:
old_ip_list=$(cat variable.tf | sed -n '/variable "allowlisted_cidrs_prod"/,$ {/^[[:blank:]]*default[[:blank:]]*=[[:blank:]]*\(.*\).*/ { s//\1/p; q; }}')
gives:
["192.168.241.88/32", "192.168.128.222/32", "192.168.231.150/32"]
Ref: https://sed.js.org/?gist=bdeddb0ed01bdc8f96b3a05952909cd7
Removes "[]" from the given output to match with $tf_alist.
echo -e "\n $old_ip_list" | gsed 's/.*\[//;s/\].*//;'
The best way to edit a structured format like this is to use a tool for that particular structured format. If this is code in a programming language, probably change the code to read the values from an external resource into a variable, and don't modify the code at all if you can avoid it.
Having said that, here is a quick refactoring.
tf_alist=$(printf ',"%s"' $alist | sed 's/^,//')
awk -v new="$tf_alist" '/^variable "/ { v=$2 }
v=="\"allowlisted_cidrs_prod\"" && $1 == "default" { sub(/\[.*/, "[" new "]") }
1' variable.tf >new_variable.tf
This simply keeps track of which variable it has last seen, and only replaces the default line when it's the one we are trying to target. The final 1 is a common Awk idiom to unconditionally print all lines. And of course sub is Awk's equivalent of the s/// command in sed, and "[" new "]" is simple string concatenation; we put square brackets around the value of the variable new (which is defined in the -v option - it contains the value of the shell variable $tf_alist).
As all ad-hoc parsers, this is fairly brittle, but it works with the example you provided (demo: https://ideone.com/7kDNCU). If this is for more than a one-off, seriously think about a different approach, rather than spending more time on making this more robust (or, heavens, reimplementing it in read-only sed).

How to access special characterized Json key in Bash shell?

I'm using Bash to extract json keys by 'jsawk'. I converted a some.xlsx to some.json by node xlsx . I have some keys like this:
"Scheduled Release"
"Standard Input Parameters\n(Dev)"
The second one contains '\n' because the cell in the .xlsx has 2 lines.
The problem is that I cannot extract the value, using this:
$ cat some.json | jsawk -n 'out(this.["Scheduled Release"])'
or
$ cat some.json | jsawk -n 'out(this.["Standard Input Parameters\n(Dev)"])'
No problem for the keys without white spaces.
My some.json contains an array of objects like this:
{
"Service": "A service",
"Resource": "Some Users",
"Engineer": "A name",
"Description": "A description",
"Scheduled Release" : "1.0",
"Method": "A method",
"Endpoint": "An enpoint",
"Standard Input Parameters\n(Dev)": "Some inputs"
}
How can I do this with Bash?
Now I can extract my expected key using this:
$cat some.json | jsawk -n 'out(this["Standard Input Parameters\n(Dev)"])'

Cannot insert white spaces in string in the examples table

Feature:player
#all
Scenario Outline:Where is the player
Given I navigate to Google
When I enter < player> in the search field
Then the text < keyword1> should be present
#current #football
Examples:
| player | keyword1 |
| Rooney | Manchester |
| Gerrard | Liverpool |
| Terry | Chelsea |
#old #football
Examples:
| player | keyword1 |
| Eric Cantona | Manchester |
If I write Cantona instead of Eric Cantona then it is working, but as soon as you run the program with white space inserted in a string it gives an error.
Try putting quotes around the Scenario Outline placeholders (and removing the leading space from the placeholder). For example:
Scenario Outline: Where is the player
Given I navigate to Google
When I enter "<player>" in the search field
Then the text "<keyword1>" should be present
The problem is that your step definition is only looking for a single word:
When /^I enter (\w+) in the search field$/ do | player |
Cucumber uses regular expressions to match steps to their definitions, and to capture the variables. Your step definition is looking for "I enter ", followed by a single word, followed by " in the search field".
You could change the "player" regex from (\w+) to something like ([\w\s]+). That would match words and white space and should match the multi word example.
When /^I enter ([\w\s]+) in the search field$/ do | player |
Alternatively if you surround your variables with quotes (as suggested by orde) then Cucumber should generate step definitions which match anything inside the quotes using a (.*) group.

Resources