Skip to content

Commit

Permalink
updates for refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
isonic1 committed Feb 22, 2016
1 parent f9b558b commit 8f4dbf7
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 92 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Assumptions:
* You have npm & Appium installed. https://www.npmjs.com/package/appium
* You're running this on a mac. Though, this shouldn't be too different to run on other platforms.
* You have Homebrew installed. http://brew.sh/
* You have ideviceinstaller installed. `brew install ideviceinstaller`
* You have android emulators installed or USB connected devices with USB Debug enabled.

To set up:
Expand Down Expand Up @@ -38,13 +39,20 @@ To set up:

* Start emulators or connect devices!

To run specs single threaded:

`rake android[single]`
`rake ios[single]`

To run specs in parallel:

`rake android[parallel]`
`rake ios[parallel]`

To run specs distributed:

`rake android[dist]`
`rake ios[dist]`

To run specs on SauceLabs:
* Goto saucelabs.com and signup.
Expand All @@ -53,17 +61,27 @@ To run specs on SauceLabs:
`export SAUCE_USERNAME=<user sauce user_id>`

`export SAUCE_ACCESS_KEY=<your sauce access key>`

`rake android[parallel,sauce]` << "Will run tests tagged with :sauce in parallel"
`rake android/ios[single,sauce]` << "Will run tests tagged with :sauce single threaded"

`rake android[dist,sauce]` << "Will run tests tagged with :sauce distributed"
`rake android/ios[dist,sauce]` << "Will run tests tagged with :sauce distributed"

Generate Allure report: (Displays test results, hub log, appium log, screenshots and video)

`allure generate report output/allure/*`

`allure report open`

## iOS debugging:
* There could be times when the tests hang on iOS. This is most likely due to a pairing issue with ideviceinstaller.
`idevicepair -u <udid> unpair`
`idevicepair -u <udid> pair`
* Accept the "Trust this computer" popup on the device.
`idevicepair -u <udid> validate`
* Make sure you get "SUCCESS: Validated pairing with device <udid>"
* You should now be able to install the app manually.
`ideviceinstaller -u <udid> -i ./appium-mobile-grid/ios/TestApp/build/Release-iphoneos/TestApp.app`

Disclaimer:
* This example was built quickly, so the code is not in the optimal state of dryness.
* No page objects were used. The tests are soley for example purposes.
18 changes: 11 additions & 7 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require_relative 'server_launcher'

desc 'Running Android on the grid!'
desc 'Running Android tests!'
task :android, :type, :tag do |t, args|
task_setup android_app, t.to_s, args
end

desc 'Running iOS on the grid!'
desc 'Running iOS tests!'
task :ios, :type, :tag do |t, args|
task_setup ios_app, t.to_s, args
end
Expand All @@ -32,10 +32,14 @@ def task_setup app, platform, args
threads = "-n #{ENV["THREADS"]}"
ENV["SERVER_URL"] = "http://localhost:4444/wd/hub" #Change this to your hub url if different.
end
elsif tag == "sauce"
ENV["ENV"] = "sauce"
ENV["SERVER_URL"] = "http://#{ENV['SAUCE_USERNAME']}:#{ENV['SAUCE_ACCESS_KEY']}@ondemand.saucelabs.com:80/wd/hub"
upload_app_to_sauce app
elsif tag.include? "sauce"
if platform == "ios"
puts "Sorry, this example is not setup to run iOS on sauce labs. Android only...\n"
return
else
ENV["ENV"], ENV["SERVER_URL"] = "sauce","http://#{ENV['SAUCE_USERNAME']}:#{ENV['SAUCE_ACCESS_KEY']}@ondemand.saucelabs.com:80/wd/hub"
upload_app_to_sauce app
end
end

Dir.chdir platform
Expand All @@ -61,7 +65,7 @@ end
def upload_app_to_sauce app
require 'sauce_whisk'
storage = SauceWhisk::Storage.new
puts "uploading #{app} to saucelabs..."
puts "Uploading #{app} to saucelabs...\n"
storage.upload app
ENV["APP_PATH"] = "sauce-storage:#{File.basename(app)}"
end
Expand Down
2 changes: 0 additions & 2 deletions android/.rspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@
--require=./spec/spec_helper.rb
--order random
--format documentation
--format html
--out output/rspec.html
22 changes: 10 additions & 12 deletions android/adb_helpers.rb → android/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Adb
module Helpers
class << self

