Getting started with Autopilot - 开始使用Autopilot

温馨提示:本人英文能力有限,翻译不当之处还望大家跟帖指正,共同学习、进步! … with-autopilot.html
Getting started with Autopilot

If you caught the last post, you’ll have some background on autopilot and what it can do. Start there if you haven’t already read the post.

So, now that we’ve seen what autopilot can do, let’s dig in to making this work for our testing efforts. A fair warning, there is some python code ahead, but I would encourage even the non-programmers among you to have a glance at what is below. It’s not exotic programming (after all, I did it!). Before we start, let’s make sure you have autopilot itself installed. Note, you’ll need to get the version from this ppa in order for things to work properly:
sudo add-apt-repository ppa:autopilot/ppa
sudo apt-get update && sudo apt-get install python-autopilot

Ok, so first things first. Let’s create a basic shell that we can use for any testcase that we want to write. To make things a bit easier, there’s a lovely bazaar branch you can pull from that has everything you need to follow along.
bzr branch lp:~nskaggs/+junk/autopilot-walkthrough
cd autopilot-walkthrough

You’ll find two folders. Let’s start with the helloworld folder. We’re going to verify autopilot can see the testcases, and then run and look at the ‘helloworld’ tests first. (Note, in order for autopilot to see the testcases, you need to be in the root directory, not inside the helloworld directory)

$ autopilot list helloworld
Loading tests from: /home/nskaggs/projects/


3 total tests.

Go ahead and execute the first helloworld test.

autopilot run helloworld.test_hello.HelloWorld.test_type_hello_world

A gedit window will spawn, and type hello world to you :wink: Go ahead and close the window afterwards. So, let’s take a look at this basic testcase and talk about how it works.
一个文本编辑窗口将会产生,并打印“hello world”给你;-)然后关闭窗口。所以,让我们看看这个基础的测试用例并谈论下它是如何工作的。

[i]from autopilot.testcase import AutopilotTestCase

class HelloWorld(AutopilotTestCase):

def setUp(self):
super(HelloWorld, self).setUp() = self.start_app(“Text Editor”)

def test_type_hello_world(self):
self.keyboard.type(“Hello World”)[/i]

If you’ve used other testing frameworks that follow in the line of xUnit, you will notice the similarities. We implement an AutopilotTestCase object (class HelloWorld(AutopilotTestCase)), and define a new method for each test (ie, test_type_hello_world). You will also notice the setUp method. This is called before each test is run by the testrunner. In this case, we’re launching the “Text Editor” application before we run each test (self.start_app(“Text Editor”)). Finally our test (test_type_hello_world) is simply sending keystrokes to type out “Hello World”.
如果你使用其它测试框架,遵循xUnit路线,你将注意到它们的相似之处。我们实现一个Autopilot测试用例对象(class HelloWorld(AutopilotTestCase)),并为每个测试定义一个新方法(例如:test_type_hello_world)。你还将注意到设置方法。这就是所谓的在每个测试之前由testrunner执行。在这种情况下,我们运行每个测试在开始的“Text Editor”应用前(self.start_app(“Text Editor”))。最后,我们的测试只是简单地发送按键打印出“Hello World”。

From this basic shell we can add more testcases to the helloworld testsuite easily by adding a new method. Let’s add some simple ones now to show off some other capabilities of autopilot to control the mouse and keyboard. If you branched the bzr branch, there is a few more tests in the file. These demonstrate some of the utility methods AutopilotTestCase makes available to us. Try running them now. The comments inside the file also explain briefly what each method does.

autopilot run helloworld.test_example.ExampleFunctions.test_keyboard
autopilot run helloworld.test_example.ExampleFunctions.test_mouse

Now there is more that autopilot can do, but armed with this basic knowledge we can put the final piece of the puzzle together. Let’s create some assertions, or things that must be true in order for the test to pass. Here’s a testcase showing some basic assertions.

autopilot run helloworld.test_example.ExampleFunctions.test_assert

Finally, there’s some standards that are important to know when using autopilot. You’ll notice a few things about each testsuite.
· We have a folder named testsuite.
· Inside the folder, we have a file named
· Inside the file, we have TestSuite class, with test_testcase_name
· Finally, in order for autopilot to see our testsuite we need to let python know there is a submodule in the directory. Ignoring the geekspeak, we need an file (this can be blank if not otherwise needed)
· 我们有一个名为testsuite的文件夹
· 在这个文件夹里,我们有一个名为test_testsuite.py的文件
· 在这个文件里,我们有TestSuite类,和test_testcase_name一起
· 最后,为了Autopilot读取测试用例集,我们需要让python知道目录中有一个子模块。忽略奇客语言,我们需要一个__init__.py文件(如果没有其他需要,这个文件的内容可以为空)。

Given the knowledge we’ve just acquired, we can tackle our first testcase conversion! For those of you who like to work ahead, you can already see the conversion inside the “firefox” folder. But the details, my dear Watson, will be revealed in due time. Until the next post, cheerio!


"autopilot run helloworld.test_hello.HelloWorld.test_type_hello_world"命令执行的自动化测试过程展示如下:

我们可以看出,在执行此命令时,系统自动弹出了一个gedit文本文档,并在打印了"Hello World"后自行关闭。
当执行过程顺利完成后,终端会返回执行此测试所用时间及测试结果(OK or FAILED)。