Skip to content

Commit db719de

Browse files
agarneha1331francisf
authored andcommitted
Parallel by default
1 parent d68a59a commit db719de

9 files changed

Lines changed: 112 additions & 51 deletions

File tree

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ gem 'rspec'
77
gem 'browserstack-local'
88
gem 'parallel_tests'
99
gem 'browserstack-automate'
10+
gem 'rake'

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ GEM
6060
rack (2.2.2)
6161
rack-test (1.1.0)
6262
rack (>= 1.0, < 3)
63+
rake (13.0.6)
6364
regexp_parser (1.7.0)
6465
rspec (3.9.0)
6566
rspec-core (~> 3.9.0)
@@ -92,6 +93,7 @@ DEPENDENCIES
9293
capybara
9394
cucumber
9495
parallel_tests
96+
rake
9597
rspec
9698
selenium-webdriver
9799

README.md

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,28 @@ cucumber-browserstack
33

44
This repository provides information and helpful tweaks to run your Cucumber tests on the BrowserStack selenium cloud infrastructure.
55

6-
###Configuration
7-
Add the CapyBara, Cucumber gems into your Gemfile.
8-
Run `bundle install`.
9-
10-
###Run tests
11-
To run the tests, execute:
12-
13-
```bash
14-
cucumber BS_USERNAME=<username> BS_AUTHKEY=<access-key> SELENIUM_PLATFORM=<platform> SELENIUM_BROWSER=<browser-name>
15-
```
16-
or
17-
```bash
18-
cucumber BS_USERNAME=<username> BS_AUTHKEY=<access-key> BS_AUTOMATE_OS=<os> BS_AUTOMATE_OS_VERSION=<os-version> SELENIUM_BROWSER=<browser-name>
19-
```
20-
21-
Examples:
22-
```bash
23-
cucumber BS_USERNAME=<username> BS_AUTHKEY=<access-key> SELENIUM_PLATFORM=WINDOWS SELENIUM_BROWSER=chrome
24-
```
25-
or
26-
```bash
27-
cucumber BS_USERNAME=<username> BS_AUTHKEY=<access-key> BS_AUTOMATE_OS="OS X" BS_AUTOMATE_OS_VERSION="Mojave" SELENIUM_BROWSER="chrome"
28-
```
6+
## Setup
7+
* Clone the repo
8+
* Install dependencies `bundle install`
9+
* To test various sample repositories with ease, it is recommended to setup `BROWSERSTACK_USERNAME` and `BROWSERSTACK_ACCESS_KEY` environment variables. Alternatively you can directly update `*.config.yml` files inside the `config/` directory with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings)
10+
11+
### Running your tests
12+
* To run tests, run `bundle exec rake parallel`
13+
* To run local tests, run `bundle exec rake local`
14+
15+
Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github)
2916

3017
Alternatively the variables can be set in the environment using env or your CI framework (like Travis or Jenkins)
3118

3219
Notice that selenium driver.quit is not required for Capybara tests because Capybara implicitly does so.
3320

3421
To run parallel tests use `parallel_cucumber`instead of cucumber. Add the option -n to specify the number of parallel processes. Add the parallel_tests gem in your Gemfile.
3522

36-
To run local testing, set the capability `browserstack.local` in features/support/env.rb to true.
23+
To run local testing, set the capability `browserstack.local` in config/local.config.yml to true.
3724

