自动遍历自动化测试_自动化测试打动您的老板

自动遍历自动化测试

动机 (Motivation)

If I had it my way, I would automate myself out of a job. I document everything I do at work so I don’t have to explain to the next person how to do things, I just point to the documentation and let them take it from there.

如果我有办法,我将使自己自动失业。 我记录了我在工作中所做的所有事情,因此我不必向下一个人解释如何做事情,我只是指向文档并让他们从那里取用。

If I have to do something repeatedly in Excel, I’ll write a macro. If there are commands I need to run all the time I’ll script it. I live by the Don’t Repeat Yourself (DRY) code so when it comes to testing you best believe I’m not going to follow the status quo of writing long test procedures in Microsoft Word or Excel. Neither do I want to sit around and run a bunch of manual procedures over and over each time there is a new feature introduced.

如果必须在Excel中重复执行某些操作,我将编写一个宏。 如果有命令,我需要一直运行,以编写脚本。 我靠的是“不要重复自己(DRY)”代码,因此在进行测试时,您最好相信我不会遵循在Microsoft Word或Excel中编写长测试程序的现状。 每次引入新功能时,我都不想坐下来反复执行一堆手动过程。

We’re all going to be replaced by robots one day anyway right? With that in mind, here’s an article on how to build automation into your workflow.

无论如何,我们都将有一天被机器人取代,对吗? 考虑到这一点,这里是有关如何在工作流程中构建自动化的文章。

机器人框架简介 (Introducing the Robot Framework)

You’ve probably heard of Selenium or Sikuli for web-based and user interface testing. I’ve used them on projects with limited to moderate success. The biggest hurdle that I’ve seen here is that writing good acceptance tests with these tool sets is that it requires a holistic view of the system (typically the systems engineers or business analysts) but writing the tests typically requires a moderate level of scripting or programming knowledge.

您可能听说过Selenium或Sikuli用于基于Web和用户界面的测试。 我已经在成功程度有限的项目中使用了它们。 我在这里看到的最大障碍是,使用这些工具集编写良好的验收测试是需要对系统(通常是系统工程师或业务分析师)进行全面的了解,但是编写测试通常需要中等程度的脚本编写或编程知识。

A person with the skill set to do both (i.e. a daywalker) isn’t always available so you have test engineers struggling to write tests or software tests making tests that don’t reflect the end-users needs, or that go overboard.

具有这两种技能的人(例如,一日游者)并非总是可用的,因此您的测试工程师很难编写测试或软件测试,而这些测试无法反映最终用户的需求,或者过于昂贵。

用简单的英语写作测试 (Writing Tests in Plain English)

The Robot Framework provides an open-source platform that can bridge that gap. Its modular setup has the ability to unlock a lot of power. Here are some examples of this:

机器人框架提供了可以弥补这一差距的开源平台。 它的模块化设置具有释放大量能量的能力。 以下是一些示例:

  • Data: These are the test cases that test engineers can write in simple terms (“user keywords”)

    数据:这些是测试工程师可以用简单的术语(“用户关键字”)编写的测试用例

  • Framework: Provides a test runner, reporting, logging and ability to integrate into your CI/CD pipeline

    框架:提供测试运行程序,报告,日志记录功能,并能够集成到您的CI / CD管道中

  • Libraries: When you write your test cases (I’ll show an example shortly) you can use keywords provided by libraries such as Selenium, Sikuli, or Database. Or you can combine these keywords into higher level ones called “user keywords”. For example, Selenium has keywords for Open Browser and Input Text. You could create your own user keyword called “Log into system” that is composed of the Selenium keywords.

    库:编写测试用例时(稍后将显示一个示例),您可以使用Selenium,Sikuli或Database之类的库提供的关键字。 或者,您可以将这些关键字组合成更高级别的关键字,称为“用户关键字”。 例如,Selenium具有用于打开浏览器和输入文本的关键字。 您可以创建自己的名为“登录系统”的用户关键字,该关键字由Selenium关键字组成。

工作示例入门 (Getting Started With a Working Example)

Let’s get to business. We’re going to set ourselves up to open up a browser, navigate to Google. Search for something and verify a result. This is just a taste of what we can do. There are so many more types of testing that you can do with different libraries such as REST API or image-based testing, see the built-in and external libraries for more here.

让我们开始做生意。 我们将设置自己来打开浏览器,导航到Google。 搜索内容并验证结果。 这只是我们可以做的事情。 您可以使用不同的库进行更多类型的测试,例如REST API或基于图像的测试,有关更多信息,请参见内置库和外部库

