编写测试 (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.
第一步,让我们初始化一个 Gemfile 来管理依赖项
然后,您可以添加 Appium Ruby 客户端依赖项,如下所示
下面的测试代码示例使用 test-unit
模块,因此请运行
完成这些步骤后,您的 Gemfile
文件应包含
appium_lib_core
是 Appium 客户端的主要部分。appium_lib
有各种辅助方法,但驱动程序实例通常被设计为全局变量使用。这可能会导致处理实例的问题。appium_lib_core
没有这样的全局变量。
此示例使用 appium_lib_core
和 test-unit
gem 模块。appium_lib
中的测试代码应该类似。
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
注意
本指南的范围不包括对 Ruby 客户端库或这里发生的所有事情进行完整的介绍,因此我们现在将暂时不详细解释代码本身。
- 您可能需要特别了解 Appium 功能。
- 功能测试代码 在 appium_lib_core GitHub 存储库中应该有助于找到更多工作示例。
- 文档 appium_lib_core 和 appium_lib 也有助于找到可用的方法。
注意
示例代码可从 GitHub Appium 存储库 获取。
基本上,此代码执行以下操作
- 定义一组“功能”(参数)发送到 Appium 服务器,以便 Appium 知道您要自动执行什么类型的东西。
- 在内置的 Android 设置应用程序上启动 Appium 会话。
- 找到“电池”列表项并单击它。
- 暂停片刻,纯粹是为了视觉效果。
- 结束 Appium 会话。
就是这样!让我们试一试。在运行测试之前,请确保您在另一个终端会话中运行了 Appium 服务器,否则您将收到有关无法连接到服务器的错误。然后,您可以执行脚本
# Please run "bundle install" first if your environment has not run the installation command yet.
bundle exec ruby test.rb
如果一切顺利,您将看到设置应用程序打开并导航到“电池”视图,然后应用程序再次关闭。
恭喜,您已经开始了 Appium 之旅!继续阅读以了解一些 下一步 以便探索。