Iam using the ruby-jmeter gem to write DSL scripts for doing performance tests automation..
I find this pretty useful to navigate to my API URLs, post data, assert for expected result and generate performance trends reports..
Iam getting stuck while using the extract regex: '', name: '' syntax in ruby-jmeter.
I want something like: I Visit http :// domain/api/user?q=create, I post a raw JSON data as input and I get required JSON response. I want to extract a pattern from this response and use this value dynamically so that I can provide it as input to my raw JSON input to Visit http: //domain/api/user?q=read
Basically i was trying:
visit name: 'CreateUser', url: "<url link>",<br>
method: "post",<br>
domain: "<domain>",<br>
path: 'api/user?q=create', <br>
raw_body: <input json> do<br>
#extract a pattern from response<br>
extract regex: '"Username":"(.+?)"', name: 'userName'<br>
puts '${userName}'<br>
# rest of code here.. <br>
# I want to use ${username} as input to my next Visit call<br>
end
Can somebody who have looked into ruby-jmeter help me here plssss?
thanks,
Vishi.
Ruby-JMeter is a DSL, it only evaluates ruby code when it generates a test plan (.jmx), it doesn't run the test plan and evaluate ruby code at the same time.
In your example this will generate a regular expression extractor in JMeter:
extract regex: '"Username":"(.+?)"', name: 'userName'
The following ruby code won't know anything about ${} style JMeter variables:
puts '${userName}'
If the regular expression matches, then the relevant ${userName} JMeter variable will be set and you should be able to use it in subsequent requests:
visit name: 'users', url: '/api/user?q=${userName}'
For more detailed examples testing APIs with Ruby please see this blog post
Related
I have a inspec test, this is great:
inspec exec scratchpad/profiles/forum_profile --reporter yaml
Trouble is I want to run this in a script and output this to an array
I cannot find the documentation that indicated what method i need to use to simulate the same
I do this
def my_func
http_checker = Inspec::Runner.new()
http_checker.add_target('scratchpad/profiles/forum_profile')
http_checker.run
puts http_checker.report
So the report method seems to give me load of the equivalent type and much more - does anyone have any documentation or advice on returning the same output as the --reporter yaml type response but in a script? I want to parse the response so I can share output with another function
I've never touched inspec, so take the following with a grain of salt, but according to https://github.com/inspec/inspec/blob/master/lib/inspec/runner.rb#L140, you can provide reporter option while instantiating the runner. Looking at https://github.com/inspec/inspec/blob/master/lib/inspec/reporters.rb#L11 I think it should be smth. like ["yaml", {}]. So, could you please try
# ...
http_checker = Inspec::Runner.new(reporter: ["yaml", {}])
# ...
(chances are it will give you the desired output)
I have a .properties files as below:
user:abcd
pwd:xyz
system:test
Next, I have a ruby script with Watir for browser automation. In this script, I have statements like
browser.text_field(:id => 'identifierId').set "#{user}:variable to be replaced by its value from .properties file".
Similarly, other values need to be replaced for "pwd" and "system".
I tried the solution per below posts:
Replace properties in one file from those in another in Ruby
However, "set" command is setting whatever has been paased as arguments to it instead of replacing the variable with its value.
Please help.
You have to read the information out of the file.
Most Watir users leverage yaml files for this.
config/properties.yml:
user: abcd
pwd: xyz
system: test
Then read the yaml file & parse your data:
properties = YAML.safe_load(IO.read('config/properties.yml'))
text_field = browser.text_field(id: 'identifierId')
text_field.set properties['user']
Alternately you can take a look at Cheezy's Fig Newton gem, which is designed to work with his Page Object gem
I'm pretty new to ruby and all the documentation on this subject has confused me a bit. So here goes.
I'm using inspec to test my infrastructure, but I want it to consume some variables from the YAML file used by ansible. This effectively means I can share vars from ansible code and use them in ruby.
The YAML file looks like this:
- name: Converge
hosts: all
vars:
elasticsearch_config:
cluster.name: "{{ elasticsearch_cluster_name }}"
node.name: "es-test-node"
path.data: "/var/lib/elasticsearch"
path.logs: "/var/log/elasticsearch"
elasticsearch_cluster_name: test
pre_tasks:
roles:
- elasticsearch
post_tasks:
At this point, I'm just playing around with ruby code to extract that, and have:
require 'yaml'
parsed = begin
YAML.load(File.open("../../playbook.yml"))
rescue ArgumentError => e
puts "Could not parse YAML: #{e.message}"
end
puts parsed
Which outputs the hash:
{"name"=>"Converge", "hosts"=>"all", "vars"=>{"elasticsearch_config"=>{"cluster.name"=>"{{ elasticsearch_cluster_name }}", "node.name"=>"es-test-node", "path.data"=>"/var/lib/elasticsearch", "path.logs"=>"/var/log/elasticsearch"}, "elasticsearch_cluster_name"=>"test"}, "pre_tasks"=>nil, "roles"=>["elasticsearch"], "post_tasks"=>nil}
So far so good. This all makes sense to me. Now, I would like to pull values out of this data and use them in the ruby code, referencing them by the keys. So, if I wanted to get the value of vars.elasticsearch_config.node.name, how would I go about doing this?
YAML.load reads the document into an array, so you must get the first element in your example:
loaded_yaml[0]["vars"]["elasticsearch_config"]["node.name"]
The reason for this is that the document you are parsing begins with a single dash, indicating a list item. Even though there is only one item in the list, Psych (thy YAML engine) is still placing it into an array representing a list. This is also why you got a no implicit conversion of String to Integer error. Note that the response you get is enclosed by square brackets:
=> [{"name"=>"Converge", "hosts"=>"all", "vars"=>{"elasticsearch_config"=>{"cluster.name"=>"{{ elasticsearch_cluster_name }}", "node.name"=>"es-test-node", "path.data"=>"/var/lib/elasticsearch", "path.logs"=>"/var/log/elasticsearch"}, "elasticsearch_cluster_name"=>"test"}, "pre_tasks"=>nil, "roles"=>["elasticsearch"], "post_tasks"=>nil}]
I created mCollective inventory script as below,
def formatting(users_ids)
YAML.load(File.open(users_ids))
end
inventory do
format "%s\t%s\t"
fields { [facts["hostname"], formatting(facts["users_ids"]) ] }
end
Here users_ids facter is in yaml format on the server. So when I do inventory for this facter I need to parse that yaml format to hash. But when I run with this script getting below error,
[root#mco-server]#
The inventory application failed to run, use -v for full error backtrace details: (eval):2:in `initialize': No such file or directory - ---
root: 0
test1: 503
testuser: 2033
[root#mco-server]#
Not sure if am missing something to get the output parsed. The strange thing is its not printing hostname also.
The facter output is below on the server from facts.yaml
users_ids: |-
---
root: 0
test1: 503
testuser: 2033
Any help would be much appreciated.
According to the error message, the argument you are passing to users_ids is not a valid filename.
def formatting(users_ids)
YAML.load(File.open(users_ids))
end
Somehow your code is passing --- as an argument to that method. This is likely due to the combination of your API calls to parse and load the yaml and the yaml file itself. Consider changing the API call to a cleaner:
def formatting(users_ids)
YAML.load_file(users_ids)
end
and I think you really want a hash in your yaml and not an array of key-value pairs with an element of ---, so your yaml should really be:
users_ids:
root: 0
test1: 503
testuser: 2033
which would also remove the --- which typically indicates the beginning of a yaml, and also seems to be what your code is erroring on in the way you are trying to load the yaml.
How can I check if / else in yaml file.
like:
if %{attribute}
attributes:
shipping_comment: Shipping comment / Instructions
else
attributes:
shipping_date: Date
YAML is a data serialisation language, so it's not meant to contain if/else style executable statements: that's the responsibility of the programming language you're using.
A simple example in Ruby to determine which config string from a YAML file to output could be defining your YAML config file as follows:
data.yml
attributes:
shipping_comment: Shipping comment / Instructions
shipping_date: Date
Then, in your program, read the file in and run the conditional there:
shipping.rb
#!/usr/bin/env ruby
require 'yaml'
config = YAML.load_file('data.yml')
attribute = true # your attribute to check here
if attribute
puts config['attributes']['shipping_comment']
else
puts config['attributes']['shipping_date']
end
Out of the box .yaml files won't include any conditional logic, as Paul Fioravanti says:
YAML is a data serialisation language, so it's not meant to contain if/else style executable statements: that's the responsibility of the programming language you're using.
However, there are some cases, such as Infrastructure as Code where you might not have the luxury of Paul's solution. In these cases, most decent Infrastructure tools provide an inbuilt way of achieving conditional logic.
Since it appears that infra is not the area you're looking in, I won't go into detail on how to write each tools solution, but for anyone that end's up here like I did, docs such as these helped me and may prove useful for you:
Ansible
CloudFormation
This is a little late for the original poster, but may be of use to others: The Azure implementation of the YAML parser does support conditional checks. For example, the following YAML in an azure-pipeline.yml:
#azure-pipeline.yml
parameters:
- name: testCondition
displayName: 'Use experimental build process?'
type: boolean
default: true
steps:
- ${{ if eq(parameters.testCondition, true) }}:
- bash: echo 'true'
- ${{ if not(eq(parameters.testCondition, true)) }}:
- bash: echo 'false'
will evaluate the value of the testCondition parameter. When run, the bash task will echo "true", something like this: