Skip to content

eperkins/ruby_console

 
 

Repository files navigation

Ruby Console for Appium

Dependency Status

Update system

Paste the following into Terminal. If you're not using RVM, you may have to prefix gem commands with sudo.

gem update --system ;\
gem update bundler

Install / Upgrade Ruby Console

gem uninstall -aIx app_lib ;\
gem uninstall -aIx ruby_console ;\
gem install --no-rdoc --no-ri ruby_console

Update Appium

To update appium from source (assuming node.js requires sudo):

Clone appium if you haven't already.

git clone [email protected]:appium/appium.git

then in the appium folder:

git pull ;\
sudo ./reset.sh ; ./reset.sh

You should uninstall the ap gem because it breaks awesome_print.

gem uninstall -aIx ap

Run

The arc command starts Appium Ruby Console.

Ruby 1.9.3 and Appium from GitHub are required. Run Appium from source.

node server.js -V --fast-reset

For OS X, export the path to your .app bundle MyApp.app or zipped app bundle MyApp.app.zip

export APP_PATH="../MyApp.app"

For Android:

export APP_PATH="/path/to/my.apk" ;\
export APP_PACKAGE="com.my.Pkg" ;\
export APP_ACTIVITY="MyActivity"

You may want to define the environment variables in ~/.bash_profile so you don't have to export them again.

Reset Appium

Reset Appium after pulling the latest changes.

$ ./reset.sh

Documentation

  • find_elements returns an empty array [] when no elements are found.

Example use of Appium's mobile gesture.

@driver.execute_script 'mobile: tap', :x => 0, :y => 500

console.rb uses some code from simple_test.rb and is released under the same license as Appium. The Accessibility Inspector is helpful for discovering button names and textfield values.

--

Tag Name | UIA --:|:-- button | UIAButton textfield | UIATextField secure | UIASecureTextField text | UIAStaticText

--

  • source Prints a JSON view of the current page.

--

alert

  1. (void) alert_accept Accept the alert.
  2. (String) alert_accept_text Get the text of the alert's accept button.
  3. (void) alert_click(value) iOS only Tap the alert button identified by value.
  4. (void) alert_dismiss Dismiss the alert.
  5. (String) alert_dismiss_text Get the text of the alert's dismiss button.
  6. (String) alert_text Get the alert message text.

button

  1. (Button) button(text, number = 0) Find a button by text and optionally number.
  2. (Button) button_include(text) Get the first button that includes text.
  3. (Array<String>, Array<Buttons>) buttons(text = nil) Get an array of button texts or button elements if text is provided.
  4. (Array<Button>) buttons_include(text) Get all buttons that include text.
  5. (Button) first_button Get the first button element.
  6. (Button) last_button Get the last button element.

textfield

  1. (Array<Textfield>) e_textfields Get an array of textfield elements.
  2. (Textfield) first_textfield Get the first textfield element.
  3. (Textfield) last_textfield Get the last textfield element.
  4. (Textfield) textfield(text) Get the first textfield that matches text.
  5. (Textfield) textfield_include(text) Get the first textfield that includes text.
  6. (Array<String>) textfields Get an array of textfield texts.

text

  1. (Array<Text>) e_texts Get an array of text elements.
  2. (Text) first_text Get the first text element.
  3. (Text) last_text Get the last text element.
  4. (Text) text(text) Get the first element that matches text.
  5. (Text) text_include(text) Get the first textfield that includes text.
  6. (Array<String>) texts Get an array of text texts.

window

  1. (Object) window_size Get the window's size.

--

e.name # button, text
e.value # secure, textfield
e.type
e.tag_name # calls .type (patch.rb)
e.text
e.size
e.location
e.rel_location
e.click
e.send_keys 'keys to send'
e.set_value 'value to set' # ruby_console specific

# alert example without helper methods
alert = $driver.switch_to.alert
alert.text
alert.accept
alert.dismiss

# Secure textfield example.
#
# Find using default value
s = secure 'Password'
# Enter password
s.send_keys 'hello'
# Check value
s.value == password('hello'.length)

routing.js lists not yet implemented end points.

--

Driver

driver will restart the driver.

x will quit the driver and exit Pry.

execute_script calls $driver.execute_script

find_element calls $driver.find_element

find_elements calls $driver.find_elements

mobile :swipe, endX: 100, endY: 100, duration: 0.01 calls $driver.execute_script 'mobile: swipe', endX: 100, endY: 100, duration: 0.01

.click to tap an element. .send_keys to type on an element.

Raw UIAutomation

execute_script "au.lookup('button')[0].tap()" is the same as execute_script 'UIATarget.localTarget().frontMostApp().buttons()[0].tap()'

See app.js for more au methods. Note that raw UIAutomation commands are not offically supported.

Advanced au.

In this example we lookup two tags, combine the results, wrap with $, and then return the elements.

s = %(
var t = au.lookup('textfield');
var s = au.lookup('secure');
var r = $(t.concat(s));
au._returnElems(r);
)

execute_script s

XPath

See #194 for details.

find_element  :xpath, 'button'
find_elements :xpath, 'button'

find_element  :xpath, 'button[@name="Sign In"]'
find_elements :xpath, 'button[@name="Sign In"]'

find_element  :xpath, 'button[contains(@name, "Sign In")]'
find_elements :xpath, 'button[contains(@name, "Sign")]'

find_element :xpath, 'textfield[@value="Email"]'
find_element :xpath, 'textfield[contains(@value, "Email")]'

find_element  :xpath, 'text[contains(@name, "Reset")]'
find_elements :xpath, 'text[contains(@name, "agree")]'

Cucumber Sauce Integration

Reset after each test and when done report the result to Sauce after quiting the driver.

require 'rest_client' # https://github.com/archiloque/rest-client
require 'json' # for .to_json

$passed = true

After do |scenario|
  $driver.execute_script 'mobile: reset'

  if $passed
    $passed = false if scenario.failed?
  end
end

at_exit do
  ID = $driver.send(:bridge).session_id
  $driver.quit
  
  if !SAUCE_USERNAME.nil? && !SAUCE_ACCESS_KEY.nil?
    URL = "https://#{SAUCE_USERNAME}:#{SAUCE_ACCESS_KEY}@saucelabs.com/rest/v1/#{SAUCE_USERNAME}/jobs/#{ID}"

    # Keep trying until passed is set correctly. Give up after 30 seconds.
    wait do
      response = RestClient.put URL, { 'passed' => $passed }.to_json, :content_type => :json, :accept => :json
      response = JSON.parse(response)

      # Check that the server responded with the right value.
      response['passed'] == $passed
    end
  end
end

About

Ruby console for use with Appium

Resources

Stars

Watchers

Forks

Packages

No packages published