Write Tests - continued

Step Definitions

I spread out the step definitions between two ruby files, one for non-app and other for app specific tests. You can as well have them in one ruby file.

steps.rb

Given /^I click about phone$/ do
  scroll_to('About phone').click
end

Given /^the Android version is a number$/ do
  android_version = 'Android version'
  scroll_to android_version

  view    = 'android.widget.TextView'
  version = xpath(%Q(//#{view}[preceding-sibling::#{view}[@text="#{android_version}"]])).text
  if !version.match(/\w/).nil? || !version.match(/\d/).nil?
    puts "Version: #{version} pass"
  else
    puts "Version: #{version} is NOT a word or number"
  # valid   = !version.match(/\d/).nil?
  end
  # expect(valid).to eq(true)
end

step_defs.rb

Given(/^I open login screen on app$/) do
  add_contact = id('com.example.android.contactmanager:id/addContactButton')
  exists(post_check=30) { add_contact.text == 'Add Contact' } ? puts('Add Contact exists') : puts('App failed to open')
end

And(/^I click add contact$/) do
  id('com.example.android.contactmanager:id/addContactButton').click
end

Then(/^I verify contact screen is displayed$/) do
  expect(id('android:id/text1').text).to eql("[email protected]")
end
Then(/^I successfully add a contact$/) do
  id('com.example.android.contactmanager:id/contactNameEditText').type "blah"
  id('com.example.android.contactmanager:id/contactPhoneEditText').type "123-456-7890"
  id('com.example.android.contactmanager:id/contactPhoneTypeSpinner').click
  tags('android.widget.CheckedTextView')[2].click
  id('com.example.android.contactmanager:id/contactEmailEditText').type "[email protected]"
  id('com.example.android.contactmanager:id/contactSaveButton').click
end

And(/^verify that it was added$/) do
  expect(id('com.example.android.contactmanager:id/contactEntryText').text).to eql('blah')
end

Then(/^take screenshot of contact form$/) do
  @driver.screenshot("contact_screen.png")
end

Explanation

I will try to explain key concepts here.

  • The locators are retrieved either through uiautomator or arc or by printing source. See the section Introspecting app to learn to retrieve locators
  • In the above step definitions, you can see we have identified the elements, then retrieved text and asserted against expected text
  • We also performed some actions like click and set text by filling contact form and verified that the contact got saved
  • It is also possible to take screenshot at any point by calling the method on the driver object

I need more explanation

Please direct your specific questions to [email protected]

results matching ""

    No results matching ""