├── .gitignore ├── .ruby-version ├── Gemfile ├── Gemfile.lock ├── LICENSE.md ├── README.md ├── Rakefile ├── config └── cucumber.yml └── features ├── main.feature ├── step_definitions ├── assertion_steps.rb ├── main_steps.rb └── sync_steps.rb └── support ├── env.rb ├── helpers.rb ├── hooks.rb └── urls.rb /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle 2 | /vendor 3 | /results 4 | -------------------------------------------------------------------------------- /.ruby-version: -------------------------------------------------------------------------------- 1 | 2.5.0 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'http://rubygems.org' 2 | 3 | gem 'bundler' 4 | gem 'chronic' 5 | gem 'cucumber' 6 | gem 'headless' 7 | gem 'nokogiri' 8 | gem 'rake' 9 | gem 'selenium-webdriver' 10 | gem 'syntax' 11 | gem 'watir', '~> 6.0' 12 | gem 'page-object' 13 | gem 'minitest' 14 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: http://rubygems.org/ 3 | specs: 4 | backports (3.11.1) 5 | builder (3.2.3) 6 | childprocess (0.8.0) 7 | ffi (~> 1.0, >= 1.0.11) 8 | chronic (0.10.2) 9 | concurrent-ruby (1.0.5) 10 | cucumber (3.0.1) 11 | builder (>= 2.1.2) 12 | cucumber-core (~> 3.0.0) 13 | cucumber-expressions (~> 4.0.3) 14 | cucumber-wire (~> 0.0.1) 15 | diff-lcs (~> 1.3) 16 | gherkin (~> 4.0) 17 | multi_json (>= 1.7.5, < 2.0) 18 | multi_test (>= 0.1.2) 19 | cucumber-core (3.0.0) 20 | backports (>= 3.8.0) 21 | cucumber-tag_expressions (>= 1.0.1) 22 | gherkin (>= 4.1.3) 23 | cucumber-expressions (4.0.4) 24 | cucumber-tag_expressions (1.1.1) 25 | cucumber-wire (0.0.1) 26 | data_magic (1.1) 27 | faker (>= 1.1.2) 28 | yml_reader (>= 0.6) 29 | diff-lcs (1.3) 30 | faker (1.8.7) 31 | i18n (>= 0.7) 32 | ffi (1.9.18) 33 | gherkin (4.1.3) 34 | headless (2.3.1) 35 | i18n (0.9.3) 36 | concurrent-ruby (~> 1.0) 37 | mini_portile2 (2.3.0) 38 | minitest (5.11.3) 39 | multi_json (1.13.1) 40 | multi_test (0.1.2) 41 | nokogiri (1.8.2) 42 | mini_portile2 (~> 2.3.0) 43 | page-object (2.2.4) 44 | page_navigation (>= 0.10) 45 | selenium-webdriver (~> 3.0) 46 | watir (~> 6.8) 47 | page_navigation (0.10) 48 | data_magic (>= 0.22) 49 | rake (12.3.0) 50 | rubyzip (1.2.1) 51 | selenium-webdriver (3.8.0) 52 | childprocess (~> 0.5) 53 | rubyzip (~> 1.0) 54 | syntax (1.2.2) 55 | watir (6.10.3) 56 | selenium-webdriver (~> 3.4, >= 3.4.1) 57 | yml_reader (0.7) 58 | 59 | PLATFORMS 60 | ruby 61 | 62 | DEPENDENCIES 63 | bundler 64 | chronic 65 | cucumber 66 | headless 67 | minitest 68 | nokogiri 69 | page-object 70 | rake 71 | selenium-webdriver 72 | syntax 73 | watir (~> 6.0) 74 | 75 | BUNDLED WITH 76 | 1.16.1 77 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License 2 | =============== 3 | 4 | Copyright (c) 2014 Modus Create, Inc. 5 | ------------------------------------- 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Cucumber-Watir Test Harness 2 | =========================== 3 | 4 | This is a test harness for automated functional testing of web applications, using the [Cucumber](http://cukes.info/) and [Watir Webdriver](http://watirwebdriver.com/) test frameworks. It can be used to support Behavioral Driven Design (BDD) or just automated functional testing of arbitrary web applications. 5 | 6 | By using this collection of frameworks, you can quickly start a suite of functional tests for an application using a basic vocabulary inspired by tests that have been used successfully on many production projects. 7 | 8 | The built in steps in `features/step-definitions/common_steps.rb` cover a variety of common use cases that occur in the functional testing of web applications, including common operations such as visiting pages, filling in form fields, finding text in a page, and checking that operations complete successfully. 9 | 10 | This approach might not be as pure as some would advocate for BDD, but it strikes a good balance between being implementation specific and being fast to write and execute. 11 | 12 | Here is an example of a Cucumber scenario written with the common steps: 13 | 14 | ``` 15 | @smoke @regression @desktop 16 | Scenario: User views profile page 17 | Given I am on the desktop home page 18 | Then I must see the page title "example.com" 19 | Given I fill in the text field "login_email" with "foobar@example.com" 20 | And I fill in the text field "login_password" with "password" 21 | And I click the "Login" span 22 | And I wait for the "Dashboard" text to be displayed 23 | Given I click the "foobar@example.com" link 24 | And I wait for the "Profile" text to be displayed 25 | And I send the "tab" keys 26 | And I send the "06/01/2014" keys 27 | And I send the "tab" keys 28 | And I send the "06/06/2014" keys 29 | And I click the "Save & Continue" span 30 | And I fill in the text field "short_description" with "Modus Kickstart short description" 31 | Then I must see the text "Thank You" displayed 32 | ``` 33 | 34 | License 35 | ======= 36 | 37 | This test framework is licensed under the [MIT License](http://opensource.org/licenses/MIT), a permissive free software license. 38 | 39 | See the file LICENSE.md for full copyright and license information. 40 | 41 | 42 | Setup on Mac OS X 43 | ================= 44 | 45 | Tools Used: 46 | ----------- 47 | 48 | 1. Mac with `sudo` Access 49 | 2. Xcode with command line tools installed. The command line tools can be installed from Xcode -> Preferences -> Downloads 50 | 3. RVM (optional) 51 | 4. Ruby 2.5.0 52 | 5. Watir-Webdriver 53 | 6. webdriver-user-agent 54 | 7. cucumber 55 | 8. homebrew (optional) 56 | 9. chromedriver 57 | 58 | 59 | Steps To Install: 60 | ----------------- 61 | 62 | ### Mac Instructions 63 | 64 | #### Install Xcode and then install command line tools. 65 | 66 | The command line tools can be installed in Xcode by going to Xcode-> Preferences -> Downloads 67 | 68 | #### Install RVM 69 | 70 | curl -L https://get.rvm.io | bash 71 | 72 | Using a Ruby version manager will isolate the changes required for testing from the system Ruby. If you don't use rvm, you might consider [rbenv](https://github.com/sstephenson/rbenv) instead. 73 | 74 | #### Install Ruby 75 | 76 | rvm install ruby-2.5.0 77 | 78 | To make this the default ruby on your Mac run the below command. However you do not need to make it the default ruby for the test scripts to run. The `.ruby-version` file in the folder has the builin workflow to use Ruby 2.5.0 when you cd into the tests/web folder. 79 | 80 | rvm use ruby-2.5.0 --default 81 | 82 | #### Install Homebrew 83 | 84 | ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)" 85 | brew doctor 86 | 87 | If the above command fails, please follow the steps in the output to fix the errors. 88 | 89 | Installing Homebrew is optional. These directions use it to install chromedriver, but you could also use `npm` to install chromedriver or install it manually. You can download chromedriver from https://chromedriver.storage.googleapis.com/index.html if you want to install it manually. 90 | 91 | #### Install chromedriver 92 | 93 | Install `chromedriver` using Homebrew or npm: 94 | 95 | brew install chromedriver 96 | # or if you prefer to install chromedriver through npm, do 97 | # npm install chromedriver -g 98 | 99 | Unzip and move the driver to any place in your path such as `/usr/local/bin`. 100 | 101 | The advantage of installing chromedriver via brew or npm is that you can update it with a single command. (`brew update && brew upgrade``) 102 | 103 | #### Install the bundler gem 104 | 105 | gem install bundler --no-ri --no-rdoc 106 | 107 | #### Install necessary gems using bundler 108 | 109 | bundle install 110 | 111 | ### Getting Started (Windows) 112 | 113 | The following instructions show how to bootstrap a Windows ruby environment. 114 | 115 | #### Download and Install Ruby 116 | Get Ruby 2.5.0-1 (or newer) from http://rubyinstaller.org/downloads/ 117 | 118 | #### Download and install chromedriver 119 | 120 | Get Chromedriver from http://chromedriver.storage.googleapis.com/index.html 121 | 122 | Make sure the chromedriver is in your PATH enviroment variable. 123 | 124 | #### From here you can follow the same instructions as above. 125 | 126 | #### Alternatively you can simply clone the below Git repo and run your tests. The package contains all gems and utilities built-in to run the tests. 127 | 128 | `https://github.com/ModusCreateOrg/ruby4windows.git` 129 | 130 | 131 | Running tests 132 | ------------- 133 | 134 | You can run the tests directly with the cucumber command, or with a set of preset command line options using the `rake` command. 135 | 136 | ### Run cucumber directly 137 | 138 | To run tests directly using Cucumber, simply issue the command: 139 | 140 | cucumber 141 | 142 | The above command runs all the tests inside the feature folder but that is not what you want sometimes. To run single individual tests you have to specify the line numbers as below:. 143 | 144 | cucumber features/main.feature:217 145 | 146 | This will run the test scenario at line number 217 in the file `features/main.feature`. 147 | 148 | ### Run cucumber using `rake` 149 | 150 | Rake uses tasks to collect a set of functions and commands. In this project, it is used to run cucumber with command line options suitable for testing out different groups of tagged features or 151 | 152 | To see a list of tasks, run: 153 | 154 | rake -T 155 | 156 | To run the whole regression suite, run: 157 | 158 | rake all 159 | 160 | An HTML report will be saved to the `results` folder with screenshots of any failures. 161 | 162 | 163 | Tips and Troubleshooting: 164 | -------------------------- 165 | 166 | ### Sencha Command integration 167 | 168 | If you are using [Sencha Command](http://www.sencha.com/products/sencha-cmd), note that `sencha` may need ruby < 2.0 to be able to build apps successfully. 169 | 170 | If you have made Ruby 2.0 your default then run the below command before you use the sencha build script. 171 | 172 | rvm use 1.9.3 173 | 174 | ### Bash alias for running tests 175 | 176 | You can simply create Alias in your bash profile to run tests. Instead of typing the whole cucumber command create this function in your `.bashrc` or `.profile` file: 177 | 178 | echo 'c() { cucumber features/main.feature:"$@" ;}' >> ~/.bashrc 179 | . ~/.bashrc 180 | 181 | Then you can run the test at line 217 by simply running the command below from the test folder: 182 | 183 | c 217 184 | 185 | ### Libxml errors 186 | 187 | If you run into libxml erorrs fix it as suggested in this post 188 | 189 | https://gist.github.com/vparihar01/5856524 190 | 191 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'cucumber/rake/task' 2 | 3 | require 'fileutils' 4 | FileUtils::mkdir_p 'results' 5 | 6 | task :default => 'all' 7 | 8 | Cucumber::Rake::Task.new(:all, 'Runs all tests') do |t| 9 | t.cucumber_opts = "features/*.feature 10 | --format html --out=results/all-report-#{Time.now.strftime("%Y-%m-%d-%H-%M-%S")}.html 11 | --format pretty --no-source 12 | --format rerun --out results/all-rerun.txt 13 | --tag @regression" 14 | end 15 | 16 | Cucumber::Rake::Task.new(:smoke, 'Runs the smoke test suite') do |t| 17 | t.cucumber_opts = "features/*.feature 18 | --format html --out=results/smoke-report-#{Time.now.strftime("%Y-%m-%d-%H-%M-%S")}.html 19 | --format pretty --no-source 20 | --format rerun --out results/smoke-rerun.txt 21 | --tag @smoke" 22 | end 23 | 24 | Cucumber::Rake::Task.new(:jenkins_all, 'Run all tests for Jenkins CI') do |t| 25 | t.cucumber_opts = "features/*.feature 26 | --format html --out=results/jenkins-all-report-#{Time.now.strftime("%Y-%m-%d-%H-%M-%S")}.html 27 | --format pretty --no-source 28 | --format rerun --out results/jenkins-rerun.txt 29 | --tag @regression 30 | 'HEADLESS=true'" 31 | end 32 | 33 | 34 | Cucumber::Rake::Task.new(:regression, 'Runs tests tagged as regression') do |t| 35 | t.cucumber_opts = "features/* 36 | --format html --out=results/regression-report.html 37 | --format pretty --no-source 38 | --format rerun --out results/regression-rerun.txt 39 | --tag @regression" 40 | end 41 | 42 | Cucumber::Rake::Task.new(:desktop, 'Runs test tagged as desktop') do |t| 43 | t.cucumber_opts = "features/* 44 | --format html --out=results/desktop-report.html 45 | --format pretty --no-source 46 | --format rerun --out results/desktop-rerun.txt 47 | --tag @desktop" 48 | end 49 | 50 | Cucumber::Rake::Task.new(:mobile, 'Runs test tagged as mobile') do |t| 51 | t.cucumber_opts = "features/* 52 | --format html --out=results/mobile-report.html 53 | --format pretty --no-source 54 | --format rerun --out results/mobile-rerun.txt 55 | --tag @mobile" 56 | end 57 | 58 | Cucumber::Rake::Task.new(:tablet, 'Runs test tagged as tablet') do |t| 59 | t.cucumber_opts = "features/* 60 | --format html --out=results/tablet-report.html 61 | --format pretty --no-source 62 | --format rerun --out results/tablet-rerun.txt 63 | --tag @tablet" 64 | end 65 | 66 | -------------------------------------------------------------------------------- /config/cucumber.yml: -------------------------------------------------------------------------------- 1 | default: features --tag "not @wip" --tag "not @manual" 2 | -------------------------------------------------------------------------------- /features/main.feature: -------------------------------------------------------------------------------- 1 | Feature: Main tests 2 | In order to make sure that the web page is working 3 | As a normal user 4 | I want to check out the home page in a variety of aspect ratios 5 | 6 | @smoke @regression @desktop 7 | Scenario: Visit the home page 8 | Given I am on the desktop home page 9 | Then I must see the page title "Modus Labs by Modus Create" 10 | Then I click the "OK" button for accept cookies if it is displayed 11 | When I click the "Cucumber Watir" link 12 | Then I wait for the "test harness" text to be displayed 13 | 14 | @regression @mobile 15 | Scenario: Visit the home page on a mobile device 16 | Given I am on the desktop home page 17 | Then I must see the page title "Modus Labs by Modus Create" 18 | Then I click the "OK" button for accept cookies if it is displayed 19 | When I click the "Cucumber Watir" link 20 | Then I wait for the "test harness" text to be displayed 21 | 22 | @wip @tablet 23 | Scenario: Visit the home page on a tablet 24 | Given I am on the desktop home page 25 | Then I must see the page title "Modus Labs by Modus Create" 26 | Then I click the "OK" button for accept cookies if it is displayed 27 | When I click the "Cucumber Watir" link 28 | Then I wait for the "test harness" text to be displayed 29 | -------------------------------------------------------------------------------- /features/step_definitions/assertion_steps.rb: -------------------------------------------------------------------------------- 1 | Then(/^I must see the text "(.*?)" displayed$/) do |text| 2 | assert_equal(true,@browser.text.include?(text)) 3 | end 4 | 5 | Then(/^I should not see the text "(.*?)" displayed$/) do |text| 6 | assert_equal(false,@browser.text.include?(text)) 7 | end 8 | 9 | Then(/^I must see the page title "(.*?)"$/) do |title| 10 | assert_equal(title,@browser.title) 11 | end -------------------------------------------------------------------------------- /features/step_definitions/main_steps.rb: -------------------------------------------------------------------------------- 1 | Given /^I am on (.+)$/ do |page_name| 2 | @browser.goto(path_to(page_name)) 3 | puts "URL = #{@browser.url}" 4 | end 5 | 6 | # Text Field 7 | Given(/^I fill in the text field "(.*?)" with "(.*?)"$/) do |ele, text| 8 | case 9 | when @browser.text_field(:id, ele).exists? 10 | @browser.text_field(:id, ele).set text 11 | 12 | when @browser.text_field(:placeholder, ele).exists? 13 | @browser.text_field(:placeholder, ele).set text 14 | 15 | when @browser.text_field(:class, ele).exists? 16 | @browser.text_field(:class, ele).set text 17 | 18 | when @browser.text_field(:name, ele).exists? 19 | @browser.text_field(:name, ele).set text 20 | 21 | else 22 | fail("Text Field => #{ele} NOT FOUND") 23 | end 24 | end 25 | 26 | # Button 27 | Given(/^I click the "(.*?)" button$/) do |ele| 28 | case 29 | when @browser.button(:id, ele).exists? 30 | @browser.button(:id, ele).click 31 | 32 | when @browser.button(:text, ele).exists? 33 | @browser.button(:text, ele).click 34 | 35 | when @browser.button(:class, ele).exists? 36 | @browser.button(:class, ele).click 37 | 38 | when @browser.button(:name, ele).exists? 39 | @browser.button(:name, ele).click 40 | 41 | when @browser.button(:value, ele).exists? 42 | @browser.button(:value, ele).click 43 | 44 | else 45 | fail("Button => #{ele} NOT FOUND") 46 | end 47 | end 48 | 49 | #Link 50 | Given(/^I click the "(.*?)" link$/) do |ele| 51 | case 52 | when @browser.link(:id, ele).exists? 53 | @browser.link(:id, ele).click 54 | 55 | when @browser.link(:text, ele).exists? 56 | @browser.link(:text, ele).click 57 | 58 | when @browser.link(:class, ele).exists? 59 | @browser.link(:class, ele).click 60 | 61 | when @browser.link(:href, ele).exists? 62 | @browser.link(:href, ele).click 63 | 64 | else 65 | fail("Link => #{ele} NOT FOUND") 66 | end 67 | end 68 | 69 | #Radio Button 70 | Given(/^I click the "(.*?)" radio button/) do |ele| 71 | case 72 | when @browser.radio(:id, ele).exists? 73 | @browser.radio(:id, ele).click 74 | 75 | when @browser.radio(:name, ele).exists? 76 | @browser.radio(:name, ele).click 77 | 78 | when @browser.radio(:class, ele).exists? 79 | @browser.radio(:class, ele).click 80 | 81 | else 82 | fail("Radio Button => #{ele} NOT FOUND") 83 | end 84 | end 85 | 86 | #Checkbox 87 | Given(/^I click the "(.*?)" checkbox/) do |ele| 88 | case 89 | when @browser.checkbox(:id, ele).exists? 90 | @browser.checkbox(:id, ele).click 91 | 92 | when @browser.checkbox(:name, ele).exists? 93 | @browser.checkbox(:name, ele).click 94 | 95 | when @browser.checkbox(:class, ele).exists? 96 | @browser.checkbox(:class, ele).click 97 | 98 | else 99 | fail("checkbox => #{ele} NOT FOUND") 100 | end 101 | end 102 | 103 | #Span 104 | Given(/^I click the "(.*?)" span$/) do |ele| 105 | case 106 | when @browser.span(:id, ele).exists? 107 | @browser.span(:id, ele).click 108 | 109 | when @browser.span(:text, ele).exists? 110 | @browser.span(:text, ele).click 111 | 112 | when @browser.span(:class, ele).exists? 113 | @browser.span(:class, ele).click 114 | 115 | else 116 | fail("Span => #{ele} NOT FOUND") 117 | end 118 | end 119 | 120 | #Div 121 | Given(/^I click the "(.*?)" div/) do |ele| 122 | case 123 | when @browser.div(:id, ele).exists? 124 | @browser.div(:id, ele).click 125 | 126 | when @browser.div(:text, ele).exists? 127 | @browser.div(:text, ele).click 128 | 129 | when @browser.div(:class, ele).exists? 130 | @browser.div(:class, ele).click 131 | 132 | else 133 | fail("Div => #{ele} NOT FOUND") 134 | end 135 | end 136 | 137 | Given(/^I click the "(.*?)" icon$/) do |ele| 138 | case 139 | when @browser.i(:id, ele).exists? 140 | @browser.i(:id, ele).click 141 | 142 | when @browser.i(:text, ele).exists? 143 | @browser.i(:text, ele).click 144 | 145 | when @browser.i(:class, ele).exists? 146 | @browser.i(:class, ele).click 147 | 148 | else 149 | fail("Icon => #{ele} NOT FOUND") 150 | end 151 | end 152 | 153 | #handle the cookie alert by pressing ok if displayed 154 | Then(/^I click the "(.*?)" button for accept cookies if it is displayed$/) do |ele| 155 | case 156 | when 157 | @browser.a(:text, ele). exists? 158 | @browser.a(:text, ele).click 159 | else 160 | fail("Icon => #{ele} NOT FOUND") 161 | end 162 | end -------------------------------------------------------------------------------- /features/step_definitions/sync_steps.rb: -------------------------------------------------------------------------------- 1 | 2 | 3 | Given(/^I wait for the "(.*?)" text to be displayed$/) do |text| 4 | Watir::Wait.until { @browser.text.include? text } 5 | end 6 | 7 | Given(/^I wait for the menu to be displayed$/) do 8 | @browser.ul(:class => "off-canvas-list").wait_until_present 9 | end -------------------------------------------------------------------------------- /features/support/env.rb: -------------------------------------------------------------------------------- 1 | require 'watir' 2 | require 'selenium-webdriver' 3 | require 'minitest/test' 4 | 5 | if ENV['HEADLESS'] 6 | require 'headless' 7 | headless = Headless.new 8 | headless.start 9 | at_exit do 10 | headless.destroy 11 | end 12 | end 13 | 14 | # custom attribute can now be used for locating objects. I found the solution here 15 | # http://jkotests.wordpress.com/2012/09/04/locate-element-via-custom-attribute-extending-watir-webdriver/ 16 | module Watir 17 | module Locators 18 | class Element 19 | class SelectorBuilder 20 | alias :old_normalize_selector :normalize_selector 21 | 22 | def normalize_selector(how, what) 23 | case how 24 | when :'ng-model' 25 | [how, what] 26 | else 27 | old_normalize_selector(how, what) 28 | end 29 | end 30 | end 31 | end 32 | end 33 | end 34 | 35 | World(MiniTest::Assertions) 36 | -------------------------------------------------------------------------------- /features/support/helpers.rb: -------------------------------------------------------------------------------- 1 | module WebdriverHelpers 2 | 3 | end 4 | 5 | World(WebdriverHelpers) -------------------------------------------------------------------------------- /features/support/hooks.rb: -------------------------------------------------------------------------------- 1 | # For more information on writing hooks please read the cuke wiki 2 | # https://github.com/cucumber/cucumber/wiki/Hooks 3 | 4 | RESULTS_DIR="results" 5 | TIMEOUT=180 6 | 7 | if ENV["DRIVER"] 8 | DRIVER = ENV["DRIVER"] 9 | else 10 | #DRIVER = "firefox" 11 | DRIVER = "chrome" 12 | end 13 | 14 | # This method to set timeout came from 15 | # http://stackoverflow.com/questions/9014121/how-do-i-change-the-page-load-timeouts-in-watir-webdriver-timeout-in-click-met/9044958#9044958 16 | # however, it's not working as expected :( 17 | # Maybe we can try solutions outlined here instead: 18 | # http://stackoverflow.com/questions/17242404/watir-implicit-wait-doesnt-seem-to-work 19 | # http://stackoverflow.com/questions/18204926/automate-timeout-handling-wait-and-refresh-in-watir 20 | # http://stackoverflow.com/questions/18659847/reload-page-until-it-has-some-element-using-ruby-selenium-webdriver/18660166#18660166 21 | def get_browser 22 | client = Selenium::WebDriver::Remote::Http::Default.new 23 | client.open_timeout = TIMEOUT # seconds – default is 30 24 | browser = Watir::Browser.new DRIVER.to_sym, :http_client => client 25 | return browser 26 | end 27 | 28 | Before ('@desktop') do 29 | @browser = get_browser 30 | screen_width = @browser.execute_script("return screen.width;") 31 | screen_height = @browser.execute_script("return screen.height;") 32 | @browser.driver.manage.window.resize_to(screen_width,screen_height) 33 | end 34 | 35 | Before ('@mobile') do 36 | @browser = get_browser 37 | if DRIVER == "chrome" 38 | @browser.window.resize_to(640,960) 39 | else 40 | @browser.window.maximize 41 | end 42 | end 43 | 44 | Before ('@tablet') do 45 | @browser = get_browser 46 | if DRIVER == "chrome" 47 | @browser.window.resize_to(1024,768) 48 | else 49 | @browser.window.maximize 50 | end 51 | end 52 | 53 | After do 54 | @browser.close 55 | end 56 | 57 | After do |scenario| 58 | if(scenario.failed?) 59 | if @browser && @browser.screenshot 60 | @browser.screenshot.save("#{RESULTS_DIR}/#{scenario.__id__}.png") 61 | Dir.chdir(RESULTS_DIR) do 62 | embed("#{scenario.__id__}.png", "image/png", "SCREENSHOT") 63 | end 64 | end 65 | end 66 | end 67 | -------------------------------------------------------------------------------- /features/support/urls.rb: -------------------------------------------------------------------------------- 1 | module Urls 2 | 3 | # Change this to switch environments 4 | TARGET = :Production 5 | #TARGET = :Local 6 | #TARGET = :LocalSSL 7 | #TARGET = :Guest 8 | 9 | BASE_URL = { 10 | :Production => "http://labs.moduscreate.com/", 11 | :Local => "http://localhost:4000/", 12 | :LocalSSL => "https://localhost:8443/", 13 | :Guest => "http://localhost:80/", 14 | } 15 | 16 | def path_to(page_name) 17 | if ENV["BASE_URL"] 18 | base_url = ENV["BASE_URL"] 19 | else 20 | base_url = BASE_URL[TARGET] 21 | end 22 | case page_name 23 | when /the desktop home page/i 24 | base_url 25 | when /the duckduckgo home page/i 26 | base_url="http://duckduckgo.com" 27 | else 28 | raise "Can't find mapping from \"#{page_name}\" to a url.\n" + 29 | "Please add a mapping in #{__FILE__}" 30 | end 31 | end 32 | 33 | end 34 | 35 | World(Urls) 36 | --------------------------------------------------------------------------------