当前位置: 移动技术网 > 移动技术>App>移动应用 > Appium翻译篇【 appium/docs/en/about-appium/getting-started.md 】

Appium翻译篇【 appium/docs/en/about-appium/getting-started.md 】

2020年08月11日  | 移动技术网移动技术  | 我要评论
Getting StartedThis doc will get you up and running with a simple Appium test and introduce you to some basic Appium ideas. For a more comprehensive introduction to Appium concepts, please check out the conceptual introduction.启动并运行一个简单的Appium测试,并介绍一些基

Getting Started

This doc will get you up and running with a simple Appium test and introduce you to some basic Appium ideas. For a more comprehensive introduction to Appium concepts, please check out the conceptual introduction.

启动并运行一个简单的Appium测试,并介绍一些基本的Appium概念。有关Appium概念的更全面介绍,请查看conceptual introduction。

Installing Appium

Appium can be installed in one of two ways: via NPM or by downloading Appium Desktop, which is a graphical, desktop-based way to launch the Appium server.

Appium的两种安装方式:① 通过NPM;②下载Appium Desktop(图形化的、基于桌面的方式来启动Appium服务器)

Installation via NPM

If you want to run Appium via an npm install, hack with Appium, or contribute to Appium, you will need Node.js and NPM (use nvm, n, or brew install node to install Node.js. Make sure you have not installed Node or Appium with sudo, otherwise you’ll run into problems). We recommend the latest stable version, though Appium supports Node 10+.
The actual installation is as simple as:

如果想通过npm安装运行Appium, 通过Appium破解或向Appium贡献代码,需要node .js和npm(使用nvm、 n或brew安装节点来安装node .js。请确保您没有使用sudo安装Node或Appium,否则您将遇到问题)。尽管Appium支持Node 10+,但推荐使用最新的稳定版本。
安装命令如下:

npm install -g appium 

Installation via Desktop App Download

Simply download the latest version of Appium Desktop from the releases page.

只需从发行版页面下载Appium Desktop的最新版本。

Driver-Specific Setup

You probably want to use Appium to automate something specific, like an iOS or Android application. Support for the automation of a particular platform is provided by an Appium “driver”. There are a number of such drivers that give you access to different kinds of automation technologies, and each come with their own particular setup requirements. Most of these requirements are the same requirements as for app development on a specific platform. For example, to automate Android applications using one of our Android drivers, you’ll need the Android SDK configured on your system.
At some point, make sure you review the driver documentation for the platform you want to automate, so your system is set up correctly:

  • The XCUITest Driver (for iOS and tvOS apps)
  • The Espresso Driver (for Android apps)
  • The UiAutomator2 Driver (for Android apps)
  • The Windows Driver (for Windows Desktop apps)
  • The Mac Driver (for Mac Desktop apps)

你可能想使用Appium来自动化一些特定的东西,比如iOS或Android应用程序。Appium“驱动程序”提供对特定平台自动化的支持。有许多这样的驱动程序可以让您访问不同种类的自动化技术,并且每个驱动程序都有它们自己特定的设置要求。这些要求中的大多数与在特定平台上开发应用程序的要求相同。例如,要使用我们的Android驱动程序来自动化Android应用程序,你需要在你的系统上配置Android SDK。
在某些情况下,确保你检查了你想要自动化的平台的驱动文档,以便你的系统被正确设置:

  • iOS 和 tvOS apps:XCUITest Driver
  • Android apps:Espresso Driver
  • Android apps:UiAutomator2 Driver
  • Windows Desktop apps:Windows Driver
  • Mac Desktop apps:Mac Driver

Verifying the Installation

To verify that all of Appium’s dependencies are met you can use appium-doctor. Install it with npm install -g appium-doctor, then run the appium-doctor command, supplying the --ios or --android flags to verify that all of the dependencies are set up correctly.

要验证满足了Appium的所有依赖项,可以使用Appium -doctor。使用npm Install -g appium-doctor安装它,然后运行appium-doctor命令,提供——ios或——android标志来验证所有依赖项都正确设置了。

Appium Clients

When all is said and done, Appium is just an HTTP server. It sits and waits for connections from a client, which then instructs Appium what kind of session to start and what kind of automation behaviors to enact once a session is started. This means that you never use Appium just by itself. You always have to use it with a client library of some kind (or, if you’re adventurous, cURL!).
Luckily, Appium speaks the same protocol as Selenium, called the WebDriver Protocol. You can do a lot of things with Appium just by using one of the standard Selenium clients. You may even have one of these on your system already. It’s enough to get started, especially if you’re using Appium for the purpose of testing web browsers on mobile platforms.
Appium can do things that Selenium can’t, though, just like mobile devices can do things that web browsers can’t. For that reason, we have a set of Appium clients in a variety of programming languages, that extend the regular old Selenium clients with additional functionality. You can see the list of clients and links to download instructions at the Appium clients list.
Before moving forward, make sure you have a client downloaded in your favorite language and ready to go.

