diff --git a/sample-code/examples/ruby/Gemfile b/sample-code/examples/ruby/Gemfile index e5481cede..270a34137 100644 --- a/sample-code/examples/ruby/Gemfile +++ b/sample-code/examples/ruby/Gemfile @@ -2,4 +2,5 @@ source "https://www.rubygems.org" gem 'appium_lib' gem "rest-client" -gem "cucumber" \ No newline at end of file +gem "cucumber" +gem "rspec-expectations" diff --git a/sample-code/examples/ruby/cucumber/features/step_definitions/steps.rb b/sample-code/examples/ruby/cucumber/features/step_definitions/steps.rb index 76a11d4fd..f60d11126 100644 --- a/sample-code/examples/ruby/cucumber/features/step_definitions/steps.rb +++ b/sample-code/examples/ruby/cucumber/features/step_definitions/steps.rb @@ -6,10 +6,14 @@ # # The instructions in the step are then executed with those variables. # -# In this example, we're using rspec's assertions to test that things are happening, but you can use any ruby code you want in the steps. +# In this example, we're using rspec's assertions to test that things are happening, +# but you can use any ruby code you want in the steps. # -# The 'selenium' object is our webdriver, set up in the cucumber/support/env.rb +# The '$driver' object is the appium_lib driver, set up in the cucumber/support/env.rb # file, which is a convenient place to put it as we're likely to use it often. +# This is a different use to most of the examples; Cucumber steps are instances +# of `Object`, and extending Object with Appium methods (through +# `promote_appium_methods`) is a bad idea. # # For more on step definitions, check out the documentation at # https://github.com/cucumber/cucumber/wiki/Step-Definitions @@ -17,18 +21,32 @@ # For more on rspec assertions, check out # https://www.relishapp.com/rspec/rspec-expectations/docs - Given /^I have entered (\d+) into field (\d+) of the calculator$/ do |value, field| - elements = selenium.find_elements(:class_name, "UIATextField") - elements[field.to_i - 1].send_keys value + # Get a textfield by index + textfield = textfield(field.to_i) + textfield.send_keys value +end + +Given /^I have entered (\d+) into a field of the calculator showing (\w+)$/ do |value, field| + # Get a textfield by string + textfield = textfield field + textfield.send_keys value end And /^I press button (\d+)$/ do |button_index| - button = selenium.find_elements(:class_name, "UIAButton")[button_index.to_i - 1 ] + # Find a button by index + button = button(button_index.to_i) + button.click +end + +And /^I press a button labelled (\w+)$/ do |button_text| + # Find a button by text + button = button button_text button.click end Then /^the result should be displayed as (\d+)$/ do |expected| - result = selenium.find_element(:class_name, "UIAStaticText") + # You can get just the first of a class of elements + result = first_s_text result.attribute("value").should eq expected end diff --git a/sample-code/examples/ruby/cucumber/features/support/appium.txt b/sample-code/examples/ruby/cucumber/features/support/appium.txt new file mode 100644 index 000000000..d0e714868 --- /dev/null +++ b/sample-code/examples/ruby/cucumber/features/support/appium.txt @@ -0,0 +1,5 @@ +[caps] +platformName = "ios" +device = "iPhone Simulator" +platformVersion = "7.1" +app = "../../../apps/TestApp/build/release-iphonesimulator/TestApp.app" \ No newline at end of file diff --git a/sample-code/examples/ruby/cucumber/features/support/env.rb b/sample-code/examples/ruby/cucumber/features/support/env.rb index 5b762148e..4a6c56036 100644 --- a/sample-code/examples/ruby/cucumber/features/support/env.rb +++ b/sample-code/examples/ruby/cucumber/features/support/env.rb @@ -1,6 +1,3 @@ -# WHAT THIS FILE IS -# ----------------- -# # This file provides setup and common functionality across all features. It's # included first before every test run, and the methods provided here can be # used in any of the step definitions used in a test. This is a great place to @@ -8,35 +5,21 @@ # test with, and the setup of selenium. require 'rspec/expectations' -require 'selenium-webdriver' +require 'appium_lib' +require 'cucumber/ast' -# Where our app lives, relative to this file -APP_PATH = '../../../../../apps/TestApp/build/release-iphonesimulator/TestApp.app' - -# What we need as a capability --> iOS device, where our app is, ect. -def desired_caps - { - 'browserName' => '', - 'platform' => 'Mac', - 'device' => 'iPhone Simulator', - 'version' => '7.1', - 'app' => absolute_app_path - } -end - -# Make sure the path above is relative to this file -def absolute_app_path - File.join(File.dirname(__FILE__), APP_PATH) +# Create a custom World class so we don't pollute `Object` with Appium methods +class AppiumWorld end -# The location of our selenium (or in this case, Appium) file -def server_url - "http://127.0.0.1:4723/wd/hub" -end - -# Set up a driver or, if one exists, return it -def selenium - @driver ||= Selenium::WebDriver.for(:remote, :desired_capabilities => desired_caps, :url => server_url) +# Load the desired configuration from appium.txt, create a driver then +# Add the methods to the world +caps = Appium.load_appium_txt file: File.expand_path('./', __FILE__), verbose: true +Appium::Driver.new(caps).start_driver +Appium.promote_appium_methods AppiumWorld + +World do + AppiumWorld.new end -After { @driver.quit } +After { $driver.driver_quit } \ No newline at end of file