def kill_adb_pid pid
Expand All @@ -10,29 +10,27 @@ def recording_setup udid
spawn "adb -s #{udid} shell rm /sdcard/recordings/* >> /dev/null 2>&1"
end

def start_video_record udid, name
return if ENV["ENV"] == "sauce"
def start_video_record udid
if ENV["UDID"].include? "emulator"
puts "\nNot video recording. Cannot video record on #{udid} emulator!\n\n"
return
else
recording_setup udid
puts "\nRecording! You have a maximum of 180 seconds record time...\n"
pid = spawn "adb -s #{udid} shell screenrecord --size 720x1280 /sdcard/recordings/video-#{name}.mp4", :out=> "/dev/null"
pid = spawn "adb -s #{udid} shell screenrecord --size 720x1280 /sdcard/recordings/video-#{udid}.mp4", :out=> "/dev/null"
ENV["VIDEO_PID"] = pid.to_s
end
end

def stop_video_record udid, name
return if ENV["UDID"].include? "emulator" || ENV["ENV"] == "sauce"
def stop_video_record udid
return if ENV["UDID"].include? "emulator"
kill_adb_pid ENV["VIDEO_PID"]
sleep 5 #delay for video to complete processing on device...
spawn "adb -s #{udid} pull /sdcard/recordings/video-#{name}.mp4 #{ENV["BASE_DIR"]}/output"
spawn "adb -s #{udid} pull /sdcard/recordings/video-#{udid}.mp4 #{ENV["BASE_DIR"]}/output"
end

def start_logcat udid, name
return if ENV["ENV"] == "sauce"
pid = spawn("adb -s #{udid} logcat -v long", :out=>"#{ENV["BASE_DIR"]}/output/logcat-#{name}.log")
def start_logcat udid
pid = spawn("adb -s #{udid} logcat -v long", :out=>"#{ENV["BASE_DIR"]}/output/logcat-#{udid}.log")
ENV["LOGCAT_PID"] = pid.to_s
end

Expand All @@ -43,7 +41,7 @@ def stop_logcat
end

module Kernel
def adb
Adb
def helper
Helpers
end
end
7 changes: 4 additions & 3 deletions android/spec/delete_note_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
describe "Modify Note Scenarios #{ENV["UDID"]}" do

before :each do
wait_true { find_element(:id, 'android:id/action_bar_title').text.eql? "Notes" }
wait_true { find_element(:id, 'android:id/action_bar_title').text == "Notes" }
find_element(:id, 'com.example.android.notepad:id/menu_add').click
wait_true { find_element(:id, "android:id/action_bar_title").text.eql? "New note" }
wait_true { find_element(:id, "android:id/action_bar_title").text == "New note" }
@note = Lorem.sentence
find_element(:id, 'com.example.android.notepad:id/note').send_keys @note
sleep 5
find_element(:id, 'com.example.android.notepad:id/menu_save').click
wait_true { find_element(:id, "android:id/action_bar_title").text.eql? "Notes" }
wait_true { find_element(:id, "android:id/action_bar_title").text == "Notes" }
end

it 'Delete A Note', sauce: false do
Expand Down
20 changes: 11 additions & 9 deletions android/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
require_relative "#{ENV["BASE_DIR"]}/common/methods"

ENV["UDID"] = get_device_data["udid"]
set_udid_environment_variable

RSpec.configure do |config|
config.color = true
config.tty = true

config.before :all do
initialize_appium_and_methods "../android/appium.txt"
require_relative '../adb_helpers'
Appium.promote_singleton_appium_methods Adb
initialize_appium_and_methods "android"
end

config.before :each do
adb.start_logcat ENV["UDID"], "#{ENV["UDID"]}"
adb.start_video_record ENV["UDID"], "#{ENV["UDID"]}"
unless ENV["ENV"] == "sauce"
helper.start_logcat ENV["UDID"]
helper.start_video_record ENV["UDID"]
end
end

config.after :each do |e|
adb.stop_logcat
adb.stop_video_record ENV["UDID"], "#{ENV["UDID"]}"
unless ENV["ENV"] == "sauce"
helper.stop_logcat
helper.stop_video_record ENV["UDID"]
end
update_sauce_status @driver.session_id, e.exception.nil?
unless e.exception.nil?
attach_report_files e, "android"
attach_report_files e
end
end

Expand Down
35 changes: 23 additions & 12 deletions common/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
include Faker

