If you're a fan of RSpec for writing concise, readable tests in Ruby, you might be interested to know that RSpec tests can now easily automate iOS and Android mobile apps using Appium, an open source mobile test automation tool. Appium is compatible with Selenium WebDriver client libraries, so an Appium test is similar to a Selenium test. In this post we'll walk through the steps involved in testing an iOS sample app using Appium's RSpec example tests. To start, fork and clone Appium from https://github.com/appium/appium, and follow the installation instructions to set up Appium in your environment. Install Appium's dependencies and build the sample apps by running the following from the Appium working directory: $ ./reset.sh --ios
Once the sample projects have have been built, you can then start Appium by running the following: $ grunt appium
Once Appium is running, get a new terminal and go to the sample-code/examples/rspec
directory. First, install the test dependencies with bundler: $ bundle install
Then, run the example RSpec tests: $ rspec simple_test.rb
Now that you're set up and able to run example tests, it's time to learn how they work. Let's look at a simple example of a test that clicks a button and interacts with an alert box in an iOS app. First, we open a "describe" block, and set up a "before" block to initialize the Selenium client we'll use to talk to Appium:
describe "sample app" do
before :all do
@driver = Selenium::WebDriver.for(
:remote,
:desired_capabilities => {'browserName' => 'iOS',
'platform' => 'Mac',
'version' => '6.0',
'app' => '/path/to/TestApp.app'}
:url => "http://127.0.0.1:4723/wd/hub")
end
The "desired_capabilities" parameter here specifies the platform (iOS 6.0) and the app we want to test. Next, we'll add an "after" block to quit the Appium session at the end of each test:
after :all do
@driver.quit
end
And finally, we'll open an "it" block and write the core of our test. This test finds all the 'button' elements on the page, selects the second one (index 1), and clicks it. That action will bring up an alert in the app, which we switch to so that we can examine its text field:
it "should handle alerts" do
els = @driver.find_elements(:tag_name, 'button')
els[1].click
a = @driver.switch_to.alert
a.text.should eq("Cool title")
a.accept
end
end
And those are the basics! There are more examples to look at in Appium's simple_test.rb. Please let us know if you have any questions or comments about running Appium tests with RSpec.