diff --git a/Gemfile b/Gemfile index c89bbf6..b588471 100644 --- a/Gemfile +++ b/Gemfile @@ -8,3 +8,4 @@ gem 'browserstack-local' gem 'parallel_tests' gem 'browserstack-automate' gem 'rake' +gem 'browserstack-ruby-sdk' diff --git a/README.md b/README.md index 4a0a047..3081f4f 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,43 @@ cucumber-browserstack ===================== +![BrowserStack Logo](https://d98b8t1nnulk5.cloudfront.net/production/images/layout/logo-header.png?1469004780) -This repository provides information and helpful tweaks to run your Cucumber tests on the BrowserStack selenium cloud infrastructure. +Code samples to get started with Cucumber tests on BrowserStack Automate using the **BrowserStack Ruby SDK**. ## Setup -* Clone the repo -* Install dependencies `bundle install` -* 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) -### Running your tests -* To run tests, run `bundle exec rake parallel` -* To run local tests, run `bundle exec rake local` +### Requirements - Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github) +1. Ruby: If you don't have Ruby installed, follow the instructions given [here](https://www.ruby-lang.org/en/documentation/installation/) +2. Bundler: If you don't have Bundler installed, follow the instructions given [here](https://bundler.io/) -Alternatively the variables can be set in the environment using env or your CI framework (like Travis or Jenkins) +### Install the dependencies -Notice that selenium driver.quit is not required for Capybara tests because Capybara implicitly does so. +```sh +bundle install +``` -###Further Reading -- [Cucumber](https://cucumber.io/) -- [CapyBara](http://jnicklas.github.io/capybara/) -- [BrowserStack documentation for Automate](https://www.browserstack.com/automate/ruby) +--- -Happy Testing! +## Configuration + +### 1. Set up `browserstack.yml` + +All BrowserStack configuration is managed through a single `browserstack.yml` file at the root of the repository. + +Set parallels. + +browserStackLocal: false # set to true for local testing. +``` + +## Running your tests + +All tests are run from the repo root. The same command is used for all run types — behaviour is controlled entirely by `browserstack.yml`: + +```sh +bundle exec browserstack-sdk exec cucumber features/sample.feature +``` + +> **Tip:** Instead of hardcoding credentials, set `BROWSERSTACK_USERNAME` and `BROWSERSTACK_ACCESS_KEY` as environment variables. + +### 2. diff --git a/Rakefile b/Rakefile deleted file mode 100644 index e5bdaa6..0000000 --- a/Rakefile +++ /dev/null @@ -1,26 +0,0 @@ -require 'rake' -require 'parallel' -require 'cucumber/rake/task' - -Cucumber::Rake::Task.new(:single) do |task| - ENV['CONFIG_NAME'] ||= 'single' - task.cucumber_opts = ['--format=pretty', 'features/sample.feature'] -end - -task default: :single - -Cucumber::Rake::Task.new(:local) do |task| - task.cucumber_opts = ['--format=pretty', 'features/sample.feature', 'CONFIG_NAME=local'] -end - -task :parallel do |_t, _args| - @num_parallel = 4 - - Parallel.map([*1..@num_parallel], in_processes: @num_parallel) do |task_id| - ENV['TASK_ID'] = (task_id - 1).to_s - ENV['CONFIG_NAME'] = 'parallel' - - Rake::Task['single'].invoke - Rake::Task['single'].reenable - end -end diff --git a/browserstack.yml b/browserstack.yml new file mode 100644 index 0000000..056889b --- /dev/null +++ b/browserstack.yml @@ -0,0 +1,29 @@ +userName: YOUR_BROWSERSTACK_USERNAME # or use BROWSERSTACK_USERNAME env var +accessKey: YOUR_BROWSERSTACK_ACCESS_KEY # or use BROWSERSTACK_ACCESS_KEY env var + +projectName: BStack cucumber-ruby +buildName: browserstack-build-1 + +browsers: + - browserName: chrome + browserVersion: latest + os: Windows + osVersion: 11 + - browserName: firefox + browserVersion: latest + os: Windows + osVersion: 11 + - browserName: edge + browserVersion: latest + os: Windows + osVersion: 11 + - browserName: safari + browserVersion: latest + os: OS X + osVersion: Ventura + +debug: true + +parallelsPerPlatform: 1 + +browserStackLocal: false # set to true for local testing \ No newline at end of file diff --git a/config/local.config.yml b/config/local.config.yml deleted file mode 100644 index 692bd8c..0000000 --- a/config/local.config.yml +++ /dev/null @@ -1,14 +0,0 @@ -server: hub.browserstack.com -user: BROWSERSTACK_USERNAME -key: BROWSERSTACK_ACCESS_KEY - -common_caps: - build: browserstack-build-1 - browserstack.debug: true - name: BStack local cucumber-ruby - browserstack.source: cucumber-ruby:sample-master:v1.1 - - -browser_caps: -- browser: chrome - browserstack.local: true diff --git a/config/parallel.config.yml b/config/parallel.config.yml deleted file mode 100644 index 05acb6b..0000000 --- a/config/parallel.config.yml +++ /dev/null @@ -1,15 +0,0 @@ -server: hub.browserstack.com -user: BROWSERSTACK_USERNAME -key: BROWSERSTACK_ACCESS_KEY - -common_caps: - build: browserstack-build-1 - browserstack.debug: true - name: BStack parallel cucumber-ruby - browserstack.source: cucumber-ruby:sample-master:v1.1 - -browser_caps: -- browser: chrome -- browser: firefox -- browser: edge -- browser: safari diff --git a/config/single.config.yml b/config/single.config.yml deleted file mode 100644 index 179b8a7..0000000 --- a/config/single.config.yml +++ /dev/null @@ -1,12 +0,0 @@ -server: hub.browserstack.com -user: BROWSERSTACK_USERNAME -key: BROWSERSTACK_ACCESS_KEY - -common_caps: - build: browserstack-build-1 - browserstack.debug: true - name: BStack single cucumber-ruby - browserstack.source: cucumber-ruby:sample-master:v1.1 - -browser_caps: -- browser: chrome diff --git a/features/support/env.rb b/features/support/env.rb index 440ed45..e7bdabb 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -2,6 +2,7 @@ require 'selenium/webdriver' require 'capybara/cucumber' require 'browserstack/local' +require 'browserstack/sdk' # monkey patch to avoid reset sessions class Capybara::Selenium::Driver < Capybara::Driver::Base @@ -10,32 +11,12 @@ def reset! end end -TASK_ID = (ENV['TASK_ID'] || 0).to_i -CONFIG_NAME = ENV['CONFIG_NAME'] || 'single' - -CONFIG = YAML.safe_load(File.read(File.join(File.dirname(__FILE__), "../../config/#{CONFIG_NAME}.config.yml"))) -CONFIG['user'] = ENV['BROWSERSTACK_USERNAME'] || CONFIG['user'] -CONFIG['key'] = ENV['BROWSERSTACK_ACCESS_KEY'] || CONFIG['key'] - Capybara.register_driver :browserstack do |app| - @caps = CONFIG['common_caps'].merge(CONFIG['browser_caps'][TASK_ID]) - # Code to start browserstack local before start of test - if @caps['browserstack.local'] && @caps['browserstack.local'].to_s == 'true' - @bs_local = BrowserStack::Local.new - bs_local_args = { 'key' => (CONFIG['key']).to_s } - @bs_local.start(bs_local_args) - end - Capybara::Selenium::Driver.new(app, - browser: :remote, - url: "https://#{CONFIG['user']}:#{CONFIG['key']}@#{CONFIG['server']}/wd/hub", - desired_capabilities: @caps) + browser: :remote, + url: "https://hub.browserstack.com/wd/hub") + # SDK injects credentials, browser caps, build, project, local, etc. automatically end Capybara.default_driver = :browserstack Capybara.run_server = false - -# Code to stop browserstack local after end of test -at_exit do - @bs_local.stop unless @bs_local.nil? -end