安装机器人框架 (Installing Robot Framework)

A habit that I’ve picked up from following other guides and tutorials is to create a Python virtual environment that uses venv. That way everything you install is nearly isolated from other projects you might have brewing on your machine.

我从以下其他指南和教程中学到的一个习惯是创建一个使用venv的Python虚拟环境。 这样,您安装的所有内容几乎都与您可能在计算机上酝酿的其他项目隔离了。

  1. Create a virtual environment. The first command creates a virtual environment and the second activates it. Read more about the Python virtual environment here.

    创建一个虚拟环境。 第一个命令创建一个虚拟环境,第二个命令将其激活。 在此处阅读有关Python虚拟环境的更多信息。

$ python -m venv myvenv
$ source myvenv/bin/activate

2. Next up, we install the Robot Framework.

2.接下来,我们安装Robot Framework

(venv) $ pip install robotframework

3. Now, since we are going to be working with web testing, we’ll install the Selenium library.

3.现在,由于我们将要进行Web测试,因此我们将安装 Selenium库

(venv) $ pip install robotframework-seleniumlibrary

4. Selenium is now installed but you still need to install the browser driver for the internet browser you want to use. You can download the driver manually or you can use WebDriverManager to take care of things for you.

4.现在已经安装了Selenium,但是您仍然需要为要使用的Internet浏览器安装浏览器驱动程序。 您可以手动下载驱动程序,也可以使用WebDriverManager自行处理。

(venv) $ 

After that installs, use this command to install drivers for Chrome and Firefox and have it put it in your PATH (e.g. /usr/local/bin).

安装完成后,使用此命令为Chrome和Firefox安装驱动程序,并将其放入您的PATH(例如/ usr / local / bin)中。

(venv) $ 

设置您的IDE (Set Up Your IDE)

If you’ve read any of my previous stuff, I’m all about using an IDE to do my work. This isn’t school, you can’t force me to use Notepad and force me to learn things the hard way anymore! Since I’m hooked on PyCharm, all I had to was go to the Plugins section and install Robot Framework Support.

如果您已经阅读了我以前的任何文章,那么我将使用IDE来完成我的工作。 这不是学校,您不能强迫我使用记事本,也不再强迫我学习困难的方法! 由于我迷上了PyCharm,因此我所要做的就是转到“插件”部分并安装Robot Framework Support。

Image for post
Source: Author
资料来源:作者

最后是一个例子! (Finally, an Example!)

The user guide is great but it can be overload, let’s bull rush through an example of performing a Google search and checking results. and you can look up the Robot Framework User Guide later to fill in the gaps.

用户指南很棒,但是它可能会很繁琐,让我们来举一个执行Google搜索和检查结果的示例。 并且您可以稍后查阅《 Robot Framework用户指南》以填补空白。

  1. Create a new Robot file. I’ll call mine “google_test.robot”.

    创建一个新的机器人文件。 我将其称为“ google_test.robot”

Image for post

2. By default, you’ll be started off with sections for Settings, Test Cases, and Keywords.

2.默认情况下,将以“设置”,“测试用例”和“关键字”部分开始。

*** Settings ***
Documentation Suite description
*** Test Cases ***
Test title
[Tags] DEBUG
Provided precondition
When action
Then check expectations
*** Keywords ***
Provided precondition
Setup system under test

3. We want to use Selenium for web testing so we import the Selenium library by adding a resource import in the Settings section.

3.我们要使用Selenium进行Web测试,因此我们通过在“设置”部分中添加资源导入来导入Selenium库。

*** Settings ***
Documentation Suite description
Library SeleniumLibrary

4. Now, let’s write a test case in plain English. The indentation is important here.

4.现在,让我们用简单的英语编写一个测试用例。 缩进在这里很重要。

  • “Perform Google Search” is the name of the test case

    “执行Google搜索”是测试用例的名称
  • [Tags] lets you specify tags (i.e. metadata) that you can later use for different purposes such as reporting or specifying which test cases you want to run

    [Tags]使您可以指定标签(即元数据),以后可以将其用于不同目的,例如报告或指定要运行的测试用例
  • [Teardown] is optional but lets you specify what you want done after the test is complete. For a web test, an example would be to close the browser, otherwise you’ll end up with a ton of open browsers.

    [Teardown]是可选的,但可让您指定测试完成后要执行的操作。 对于Web测试,一个示例是关闭浏览器,否则您将获得大量打开的浏览器。
  • All of the other phrases in between are the keywords which we’ll talk about next.

    介于两者之间的所有其他短语都是我们接下来将要讨论的关键字