def update_sauce_status job_id, status
return unless ENV["ENV"].eql? "sauce"
return unless ENV["ENV"] == "sauce"
job = SauceWhisk::Jobs
job.change_status job_id, status
end
Expand All @@ -14,26 +14,37 @@ def thread
end

def get_device_data
JSON.parse(ENV["DEVICES"]).find { |t| t["thread"].eql? thread } unless ENV["ENV"] == "sauce"
unless ENV["ENV"] == "sauce"
JSON.parse(ENV["DEVICES"]).find { |t| t["thread"].eql? thread }
else
{}
end
end

def set_udid_environment_variable
return if ENV["ENV"] == "sauce"
ENV["UDID"] = get_device_data["udid"]
end

def initialize_appium_and_methods appium_file
device_data = get_device_data
caps = Appium.load_appium_txt file: File.join(File.dirname(__FILE__), appium_file)
caps[:caps][:udid] = device_data["udid"]
caps[:caps][:platformVersion] = device_data["os"]
caps[:caps][:deviceName] = device_data["name"]
def initialize_appium_and_methods platform
device = get_device_data
caps = Appium.load_appium_txt file: File.join(File.dirname(__FILE__), "../#{platform}/appium.txt")
caps[:caps][:udid] = device.fetch("udid", nil)
caps[:caps][:platformVersion] = device.fetch("os", nil)
caps[:caps][:deviceName] = device.fetch("name", platform)
caps[:caps][:app] = ENV["APP_PATH"]
caps[:appium_lib][:server_url] = ENV["SERVER_URL"]
caps[:caps][:name] = self.class.metadata[:full_description] #for sauce labs
caps[:caps][:name] = self.class.metadata[:full_description] #for sauce labs test description
@driver = Appium::Driver.new(caps).start_driver

Appium.promote_appium_methods Object
Appium.promote_appium_methods RSpec::Core::ExampleGroup
require_relative "../#{platform}/helpers"
Appium.promote_singleton_appium_methods Helpers
end

def attach_report_files example, platform
example.attach_file("Hub Log: #{ENV["UDID"]}", File.new("#{ENV["BASE_DIR"]}/output/#{platform}-hub.log")) unless ENV["THREADS"].nil?
def attach_report_files example
return if ENV["ENV"] == "sauce"
example.attach_file("Hub Log: #{ENV["UDID"]}", File.new("#{ENV["BASE_DIR"]}/output/hub.log")) unless ENV["THREADS"].nil?
@driver.screenshot "#{ENV["BASE_DIR"]}/output/screenshot-#{ENV["UDID"]}.png"
files = (`ls #{ENV["BASE_DIR"]}/output/*#{ENV["UDID"]}*`).split("\n").map { |file| { name: file.match(/output\/(.*)-/)[1], file: file } }
files.each { |file| example.attach_file(file[:name], File.new(file[:file])) } unless files.empty?
Expand Down
2 changes: 0 additions & 2 deletions ios/.rspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@
--require=./spec/spec_helper.rb
--order random
--format documentation
--format html
--out output/rspec.html
11 changes: 11 additions & 0 deletions ios/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Helpers
class << self
#puts iOS specific helper methods here...
end
end

module Kernel
def helper
Helpers
end
end
2 changes: 1 addition & 1 deletion ios/spec/alert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe "Validate Alert Popups #{ENV["UDID"]}" do

it 'Alert popup text', :sauce do
it 'Alert popup text' do
find_element(:id, "show alert").click
expect(texts.first.text).to eq "Cool title"
expect(texts.last.text).to eq "FAIL ON PURPOSE!!!"
Expand Down
2 changes: 1 addition & 1 deletion ios/spec/features_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe "Validate Test App Features #{ENV["UDID"]}" do

it 'Compute Sum', :sauce do
it 'Compute Sum' do
a = rand(99); b = rand(99)
find_element(:id, "IntegerA").type a
find_element(:id, "IntegerB").type b
Expand Down
7 changes: 3 additions & 4 deletions ios/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
require_relative "#{ENV["BASE_DIR"]}/common/methods"

ENV["UDID"] = get_device_data["udid"]
set_udid_environment_variable

RSpec.configure do |config|
config.color = true
config.tty = true

config.before :all do
initialize_appium_and_methods "../ios/appium.txt"
initialize_appium_and_methods "ios"
end

config.after :each do |e|
update_sauce_status @driver.session_id, e.exception.nil?
unless e.exception.nil?
attach_report_files "ios"
attach_report_files e
end
end

Expand Down
Loading

0 comments on commit 8f4dbf7

Please sign in to comment.