diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..ee3d590 --- /dev/null +++ b/Gemfile @@ -0,0 +1,6 @@ +source "https://www.rubygems.org" + +gem 'selenium-webdriver' +gem 'rake' +gem 'test-unit' +gem 'browserstack-local' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..69d0199 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,29 @@ +GEM + remote: https://www.rubygems.org/ + specs: + browserstack-local (0.1.1) + childprocess (0.5.9) + ffi (~> 1.0, >= 1.0.11) + ffi (1.9.10) + power_assert (0.3.0) + rake (11.1.2) + rubyzip (1.2.0) + selenium-webdriver (2.53.0) + childprocess (~> 0.5) + rubyzip (~> 1.0) + websocket (~> 1.0) + test-unit (3.1.8) + power_assert + websocket (1.2.3) + +PLATFORMS + ruby + +DEPENDENCIES + browserstack-local + rake + selenium-webdriver + test-unit + +BUNDLED WITH + 1.11.2 diff --git a/README.md b/README.md index 472a4f7..b3c6a13 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,32 @@ # testunit-browserstack +======================= + Selenium examples for TestUnit and BrowserStack Automate + +This repository provides information and helpful tweaks to run your TestUnit tests on the BrowserStack selenium cloud infrastructure. + +###Configuration +Add rake, test-unit, browserstack-local gems into your Gemfile. +Run `bundle install`. + +### BrowserStack Authentication + +Export the environment variables for the username and access key of your BrowserStack account. +These can be found on the automate accounts page on [BrowserStack](https://www.browserstack.com/accounts/automate) + +`export BROWSERSTACK_USERNAME=` + +`export BROWSERSTACK_KEY=` + + +###Run tests +* Single Test: `rake name_of_test ` Example: `rake windows_firefox_40` +You see the other tests in the rakefile. +* Parallel Tests: `rake parallel_test` +* Local test: To run local tests, just set the `browserstack.local` capability to true in the test. + +###Further Reading +- [TestUnit](http://ruby-doc.org/stdlib-1.8.7/libdoc/test/unit/rdoc/Test/Unit.html) +- [BrowserStack documentation for Automate](https://www.browserstack.com/automate/ruby) + +Happy Testing! diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..a0d4108 --- /dev/null +++ b/Rakefile @@ -0,0 +1,29 @@ + +def run_tests(platform, browser, version) + system("os=\"#{platform}\" browser=\"#{browser}\" browser_version=\"#{version}\" ruby test.rb") +end + +task :windows_chrome_43 do + run_tests('Windows', 'chrome', '43') +end + +task :windows_firefox_40 do + run_tests('Windows', 'firefox','40') +end + +task :windows_chrome_45 do + run_tests('Windows', 'chrome','45') +end + +task :windows_firefox_39 do + run_tests('Windows', 'firefox','39') +end + +multitask :parallel_test => [ + :windows_chrome_43, + :windows_firefox_40, + :windows_chrome_45, + :windows_firefox_39 + ] do + puts 'Running parallel Tests' +end diff --git a/test.rb b/test.rb new file mode 100644 index 0000000..9b7d976 --- /dev/null +++ b/test.rb @@ -0,0 +1,33 @@ +require 'rubygems' +require 'selenium-webdriver' +require 'test-unit' +require 'browserstack/local' + +class SampleTests < Test::Unit::TestCase + def setup + caps = Selenium::WebDriver::Remote::Capabilities.new + caps["browser"] = "#{ENV['browser']}" + caps["browser_version"] = "#{ENV['browser_version']}" + caps["os"] = "#{ENV['os']}" + caps["browserstack.local"] = "false" + if caps['browserstack.local'] && caps['browserstack.local'] == 'true'; + @bs_local = BrowserStack::Local.new + bs_local_args = { "key" => "#{BROWSERSTACK_KEY}", "forcelocal" => true } + @bs_local.start(bs_local_args) + end + + url = "http://#{BROWSERSTACK_USERNAME}:#{BROWSERSTACK_KEY}@hub.browserstack.com/wd/hub" + @driver = Selenium::WebDriver.for(:remote, :url => url, :desired_capabilities => caps) + end + + def test_post + @driver.get "https://rubygems.org/gems/selenium-webdriver" + title = @driver.title() + assert_equal(title, "selenium-webdriver | RubyGems.org | your community gem host") + end + + def teardown + @driver.quit + @bs_local.stop unless @bs_local.nil? + end +end \ No newline at end of file