Running Cucumber (WATIR) tests in Jenkins, from multiple Feature directories - ruby

I am currently running Cucumber (WATIR) tests in Jenkins, with the standard directories structure running a set of features.
The directory structure is as follows -
Features
- Pages
-- main.rb
-- httparty_config.rb
-- page1.rb
-- page2.rb
- Step Definitions
-- page1_steps.rb
-- page2_steps.rb
feature_one.feature
feature_two.feature
I am now implementing multiple sites, and am wanting to share the same main class (and other configs for HTTParty etc, but separate each of the sites, into their own cucumber sub-directory (for cleanliness purposes).. The following tests run in Rubymine with no issues.
Env
- main.rb
- httparty_config.rb
Site_1
- Features
-- Pages
--- s1_page1.rb
--- s1_page2.rb
-- Step Definitions
--- s1_page1_steps.rb
--- s1_page2_steps.rb
- s1_feature_one.feature
- s1_feature_two.feature
Site_2
- Features
-- Pages
--- s2_page1.rb
--- s2_page2.rb
-- Step Definitions
--- s2_page1_steps.rb
--- s2_page2_steps.rb
- s2_feature_one.feature
- s2_feature_two.feature
When attempting to run a feature test in Jenkins using this structure, Jenkins fails, as it does not meet the required Cucumber structures.. It cannot find the Feature folder.
How can I keep the new multi-site structure and define the structure so that Jenkins knows where to find each feature, in the different directories?

OK.. So I was making that way more complicated than it needed to be.
I just needed to add a CD command in the Jenkins execute shell command
eg
#!/bin/bash
source /home/jenkins/.rvm/bin/rvm
rvm list
mv gemfile Gemfile
bundle install
cd Site_1
xvfb-run cucumber --format json -o cucumber.json

Related

Having Gitlab Projects calling the same gitlab-ci.yml stored in a central location

I have many Gitlab project followed the same CI template. Whenever there is a small change in the CI script, I have to manually modify the CI script in each project. Is there a way you can store your CI script in a central location and have your project called that CI script with some environment variable substitution? For instance,
gitlab-ci.yml in each project
/bin/bash -c "$(curl -fsSL <link_to_the_central_location>.sh)"
gitlab-ci.yml in the central location
stages:
- build
- test
build-code-job:
stage: build
script:
- echo "Check the ruby version, then build some Ruby project files:"
- ruby -v
- rake
test-code-job1:
stage: test
script:
- echo "If the files are built successfully, test some files with one command:"
- rake test1
test-code-job2:
stage: test
script:
- echo "If the files are built successfully, test other files with a different command:"
- rake test2
You do not need curl, actually gitlab supports this via the include directive.
you need a repository, where you store your general yml files. (you can choose if it is a whole ci file, or just parts. For this example lets call this repository CI and assume your gitlab runs at example.com - so the project url would be example.com/ci. we create two files in there just to show the possibilities.
is a whole CI definition, ready to use - lets call the file ci.yml. This approach is not really flexible
stages:
- build
- test
build-code-job:
stage: build
script:
- echo "Check the ruby version, then build some Ruby project files:"
- ruby -v
- rake
test-code-job1:
stage: test
script:
- echo "If the files are built successfully, test some files with one command:"
- rake test1
test-code-job2:
stage: test
script:
- echo "If the files are built successfully, test other files with a different command:"
- rake test2
is a partly CI definition, which is more extendable. lets call the files includes.yml
.build:
stage: build
script:
- echo "Check the ruby version, then build some Ruby project files:"
- ruby -v
- rake
.test:
stage: test
script:
- echo "this script tag will be overwritten"
There is even the option to use template string from yaml. please reference the gitlab documentation but it is similar to 2.
we do have our project which wants to use such definitions. so either
For the whole CI file
include:
- project: 'ci'
ref: master # think about tagging if you need it
file: 'ci.yml'
as you can see now we are referencing one yml file, with all the cahnges.
with partial extends
include:
- project: 'ci'
ref: master # think about tagging if you need it
file: 'includes.yml'
stages:
- build
- test
build-code-job:
extends: .build
job1:
extends: .test
script:
- rake test1
job2:
extends: .test
script:
- rake test2
As you see, you can easily use the includes, to have a way more granular setup. Additionally you could define at job1 and job2 variables, eg for the test target, and move the script block into the includes.yml
Futhermore you can also use anchors for the script parts. Which looks like this
includes.yml
.build-scirpt: &build
- echo "Check the ruby version, then build some Ruby project files:"
- ruby -v
- rake
.build:
stage: build
script:
- *build
and you can use also the script anchor within your configuration
For a deeper explanation you can also take a look at https://docs.gitlab.com/ee/ci/yaml/includes.html

RubyMine IDE Cannot Find Spec Files in Spec directory

I have loaded a Ruby project into RubyMine, that has it's spec files located in a spec directory, as usual. Right-clicking and selecting 'run' on any single spec file correctly runs it. Right-clicking and selecting 'run all' on the specs directory causes every test to fail with the following message:
Fail to load: /Users/nathaniel/repos/close-web/spec/compositions/analysis/analysis_data_spec.rb:1
Exception message: cannot load such file -- spec_helper
Inside the launch configuration, the 'Tests folder' seems to be set correctly:
/Users/nathaniel/repos/close-web/spec
And the working directory as well:
/Users/nathaniel/repos/close-web
The Ruby arguments are set like this:
-e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) -Itest
(There is some indication on the RubyMine website that -Itest is required.) Under the Bundler tab 'Run the script in the context of the bundle' is checked as well. I'm unclear what else RubyMine might need to be able to recursively find and execute the tests. Running them from the command line works fine:
~repos/closeweb $ bundle exec rspec spec
... (all the tests running)
Finished in 6 minutes 27 seconds (files took 13.39 seconds to load)
1054 examples, 0 failures, 108 pending
What configuration is wrong here that all the tests won't run?
Given that your folder is called spec, not test, you need to -Ispec, not -Itest.
In other words, your RubyMine arguments should be:
-e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) -Ispec
From the ruby man page:
-I directory Used to tell Ruby where to load the library scripts.
Directory path will be added to the load-path variable ($:)

