Selenium 简明教程

Selenium - Automation Frameworks

Selenium Webdriver 可以用来开发基于关键字、数据和混合驱动框架的测试脚本。关键字驱动框架主要用于创建功能测试用例,其中测试用例的设计和开发具有明确的界限。数据驱动框架主要用于创建依赖于来自外部数据源的数据的测试用例,并且也可以用于回归测试。混合驱动框架是关键字驱动框架和数据驱动框架的结合。

Selenium Webdriver can be used to develop test scripts which are based on the keyword, data, and hybrid driven framework. A keyword driven framework is mostly used to create functional test cases where there is a clear demarcation between the design of the test cases and development. A data driven framework is mostly used to create test cases which are dependent on data available from an external source and can also be used for regression testing. A hybrid driven framework is a combination of a keyword driven and data driven framework.

Need of Automation Frameworks

让我们来看一个情况,即让一个三人团队为同一应用程序创建测试脚本。除了为某些需求编写测试脚本之外,他们还需要在测试脚本中结合日志记录和报告逻辑。

Let us take a situation where a team of three people are asked to create test scripts for the same application. Along with writing the test scripts for certain requirements, they also need to incorporate logging and reporting logic within the test scripts.

在没有框架的情况下,所有三位团队成员都将开始以适合他们匹配技能集的方式创建测试脚本。除此之外,所有测试脚本将采用不同的方法开发,这可能使得难以一起执行所有测试脚本。

In the absence of a framework, all the three team members will start creating test scripts in a way they are comfortable with their matching skill sets. Apart from that, all the test scripts will be developed by taking different approaches which may make it difficult to execute all the test scripts together.

此外,每个测试脚本都将遵循自定义日志记录,这使得执行后难以收集日志。给他们的需求可能需要从外部来源获取数据。在这种情况下,团队成员可能对数据进行硬编码,或使用 Excel 文件或 xml 文件等。因此将不会有任何统一性。

Moreover, every test script will follow custom logging which will make it difficult in collecting logs post the execution. The requirements given to them may require data to be fetched from external sources. In such a situation, the team members may hard code the data, or use an excel file, or an xml file, and so on. So there will not be any uniformity.

给团队的需求可能具有共同的功能,如登录应用程序。在没有框架的情况下,每个团队成员都将为通用登录功能创建测试脚本以及其他唯一需求。因此,无法重复使用登录功能,导致测试脚本重复。

The requirements given to the team may have a common functionality like login to the application. In the absence of a framework, each team member will create test scripts for the common login feature along with the other unique requirements. Thus the login feature can not be reused leading it duplicacy of test scripts.

在将测试脚本报告为通过/失败时,团队的每个成员都将采取不同的方法,这使得执行后难以报告。此外,测试脚本可以在不同的测试运行器中执行,如使用简单的 Java、TestNG、JUnit、Maven 等。在没有框架的情况下,每个团队成员都可以采用不同的测试脚本运行方法,因此我们可能无法像套件一样运行测试脚本。

While reporting the test scripts as pass/fail, a different approach will be taken by each member of the team which will make it difficult in reporting post the execution. Also, test scripts can be executed in different test runners like using plain Java, TestNG, JUnit, Maven,and so on. In the absence of a framework, each team member can follow a different approach of running the test scripts, thus we may not be in a situation to run the test scripts as a suite.

要修复这些问题,我们需要自动化框架。

To fix these issues we need automation frameworks.

What is an Automation Framework?

自动化框架是一组标准、假设和准则,用于创建每个人都应遵守的测试脚本。每个组织或团队都可以使用不同的框架,但每个组织或团队都应遵循一组通用的规则。

An automated framework is a group of standards, assumptions and guidelines for creating test scripts that everyone should adhere to. Every organization or team may use different frameworks but each of them should be guided by a common set of rules.

框架开发是一个持续的过程,需要一段时间才能开发出一个详尽的框架。框架需要定期更新和维护。例如,某些库可能会被弃用,为此我们可能必须更新库,在某些情况下可能会添加额外的库以使框架更强大。

A framework development is a continuous process and it needs time for development of an exhaustive framework. A framework requires regular updates and maintenance. For example, some libraries may be deprecated, for that we may have to update the libraries, additional libraries may be added in some cases to make the framework more powerful.

