Is there a way to start a scenario from inside of the hooks file. In the After hook I am grabbing the line of the scenario that failed and the file or feature that the scenario is in and formatted it so that I can run that line in the cmd and it will run just the failed scenario.
Example: features\homepage.feature:8 environment='http://stage.homepage.local/'
Now I need help running this in the After hook on a failed scenario
After() do |scenario|
if scenario.failed?
#code here w/ cucumber features\homepage.feature:8 environment='http://stage.homepage.local/'
end
end
Can this even be done?
We can run the scenario from hooks file. System is the keyword that used to execute the given command in the console.
Try the below code
After() do |scenario|
if scenario.failed?
run_cmd = "cucumber features\homepage.feature:8 environment='http://stage.homepage.local/'"
system run_cmd
end
end
Related
I'm writing a chef recipe and on this I need to perform an operation (run a batch) only if a service is not working.
I use this snippet:
batch 'run commnad' do
cwd target_path + '/bin/win64'
code 'command to be executed'
not_if '::Win32::Service.exists?("Service name")'
end
But it does not seems to work. After seeing this question I changed the process using an if clause instead of the guard and it works fine:
if !::Win32::Service.exists?("Service name") then
batch 'Install zabbix agent' do
cwd target_path + '/bin/win64'
code 'command to be executed'
end
end
But this should not be, for what I understood, the right way to manage this, so I'm wondering: why is the guard not working properly?
Thanks,
Michele.
The way you wrote your not_if statement runs the command as a shell script.
The shell doesn't know Ruby code, so the whole command will fail.
Need to first:
require win32/service
In order to use not_if with Ruby code you should put it inside a block instead:
not_if { ::Win32::Service.exists?("Service name") }
See some more examples here (search for not_if on the page):
https://docs.chef.io/resource_common.html
Here is the working example (Chef 13)
require 'win32/service'
windows_service "jenkins" do
action [:stop, :disable]
only_if { ::Win32::Service.exists?("jenkins")}
end
I want to end with something like
Tests = run_tests()
if !Tests.passed
puts Tests.errors
exit
end
start_function_running_for_a_long_time_using_now_verified_code()
Assuming your tests can be run from my_tests.rb, you could run them with a system call and use the exit status to determine if they passed:
`ruby my_tests.rb`
unless $?.success?
abort "Tests didn't pass; aborting"
end
start_function_running_for_a_long_time_using_now_verified_code()
While it doesn't conditionally print the errors, it does stop execution if the tests failed.
I am building a gem for command line use, and I am aware of the if __FILE__ == $0 method for determining whether the file being run is the current file (from Run code only if script called from the command line), however this doesn't work in my case. I have a module with an initialize function that I would like to run when the gem is called from the command line.
module MyApp
def self.initialize
# do command line stuff
end
def self.test
# run a rake test
end
end
MyApp::initialize
However, when running rake test it calls the initialize function which returns an error:
/Library/WebServer/Documents/myapp ❤ rake test
Options:
-v, --[no-]verbose Run verbosely
-h, --help Show this message
rake aborted!
Command failed with status (255): [ruby -I"lib" -I"/Users/bbriggs/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.1.0/lib" "/Users/bbriggs/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.1.0/lib/rake/rake_test_loader.rb" "test/test_myapp.rb" ]
/Users/bbriggs/.rvm/gems/ruby-2.0.0-p195/bin/ruby_noexec_wrapper:14:in `eval'
/Users/bbriggs/.rvm/gems/ruby-2.0.0-p195/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => test
(See full trace by running task with --trace)
I think this is because I am doing MyApp:initialize, because if I take that out of my code the rake test runs as expected, but the command line tool no longer works.
At the moment I am testing my app via bundle exec bin/myapp, and printing out the __FILE__ and $0 variables give me bin/myapp and path/to/lib/myapp.rb respectively, so I was wondering what the best way is to detect whether the module is being required or called directly. Am I even doing this right? I'm a bit of a Ruby newbie. :-)
Finally figured this out. Instead of running MyApp:initialize in lib/myapp.rb I put it in the bin/myapp file. This ensures that it is only run when the app is run from the command line and not when being tested via Rake or required by another script.
How can I do the scenario below: (the purpose is to be able to run all selenium scripts I have or only run some scripts I want if I pass test files as arguments from terminal window with the below code and scenario:
if test files are passed in terminal window
then system will run below codes:
scripts = ENV[scripts].plit(',')
FileList[scripts].each { |file|
system("ruby #{file} > #{directory_name}/#{file}.out")}
if test files are NOT passed in terminal window (it means I want to run all scripts I have in my test suite), then system will run below codes:
FileList['test*.rb'].each { |file|
system("ruby #{file} > #{directory_name}/#{file}.out")}
If I understand correctly, can you just use an if statement?
task :default do
if ENV[scripts]
scripts = ENV[scripts].split(',')
FileList[scripts].each { |file|
system("ruby #{file} > #{directory_name}/#{file}.out")}
else
FileList['test*.rb'].each { |file|
system("ruby #{file} > #{directory_name}/#{file}.out")}
end
end
I need to run a standalone ruby script as Unix (linux) daemon.
After running that daemon I need to run another Ruby method with it.
I installed the ruby-daemon gem on my machine by using the gem install daemon.
I did the test daemon program.
My test.rb file is :
module Test
def test_method
#s =" ITS WORKING !"
file=File.new("/home/username/test.txt", "w")
file.puts #s
file.close
end
end
My test_control.rb file is :
# this is myserver_control.rb
require 'rubygems' # if you use RubyGems
require 'daemons'
Daemons.run('test.rb')
After this I run the following command: ruby test_control.rb start
Now how can I check whether the daemon program has started properly?
How can I invoke a method with it?
Looks like the formatting on your post is way off, so hopefully someone can fix that, but I think the problem here is you're defining a module but not actually firing off the method you define.
The Daemons utility only executes the script provided. You should test that your "test.rb" file can be run on the command line directly before trying to diagnose what might be wrong with Daemons itself.
It may be as reworking test.rb:
module Test
def self.test_method
#s =" ITS WORKING !"
file = File.new("/home/username/test.txt", "w")
file.puts #s
file.close
end
end
Test.test_method
There are other ways to use Daemons where you pass it a module to run, but you're not using it that way.