TravisCI Ruby project not working with rspec

I have a Logstash plugin written in Ruby that has started failing on TravisCI, but it works locally. Any ideas what this means?
$ bundle exec rspec spec
The signal EXIT is in use by the JVM and will not work correctly on this platform
Coverage may be inaccurate; set the "--debug" command line option, or do JRUBY_OPTS="--debug" or set the "debug.fullTrace=true" option in your .jrubyrc
W, [2016-12-02T14:12:20.127000 #5894] WARN -- : This usage of the Code Climate Test Reporter is now deprecated. Since version
1.0, we now require you to run `SimpleCov` in your test/spec helper, and then
run the provided `codeclimate-test-reporter` binary separately to report your
results to Code Climate.
More information here: https://github.com/codeclimate/ruby-test-reporter/blob/master/README.md
The command "bundle exec rspec spec" exited with 1.
From here:
https://travis-ci.org/mikebski/logstash-filter-datepart
As you can see on the code-climate/ruby-test-reporter CHANGELOG, you need to replace these lines on your test helper file (in my case spec/spec_helper.rb):
require 'codeclimate-test-reporter'
CodeClimate::TestReporter.start
for these ones:
require 'simplecov'
SimpleCov.start
Henceforth, you also have to call codeclimate-test-reporter explicitly.
Hope it helps!

Running Cucumber tests on different environments

I'm using Cucumber and Capybara for my automated front end tests.
I have two environments that I would like to run my tests on. One is a staging environment, and the other is the production environment.
Currently, I have my tests written to access staging directly.
visit('https://staging.somewhere.com')
I would like to re-use the tests in production (https://production.somewhere.com).
Would it be possible to store the URL in a variable in my step definitions
visit(domain)
and define domain using an environment variable called form the command line? Like
$> bundle exec cucumber features DOMAIN=staging
if I want to point the tests to my staging environment, or
$> bundle exec cucumber features DOMAIN=production
if I want it to run in production?
How do I go about setting this up? I'm fairly new to Ruby and I've been searching the forums for a straight forward information but could not find any. Let me know if I can provide more information. Thanks for your help!
In the project's config file, create a config.yml file
---
staging:
:url: https://staging.somewhere.com
production:
:url: https://production.somewhere.com
Then extra colon in the yml file allows the hash key to be called as a symbol.
In your support/env.rb file, add the following
require 'yaml'
ENV['TEST_ENV'] ||= 'staging'
project_root = File.expand_path('../..', __FILE__)
$BASE_URL = YAML.load_file(project_root + "/config/config.yml")[ENV['TEST_ENV']][:url]
This will default to the staging environment unless you override the TEST_ENV. Then, from your step or hook, you can call:
visit($BASE_URL)
or you might need :/
visit "#{$BASE_URL}"
This will allow you to use
bundle exec cucumber features TEST_ENV=production
I don't use cucumber much but you should be able to do
bundle exec cucumber features DOMAIN=staging
then in your tests use ENV['DOMAIN'] || YOUR_DEFAULT_DOMAIN to utilize this variable. YOUR_DEFAULT_DOMAIN should probably be your test environment.
See Here

Cucumber feature file runs on the command line but not in Intellij: Can't load step definitions (undefined)

The Cucumber feature file runs on the command line, but not in Intellij. I believe this means there are additional configuration steps that I'm missing.
I am using Windows 7, Intellij IDEA 13.1.3, Ruby 1.9.3, Cucumber 1.3.4
Here is my project folder structure:
\cucumber
\projectName
\features
-mainFeature.feature
\step_definitions
-helperFeature.feature
On the command line I navigate to the cucumber folder:
E:\path\to\cucumber\folder\
Then I execute the following command:
bundle exec cucumber projectName/features/mainFeature.feature
And this command runs the cucumber test case successfully.
When I try to run the cucumber test case in Intellij, Intellij is unable to find/load the step definitions. It says that I "can implement step definitions for undefined steps with these snippets" for every single step I use in my features.
The specific error message is:
You can implement step definitions for undefined steps with these snippets:
Then(/^verify the same\-date destination airport grouping strategy$/) do
pending # express the regexp above with the code you wish you had
end
1 scenario (1 undefined)
5 steps (5 undefined)
0m0.014s
Under "Run > Edit Configurations" I've defined the following on the configuration tab:
Feature file: E:\path\to\cucumber\folder\projectName\features\scratchpad_grouping_strategy.feature
Runner options: --color -r features
'cucumber' gem: 1.3.4
Working directory: E:\path\to\cucumber\folder\
Environment variables: [environment variables]
Ruby arguments: -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift);
Ruby SDK: Use other SDK -> 'cucumber' gem: ruby 1.9.3
And under the Bundler tab:
Run the script in context of the bundle (bundle exec) is checked
Suggestions?
I had to specify my working directory correctly. Below is my directory
My working directory: /user/jchiang/test/cucumber
My cucubmer feature files: /user/jchiang/test/cucumber/features
My cucubmer step definitions:/user/jchiang/test/cucumber/lib

Resources