经过一段时间后,一旦动态框架经过开发,就可在极短的时间内开发出新的测试脚本。随后,维护也变得相当容易。

Once a dynamic framework has been developed after a period of time, new test scripts can be developed in a very short span of time. Along with that, maintenance also becomes relatively easier.

Components of an Automation Framework

自动化框架的组件包括 −

The components of an automation framework are −

Standardization of Test Cases(Tests)

测试用例应以统一方式开发。例如,可通过遵循名为页面对象模型的设计模式方法创建测试用例。

The test cases should be developed in a uniform manner. For example, test cases can be created by following a design pattern approach called the Page Object Model.

Standardization of Logging(Logger)

应开发测试用例,以便正确记录在执行期间出错或正确的情况,并进行调试。

The test cases should be developed such that there is proper logging to examine what went right or wrong during execution, and to debug.

Standardization of Test Data and Configuration(Data Providers)

应开发测试用例以尽量减少硬编码,应在独立文件中维护测试数据和配置。因此,在发生更改时,我们仅需要更新这些文件,而无需更新其他区域。

The test cases should be developed such that there is minimal hard coding and test data and configurations should be maintained in separate files. Thus, in case there are changes, we will need to update only those files and not other areas.

Standardization of Utility Libraries(Helper/Reusable libraries)

有些实用程序与项目无关,如在失败时捕获屏幕截图、同步、生成报告、记录,等等。此外,还有些实用程序与项目相关且大多可重复使用,如登录功能。

There are some utilities which are project independent like capturing screenshots on failures, synchronizations, generating reports, logs, and so on. In addition to this, there are some utilities which are project dependent and mostly reusable like the login feature.

Test Execution Engines(Test Runner)

应识别并定义恰当的执行引擎。测试用例可通过 Jenkins、Maven、TestNG 等运行。

There should be proper execution engines identified and defined. The test cases can run from Jenkins, Maven, TestNG, and so on.

Standardization of Reporting(Reporter)

应进行恰当的报告,以展示执行结果如何传递给项目利益相关者。报告应至少涵盖运行测试的功能、测试总数、通过和失败测试的次数、测试持续时间、失败屏幕截图,等等。

There should be proper reporting to showcase how the execution went to project stakeholders. A report should at least cover the features on which the tests ran, total number of tests, count of passed and failed tests, duration of tests, screenshots of failures, and so on.

Types of Automation Frameworks

下列为自动化框架的类型 −

The types of automation frameworks are listed below −

Keyword Driven Framework

关键字驱动测试框架是自动化和手动测试人员广泛使用的最受欢迎的测试框架之一。

A keyword driven testing framework is one of the most popular testing frameworks used extensively by both automation and manual testers.

Data Driven Framework

数据驱动测试框架是测试领域中另一个广泛使用的框架,其中数据和实施代码独立维护。

A data driven testing framework is another widely used framework in the testing world where the data and implementation code are maintained separately.

Hybrid Driven Framework

混合驱动框架结合数据驱动和关键字驱动框架使用。测试数据和关键字(即在应用程序上执行的操作)都从外部文件馈入。

A hybrid driven framework works in combination of data driven and keyword driven frameworks. Both the test data and keywords(i.e the actions to be performed on the application) are fed from external files.

外部测试数据源可以是任何具有 .txt、.properties、.xlsx、.xls、.csv 扩展名的文件,也可以在数据提供程序的帮助下。关键字可以来自 Java 类文件、excel 文件,等等。

The external test data source can be any file having extensions with .txt, .properties, .xlsx, .xls, .csv, and also with the help of data providers. The keywords can be from Java class files, excel files, and so on.

Conclusion

本文的 Selenium Webdriver 自动化框架教程就此结束。我们从描述为什么我们需要自动化框架、什么是自动化框架、自动化框架的组件和类型、并描述了关键字驱动、数据驱动和混合框架开始。这将为您提供有关 Selenium Webdriver 中的自动化框架的深入知识。明智的做法是不断练习您所学的内容,并探索与 Selenium 相关的其他内容,以加深您的理解并拓展您的视野。

This concludes our comprehensive take on the tutorial on Selenium Webdriver Automation Frameworks. We’ve started with describing why we need automation frameworks, what is an automation framework, what are the components and types of an automation framework, and described keyword driven, data driven, and hybrid frameworks. This equips you with in-depth knowledge of the automation frameworks in Selenium Webdriver. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.