*** Test Cases ***
Perform Google Search
[Tags] Example tag
Open Google
Enter Search Query
Check Search Results
[Teardown] Close Browser

5. Keywords are where we get all our power from. They let you write tests that are easy to understand by hiding all the complex details under the covers.

5.关键字是我们发挥全部力量的地方。 通过隐藏所有复杂的细节,它们使您可以编写易于理解的测试。

There are two categories of keywords:

关键字分为两类:

  • There are library keywords that come out of the built-in or external libraries. You can also develop your own custom library keywords using Java or Python. I like to call these the LEGO blocks.

    有些库关键字来自内置库或外部库。 您还可以使用Java或Python开发自己的自定义库关键字。 我喜欢称这些为乐高积木。
  • Then you have user keywords. These are keywords that you build up by using library keywords or other lower-level user keywords.

    然后,您就有了用户关键字。 这些是您使用库关键字或其他较低级别的用户关键字构建的关键字。

First, we want to open a browser. There’s a keyword for this in the SeleniumLibrary called “Open Browser. We give it two inputs, the URL and the browser we want to open it with. Note: Robot looks for at least two spaces to separate elements.

首先,我们要打开浏览器。 SeleniumLibrary中有一个名为“ Open Browser”的关键字。 我们给它两个输入,URL和我们想用来打开它的浏览器。 注意: Robot会寻找至少两个空格来分隔元素。

*** Keywords ***
Open Google
Open Browser http://www.google.com Chrome

Now we want to enter text in the search field. To do that we use the “Input Text” keyword from the SeleniumLibrary. This keyword needs a “locator” so we use the Inspect Element feature of the browser to see if there is an HTML ID or Name we can use to identify the search field.

现在我们要在搜索字段中输入文本。 为此,我们使用SeleniumLibrary中的“输入文本”关键字。 此关键字需要一个“定位器”,因此我们使用浏览器的“检查元素”功能来查看是否存在可用于标识搜索字段HTML ID或名称。

Image for post
Source: Author
资料来源:作者

I’ll go with the name as a locator which is “q”.

我将使用名称作为定位符“ q”。

Image for post

We can now create a keyword definition for Enter Search Query. Here we enter text in the search field (its HTML name was “q”), we wait until the search button is ready and then click it.

现在,我们可以为Enter Search Query创建关键字定义 在这里,我们在搜索字段中输入文本(其HTML名称为“ q”),我们等待直到搜索按钮准备就绪,然后单击它。

*** Keywords ***
Open Google
Open Browser http://www.google.com ChromeEnter Search Query
Input Text name:q My boss is awesome
Wait Until Element Is Visible name:btnK
Click Element name:btnK

Finally, a test should have some condition that it checks for so we check that the search result page contains something related to your search query. Putting it all together this is what the test case looks like:

最后,测试应该具有要检查的条件,因此我们检查搜索结果页面是否包含与您的搜索查询相关的内容。 放在一起,这就是测试用例的样子:

*** Settings ***
Documentation Suite description
Library SeleniumLibrary
*** Test Cases ***
Perform Google Search
[Tags] Example tag
Open Google
Enter Search Query
Check Search Results
[Teardown] Close Browser
*** Keywords ***
Open Google
Open Browser http://www.google.com Chrome
Title Should Be Google
Enter Search Query
Input Text name:q My boss is awesome
Wait Until Element Is Visible name:btnK
Click Element name:btnK
Check Search Results
Page Should Contain My boss is awesome

运行测试套件 (Running a Test Suite)

The file that contains your test case is called a test suite. To run it, enter the following:

包含您的测试用例的文件称为测试套件。 要运行它,请输入以下内容:

(venv) $ robot --outputdir results google_test.robot

This runs your Robot test and outputs the results to a folder name Results. The output should look like this:

这将运行您的机器人测试,并将结果输出到名为“ 结果”的文件夹中 输出应如下所示:

Image for post
Source: Author
资料来源:作者

结论 (Conclusion)

With all this automation knowledge in tow, go show the boss how awesome you are and how much time you can shave off the testing budget while improving the quality of the system! In my opinion, it’s about working smarter and not harder. I hope this article has helped you to implement these practices in your life.

掌握了所有这些自动化知识后,就可以向老板展示您有多出色,以及可以节省多少预算,同时提高系统质量! 我认为,这是关于更聪明而不是更努力地工作。 我希望本文能帮助您在生活中实施这些做法。

翻译自: https://codeburst.io/impress-your-boss-with-automated-testing-667139ab7c97

自动遍历自动化测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值