3825
###Further Reading
3926
- [Cucumber](https://cucumber.io/)
4027
- [CapyBara](http://jnicklas.github.io/capybara/)
4128
- [BrowserStack documentation for Automate](https://www.browserstack.com/automate/ruby)
4229

4330
Happy Testing!
44-
45-

Rakefile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require 'rake'
2+
require 'parallel'
3+
require 'cucumber/rake/task'
4+
5+
Cucumber::Rake::Task.new(:single) do |task|
6+
ENV['CONFIG_NAME'] ||= 'single'
7+
task.cucumber_opts = ['--format=pretty', 'features/sample.feature']
8+
end
9+
10+
task default: :single
11+
12+
Cucumber::Rake::Task.new(:local) do |task|
13+
task.cucumber_opts = ['--format=pretty', 'features/sample.feature', 'CONFIG_NAME=local']
14+
end
15+
16+
task :parallel do |_t, _args|
17+
@num_parallel = 4
18+
19+
Parallel.map([*1..@num_parallel], in_processes: @num_parallel) do |task_id|
20+
ENV['TASK_ID'] = (task_id - 1).to_s
21+
ENV['name'] = 'parallel_test'
22+
ENV['CONFIG_NAME'] = 'parallel'
23+
24+
Rake::Task['single'].invoke
25+
Rake::Task['single'].reenable
26+
end
27+
end
28+
29+
task :test do |_t, _args|
30+
Rake::Task['single'].invoke
31+
Rake::Task['single'].reenable
32+
Rake::Task['local'].invoke
33+
Rake::Task['parallel'].invoke
34+
end

config/local.config.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
server: hub.browserstack.com
2+
user: BROWSERSTACK_USERNAME
3+
key: BROWSERSTACK_ACCESS_KEY
4+
5+
common_caps:
6+
build: browserstack-build-1
7+
browserstack.debug: true
8+
name: BStack local cucumber-ruby
9+
browserstack.source: cucumber-ruby:sample-master:v1.1
10+
11+
12+
browser_caps:
13+
- browser: chrome
14+
browserstack.local: true

config/parallel.config.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
server: hub.browserstack.com
2+
user: BROWSERSTACK_USERNAME
3+
key: BROWSERSTACK_ACCESS_KEY
4+
5+
common_caps:
6+
build: browserstack-build-1
7+
browserstack.debug: true
8+
name: BStack parallel cucumber-ruby
9+
browserstack.source: cucumber-ruby:sample-master:v1.1
10+
11+
browser_caps:
12+
- browser: chrome
13+
- browser: firefox
14+
- browser: edge
15+
- browser: safari

config/single.config.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
server: hub.browserstack.com
2+
user: BROWSERSTACK_USERNAME
3+
key: BROWSERSTACK_ACCESS_KEY
4+
5+
common_caps:
6+
build: browserstack-build-1
7+
browserstack.debug: true
8+
name: BStack single cucumber-ruby
9+
browserstack.source: cucumber-ruby:sample-master:v1.1
10+
11+
browser_caps:
12+
- browser: chrome

features/support/env.rb

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,41 @@
1+
require 'yaml'
12
require 'selenium/webdriver'
23
require 'capybara/cucumber'
34
require 'browserstack/local'
4-
require 'browserstack-automate'
5-
BrowserStack.for "cucumber"
65

7-
url = "https://#{ENV['BS_USERNAME']}:#{ENV['BS_AUTHKEY']}@hub.browserstack.com/wd/hub"
8-
9-
Capybara.register_driver :browserstack do |app|
6+
# monkey patch to avoid reset sessions
7+
class Capybara::Selenium::Driver < Capybara::Driver::Base
8+
def reset!
9+
@browser.navigate.to('about:blank') if @browser
10+
end
11+
end
1012

11-
capabilities = Selenium::WebDriver::Remote::Capabilities.new
12-
if ENV['BS_AUTOMATE_OS']
13-
capabilities['os'] = ENV['BS_AUTOMATE_OS']
14-
capabilities['os_version'] = ENV['BS_AUTOMATE_OS_VERSION']
15-
else
16-
capabilities['platform'] = ENV['SELENIUM_PLATFORM'] || 'ANY'
17-
end
13+
TASK_ID = (ENV['TASK_ID'] || 0).to_i
14+
CONFIG_NAME = ENV['CONFIG_NAME'] || 'single'
1815

19-
capabilities['browser'] = ENV['SELENIUM_BROWSER'] || 'chrome'
20-
capabilities['browser_version'] = ENV['SELENIUM_VERSION'] if ENV['SELENIUM_VERSION']
21-
capabilities['browserstack.debug'] = 'true'
22-
capabilities['project'] = ENV['BS_AUTOMATE_PROJECT'] if ENV['BS_AUTOMATE_PROJECT']
23-
capabilities['build'] = ENV['BS_AUTOMATE_BUILD']? ENV['BS_AUTOMATE_BUILD'] : 'browserstack-build-1'
24-
capabilities['browserstack.local'] = 'false'
25-
capabilities['browserstack.source'] = 'cucumber-ruby:sample-master:v1.0'
16+
CONFIG = YAML.safe_load(File.read(File.join(File.dirname(__FILE__), "../../config/#{CONFIG_NAME}.config.yml")))
17+
CONFIG['user'] = ENV['BROWSERSTACK_USERNAME'] || CONFIG['user']
18+
CONFIG['key'] = ENV['BROWSERSTACK_ACCESS_KEY'] || CONFIG['key']
2619

27-
if capabilities['browserstack.local'] && capabilities['browserstack.local'] == 'true';
20+
Capybara.register_driver :browserstack do |app|
21+
@caps = CONFIG['common_caps'].merge(CONFIG['browser_caps'][TASK_ID])
22+
# Code to start browserstack local before start of test
23+
if @caps['browserstack.local'] && @caps['browserstack.local'].to_s == 'true'
2824
@bs_local = BrowserStack::Local.new
29-
bs_local_args = { "key" => "#{ENV['BS_AUTHKEY']}", "forcelocal" => true }
25+
bs_local_args = { 'key' => (CONFIG['key']).to_s }
3026
@bs_local.start(bs_local_args)
3127
end
32-
Capybara::Selenium::Driver.new(app, :browser => :remote, :url => url, :desired_capabilities => capabilities)
33-
3428

29+
Capybara::Selenium::Driver.new(app,
30+
browser: :remote,
31+
url: "https://#{CONFIG['user']}:#{CONFIG['key']}@#{CONFIG['server']}/wd/hub",
32+
desired_capabilities: @caps)
3533
end
3634

3735
Capybara.default_driver = :browserstack
38-
Capybara.app_host = "http://www.google.com"
3936
Capybara.run_server = false
4037

38+
# Code to stop browserstack local after end of test
4139
at_exit do
42-
@bs_local.stop unless @bs_local.nil?
40+
@bs_local.stop unless @bs_local.nil?
4341
end

0 commit comments

Comments
 (0)