Write a Test (Ruby)
The AppiumLib and the AppiumLibCore (recommended) are official Appium client libraries in Ruby, which are available via gem under the appium_lib and the appium_lib_core package names. The appium_lib_core inherits from the Selenium Ruby Binding, and the appium_lib inherits from the appium_lib_core, so installing these libraries include the selenium binding. We recommend appium_lib_core
if you need a less complex client-side solution. The appium_lib
has some useful methods the core does not have, but for the cost of greater complexity and historical methods which may not work in the latest environment.
As the first step, let's initialize a Gemfile to manage the dependency:
Then, you could add Appium Ruby Client dependency as below:
Test code example below uses test-unit
module, thus please run:
Once these steps has done, your Gemfile
file should include:
The appium_lib_core
is the main part as an Appium client.
appium_lib
has various helper methods, but the driver instance was ordinary designed to be used as a global variable. It could causes an issue to handle the instance.
appium_lib_core
does not have such a global variable.
This example is by the appium_lib_core
with test-unit
gem module.
Tes code in appium_lib
should be similar.
require 'appium_lib_core'
require 'test/unit'
CAPABILITIES = {
platformName: 'Android',
automationName: 'uiautomator2',
deviceName: 'Android',
appPackage: 'com.android.settings',
appActivity: '.Settings',
language: 'en',
locale: 'US'
}
SERVER_URL = 'http://localhost:4723'
class AppiumTest < Test::Unit::TestCase
def setup
@core = ::Appium::Core.for capabilities: CAPABILITIES
@driver = @core.start_driver server_url: SERVER_URL
end
def teardown
@driver&.quit
end
def test_version
@driver.wait { |d| d.find_element :xpath, '//*[@text="Battery"]' }.click
end
end
Note
It's not within the scope of this guide to give a complete run-down on the Ruby client library or everything that's happening here, so we'll leave the code itself unexplained in detail for now.
- You may want to read up particularly on Appium Capabilities.
- functional test code in the appium_lib_core GitHub repository should help to find more working example.
- Documentation appium_lib_core and appium_lib also helps to find available methods.
Note
The sample code is available from GitHub Appium repository.
Basically, this code is doing the following:
- Defining a set of "Capabilities" (parameters) to send to the Appium server so Appium knows what kind of thing you want to automate.
- Starting an Appium session on the built-in Android settings app.
- Finding the "Battery" list item and clicking it.
- Pausing for a moment purely for visual effect.
- Ending the Appium session.
That's it! Let's give it a try. Before you run the test, make sure that you have an Appium server running in another terminal session, otherwise you'll get an error about not being able to connect to one. Then, you can execute the script:
# Please run "bundle install" first if your environment has not run the installation command yet.
bundle exec ruby test.rb
If all goes well, you'll see the Settings app open up and navigate to the "Battery" view before the app closes again.
Congratulations, you've started your Appium journey! Read on for some next steps to explore.