diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e68aa8c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.idea/ +Gemfile.lock +results.html +test.rb +screenshots/ \ No newline at end of file diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..a71c3e0 --- /dev/null +++ b/Gemfile @@ -0,0 +1,19 @@ +source 'https://rubygems.org' + +gem 'cucumber' +gem 'rspec' +gem 'selenium-webdriver' +gem 'watir-webdriver' +gem 'require_all' +gem 'activesupport' + + +# gem to connect to saucelabs and also dynamically change browser parameters +gem 'saucelabs' + +gem 'nokogiri' + + + + + diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..83fb128 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2014 Pradeep K. Macharla + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..be3235a --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +Training +======== + +This project is being used to demonstrate modules or keyword framework pattern for Test Automation in Ruby + +Instruction to use +=================== +1) Install Ruby (1.9 or higher) +2) gem install cucumber +3) gem install bundler +4) check out the project +5) From project root folder, "bundle exec cucumber features" + + +Known Issues +===================== +Hooks.rb code launches browser even for non-browser scenarios, however it closes it down too + + +Instructions to use +===================== + +1) Checkout the project +2) Run bundle install +3) Go through .feature files +4) Check step defnitions + + +## Contributing + +1. Fork it +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create new Pull Request diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..41861da --- /dev/null +++ b/Rakefile @@ -0,0 +1,9 @@ +require 'rubygems' +require 'cucumber' +require 'cucumber/rake/task' + +Cucumber::Rake::Task.new(:features) do |t| + t.profile = 'default' +end + +task :default => :features \ No newline at end of file diff --git a/cucumber.yml b/cucumber.yml new file mode 100644 index 0000000..d9e2ebb --- /dev/null +++ b/cucumber.yml @@ -0,0 +1 @@ +default: --no-source --color --format pretty --format html --out results.html \ No newline at end of file diff --git a/features/KeywordDriven.feature b/features/KeywordDriven.feature new file mode 100644 index 0000000..db8e014 --- /dev/null +++ b/features/KeywordDriven.feature @@ -0,0 +1,14 @@ +Feature: Sign in and contact us + + Scenario: Successful login + When I open "http://automationpractice.com" website + Then I login using credentials "abc@xyz.com" and "Test@123" + + Scenario: unsuccessful login + When I open "http://automationpractice.com" website + Then I login using credentials "abc@xyz1.com" and "Test@123" + + Scenario: Contact us + When I open "http://automationpractice.com" website + Then I contact customer service with order reference "abc123" and message "hello there" + diff --git a/features/step_definitions/keyword_driven.rb b/features/step_definitions/keyword_driven.rb new file mode 100644 index 0000000..968d0fa --- /dev/null +++ b/features/step_definitions/keyword_driven.rb @@ -0,0 +1,11 @@ +When(/^I open "([^"]*)" website$/) do |url| + visit url +end + +Then(/^I login using credentials "([^"]*)" and "([^"]*)"$/) do |username, password| + sign_in(username,password) +end + +Then(/^I contact customer service with order reference "([^"]*)" and message "([^"]*)"$/) do |order_ref, message| + contact_customer_service(order_ref,message) +end \ No newline at end of file diff --git a/features/support/env.rb b/features/support/env.rb new file mode 100644 index 0000000..cbe8953 --- /dev/null +++ b/features/support/env.rb @@ -0,0 +1,18 @@ +require 'watir-webdriver' +require 'rspec' +require 'require_all' +require 'saucelabs' +require 'nokogiri' + +require './lib/data_helper' +require './features/support/modules/business_modules' + +OR = YAML.load_file './lib/config/object_repository.yml' + + +World DataHelper +World Keywords + + + + diff --git a/features/support/hooks.rb b/features/support/hooks.rb new file mode 100644 index 0000000..96de928 --- /dev/null +++ b/features/support/hooks.rb @@ -0,0 +1,32 @@ +require 'watir-webdriver' +require 'selenium-webdriver' +include SauceLabs + + +Before do + + ENV['BROWSER'] = "chrome" if ENV['BROWSER'].nil? + if(ENV['BROWSER']=='firefox') + profile = Selenium::WebDriver::Firefox::Profile.from_name 'seleniumprofile' + @browser = Watir::Browser.new :firefox, :profile => profile + else + @browser = Watir::Browser.new ENV['BROWSER'].to_sym + end + @browser.window.maximize +end + + +After do |scenario| + if scenario.failed? + Dir::mkdir('screenshots') if not File.directory?('screenshots') + screenshot = "./screenshots/FAILED_#{scenario.name.gsub(' ','_').gsub(/[^0-9A-Za-z_]/, '')}.png" + @browser.driver.save_screenshot(screenshot) + embed screenshot, 'image/png' + end + @browser.cookies.clear + @browser.quit + +end + + + diff --git a/features/support/modules/business_modules.rb b/features/support/modules/business_modules.rb new file mode 100644 index 0000000..323548d --- /dev/null +++ b/features/support/modules/business_modules.rb @@ -0,0 +1,24 @@ +module Keywords + + def visit url + @browser.goto url + end + + def sign_in(username,password) + @browser.link(text: OR['signin_text']).click + @browser.text_field(id: OR['username_id']).set username + @browser.text_field(id: OR['password_id']).set password + @browser.button(id: OR['submit_login_id']).click + expect(@browser.link(text: "Sign out").present?).to be_truthy + end + + def contact_customer_service(order_ref,message) + @browser.link(text: OR['contact_us_text']).click + + @browser.select(id: OR['subject_heading_id']).select "Customer service" + @browser.text_field(id: OR['email_id']).set "pradeep@seleniumframework.com" + @browser.text_field(id: OR['order_reference_id']).set order_ref + @browser.text_field(id: OR['message_id']).set message + @browser.button(id: OR['submit_message_id']).click + end +end \ No newline at end of file diff --git a/lib/config/data/default.yml b/lib/config/data/default.yml new file mode 100644 index 0000000..0d33c8d --- /dev/null +++ b/lib/config/data/default.yml @@ -0,0 +1,4 @@ +username: abc@xyz.com +password: Test@123 +order_reference: 123456 +message: Keyword driven framework \ No newline at end of file diff --git a/lib/config/object_repository.yml b/lib/config/object_repository.yml new file mode 100644 index 0000000..f08ea6c --- /dev/null +++ b/lib/config/object_repository.yml @@ -0,0 +1,11 @@ +signin_text: Sign in +username_id: email +password_id: passwd +submit_login_id: SubmitLogin + +contact_us_text: Contact us +subject_heading_id: id_contact +email_id: email +order_reference_id: id_order +message_id: message +submit_message_id: submitMessage diff --git a/lib/data_helper.rb b/lib/data_helper.rb new file mode 100644 index 0000000..0ebf120 --- /dev/null +++ b/lib/data_helper.rb @@ -0,0 +1,58 @@ +module DataHelper + + # Read basics on YAML - http://www.seleniumframework.com/test-data/test-data-with-yaml/ + def data_yml_hash + @data_yml = YAML.load_file "#{data_default_directory}/#{yml_file}" + end + + # Read basics on excel parsing - http://www.seleniumframework.com/test-data/test-data-with-excel/ + def data_excel_hash (data_sheet) + workbook = RubyXL::Parser.parse data_default_directory+'/'+excel_file + sheet = workbook[data_sheet] + header_row = sheet.sheet_data[0] + @data_excel = sheet.get_table(header_row) + end + + # Json parsing reference - http://www.seleniumframework.com/test-data/test-data-with-json/ + def data_json_hash + @data_json = JSON.parse data_default_directory+'/'+json_file + end + + def set_data_directory(dir) + @data_directory = dir + end + + + private + + def yml_file + ENV['DATA_YML_FILE']? ENV['DATA_YML_FILE'] : 'default.yml' + end + + def excel_file + ENV['DATA_EXCEL_FILE']?ENV['DATA_EXCEL_FILE'] : 'default.xlsx' + end + + def json_file + ENV['DATA_JSON_FILE']?ENV['DATA_JSON_FILE'] : 'default.json' + end + + def xml_file + ENV['DATA_XML_FILE']?ENV['DATA_XML_FILE'] : 'default.xml' + end + + + def data_default_directory + @data_directory ||= 'lib/config/data' + end + + + class << self + + attr_accessor :data_yml,:data_excel,:data_json,:data_xml, :data_directory + + end + + + +end \ No newline at end of file