总之,Appium只是一个HTTP服务器。Appium等待来自客户机的连接,然后客户机指示Appium启动什么样的会话,以及在会话启动后执行什么样的自动化行为。这意味着不能单独使用Appium,必须将它与某种客户机库一起使用(或者,如果您喜欢冒险,可以使用cURL!)
幸运的是,Appium使用与Selenium相同的协议,称为WebDriver协议。通过使用一个标准的Selenium客户机,您可以使用Appium做很多事情。您的系统中甚至可能已经有了其中一个。对于初学者来说,这已经足够了,特别是如果您正在使用Appium来测试移动平台上的web浏览器。
Appium可以做Selenium不能做的事情,就像移动设备可以做web浏览器不能做的事情一样。由于这个原因,我们有一组使用各种编程语言的Appium客户机,它们用额外的功能扩展了常规的老Selenium客户机。您可以在Appium clients列表中看到客户机列表和下载说明的链接。
在继续之前,确保您已经下载了您最喜欢的语言的客户机并准备就绪。

Starting Appium

Now we can kick up an Appium server, either by running it from the command line like so (assuming the NPM install was successful):
Or by clicking the huge Start Server button inside of Appium Desktop.

通过从命令行运行命令(假设NPM安装成功),appium,或者通过点击Appium桌面服务器来启动。

Appium will now show you a little welcome message showing the version of Appium you’re running and what port it’s listening on (the default is 4723). This port information is vital since you will have to direct your test client to make sure to connect to Appium on this port. If you want to change, the port, you can do so by using the -p flag when starting Appium (be sure to check out the full list of server parameters).

Appium现在将向您显示一个欢迎消息,显示您正在运行的Appium版本以及它正在监听的端口(默认为4723)。此端口信息非常重要,因为您必须指示测试客户端确保在此端口上连接到Appium。如果您希望更改端口,可以在启动Appium时使用-p标志(请确保检查服务器参数的完整列表)。

Running Your First Test

In this section we’ll run a basic “Hello World” Android test. We’ve chosen Android because it’s available on all platforms. We’ll be using the UiAutomator2 Driver so ensure you’ve read through that doc and gotten your system set up appropriately. We’ll also be using JavaScript as the language so that we don’t have to deal with additional dependencies.
(Chances are, you’ll eventually want to automate something other than Android using something other than JavaScript. In that case, check out our sample-code, which has code samples for many languages and platforms.)

在本节中,将运行一个基本的“Hello World”Android测试。选择Android是因为它可以在所有平台上使用。我们将使用UiAutomator2驱动程序,所以请确保已经通读了文档并适当地设置了系统。将使用JavaScript作为语言,这样不必处理额外的依赖关系。
(很有可能,你最终会想用JavaScript以外的语言来自动化Android以外的平台。在这种情况下,请查看示例代码,其中包含针对多种语言和平台的代码示例。)

Prerequisites

  • We’ll assume you have an Android 8.0 emulator configured and running (the example will work on lower versions, just fix the version numbers accordingly)
  • We’ll assume you have this test APK downloaded and available on your local filesystem
  • 假设已经配置并运行了一个Android 8.0模拟器(示例将在较低的版本上运行,只需相应地修正版本号)
  • 假设已经下载了这个测试APK,并且可以在本地文件系统上使用

Setting up the Appium Client

For this example, we’ll use Webdriver.io as our Appium client. Create a directory for this example, then run:

使用Webdriver.o作为我们的Appium客户端。为这个例子创建一个目录,然后运行:

npm init -y 

Once the project has been initialized, install webdriverio:

一旦项目已经初始化,安装webdriverio:

npm install webdriverio 

Session Initialization

Now we can create our test file, named index.js, and initialize the client object:

创建名为index.js的测试文件,初始化客户端对象:

// javascript
const wdio = require("webdriverio"); 

The next thing we need to do is to start an Appium session. We do this by defining a set of server options and Desired Capabilities, and calling wdio.remote() with them. Desired Capabilities are just a set of keys and values that get sent to the Appium server during session initialization, that tell Appium what kind of thing we want to automate. The minimum set of required capabilities for any Appium driver should include:

  • platformName: the name of the platform to automate
  • platformVersion: the version of the platform to automate
  • deviceName: the kind of device to automate
  • app: the path to the app you want to automate (but use the browserName capability instead in the case of automating a web browser)
  • automationName: the name of the driver you wish to use
    For more information on Desired Capabilities and for a list of all the Capabilities you can use in Appium, see our Capabilities doc.
    So here is how we begin to construct a session in our test file:

启动一个Appium会话。为此,定义了一组服务器选项和所需的功能,并使用它们调用wdio.remote()。所需的功能只是在会话初始化期间发送到Appium服务器的一组键和值,这些键和值告诉Appium我们想要自动化的事情。任何Appium驱动程序所需的最小功能集应该包括:

  • platformName:要自动化的平台名称
  • platformVersion:要自动化的平台版本
  • deviceName: 要自动化的设备类型
  • app:要自动化的应用程序的路径(但是在自动化web浏览器的情况下使用browserName)
  • automationName:使用的驱动程序的名称
    有关所需功能的更多信息以及Appium中可以使用的所有功能的列表,请参阅Capabilities doc。
    下面是开始在测试文件中构建会话的方法:
// javascript
const opts = {
  path: '/wd/hub',
  port: 4723,
  capabilities: {
    platformName: "Android",
    platformVersion: "8",
    deviceName: "Android Emulator",
    app: "/path/to/the/downloaded/ApiDemos.apk",
    appPackage: "io.appium.android.apis",
    appActivity: ".view.TextFields",
    automationName: "UiAutomator2"
  }
};
async function main () {
  const client = await wdio.remote(opts);
  await client.deleteSession();
}
main(); 

Running Test Commands

You can see that we’ve specified our Appium port and also constructed our Desired Capabilities to match our requirements (but don’t forget to replace the path with the actual download path for your system). We’ve registered this fact with webdriverio and now have a client object which will represent the connection to the Appium server. From here, we can go ahead and start the session, perform some test commands, and end the session. In our case, we will simply type into a text field and check that the correct text was entered:

可以看到,已经指定了Appium端口,还构建了所需的功能,以匹配需求(但不要忘记用系统的实际下载路径替换该路径)。我们已经在webdriverio上注册了这个事实,现在有一个客户端对象,它将代表到Appium服务器的连接。从这里,我们可以继续并开始会话,执行一些测试命令,并结束会话。在本例中,我们将简单地在文本框中输入并检查输入的文本是否正确:

// javascript
const field = await client.$("android.widget.EditText");
await field.setValue("Hello World!");
const value = await field.getText();
assert.equal(value, "Hello World!"); 

What’s going on here is that after creating a session and launching our app, we’re instructing Appium to find an element in the app hierarchy and type into it. The same field is then queried for its text, which is asserted to be what we expect. Putting it all together, the file should look like:

这里的情况是,在创建一个会话并启动app后,我们会指示Appium在app层级中找到一个元素并输入进去。然后查询该字段的文本,该文本被断言为我们所期望的内容。把它们放在一起,文件应该是这样的:

// javascript
const wdio = require("webdriverio");
const assert = require("assert");
const opts = {
  path: '/wd/hub',
  port: 4723,
  capabilities: {
    platformName: "Android",
    platformVersion: "8",
    deviceName: "Android Emulator",
    app: "/path/to/the/downloaded/ApiDemos.apk",
    appPackage: "io.appium.android.apis",
    appActivity: ".view.TextFields",
    automationName: "UiAutomator2"
  }
};
async function main () {
  const client = await wdio.remote(opts);
  const field = await client.$("android.widget.EditText");
  await field.setValue("Hello World!");
  const value = await field.getText();
  assert.equal(value,"Hello World!");
  await client.deleteSession();
}
main(); 

You can try and run this test on your own. Simply save it and execute it using node:

您可以尝试自己运行这个测试。保存并使用node执行它:

node index.js 

If everything is set up correctly, you’ll see Appium begin spitting out lots of logs and eventually the app will pop up on the screen and start behaving as if an invisible user were tapping on it!

如果一切都设置正确,你会看到Appium开始吐出大量的日志,最终应用程序将弹出在屏幕上,并开始表现得好像一个隐形用户正在点击它!

What’s Next

We’ve only scratched the surface of what you can do with Appium. Check> out these resources to help you on your journey:

  • The Appium command reference - learn about what commands are available, how to use them with specific client libraries, etc…
  • The sample-code directory, where lots more code samples are available
  • discuss.appium.io - this is the Appium community forum, which is a great first place to go for help getting started, or if you think you may have run into a bug
  • The Appium issue tracker - let the Appium maintainers know here if you think you’ve found a bug

我们对Appium的作用只了解了皮毛。以下资源会或许有所帮助:

  • Appium命令参考:了解哪些命令是可用的,如何在特定的客户端库中使用它们,等等。
  • 示例代码目录:其中有更多的代码示例
  • discuss.appium.io:这是Appium社区论坛,如果你认为你可能遇到了bug,这是一个很好的开始寻求帮助的地方
  • Appium问题跟踪器:如果你认为你找到了一个bug,请在这里让Appium维护人员知道

本文地址:https://blog.csdn.net/vale_/article/details/107867763

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网