Want to learn automated testing? These basics must be known!

Foreword:

The red mark is mainly for the convenience of memory, and to check the key points of personal feeling

Block quotes: Most of them are easy to understand, just take a look at them, mainly to distinguish them from normal text

1. What is automated testing

Automated testing is an important branch and part of software testing activities. With the development of the software industry, the market has higher and higher requirements for software cycle and software quality, which has spawned various development models, such as the common development model agile development. Testers put forward higher requirements. At this time, automated testing is generated, that is, to achieve the purpose of testing through tools or scripts. Software testing activities without manual or little manual participation are called automated testing.

自动化测试就是将测试流程从手工转换为自动化实现形式

Automated testing technology is a very popular and mainstream testing technology in the industry at present, and it is one of the core capabilities of testers.

2. What is the automated testing framework?

Reference document: Explanation of the most complete automated testing framework in 2023, it will not be impossible!

The most mainstream automated testing framework in WEBUI is based on selenium+wendriver

3. Why do we need automated testing?

①, black box test regression efficiency is low

②, Contingency and uncertainty of manual testing

③ Insufficient coverage of regression

④. The quality of delivered products cannot be guaranteed, it all depends on evaluation

⑤. The more complex the system, the more problems

⑥. Butterfly effect caused by long online time and high failure rate of components (fast iteration, more overtime)

4. What problems can automated testing solve?

①. Improve the response rate after problems occur

②. Reduce return cost

③. Improve regression coverage

④, improve regression efficiency

⑤. Improve the stability of regression

5. Advantages of automated testing

1) It is more convenient to perform regression testing on the system. When the software version is released more frequently, the effect of automated testing will be more obvious

2) It can automatically handle the original tedious and complicated tasks, provide more accurate tests, and provide staff enthusiasm

3) Automated testing is consistent and reusable, and test scripts can be tested repeatedly in different versions to ensure the consistency of test content

This is why in the field of software testing, automated testing has become the original trend, and more and more Internet companies regard automated testing as an important part of testing activities, which greatly liberates productivity.

6. What are the shortcomings of automated testing?

①. Cannot reduce cost input, but to speed up test result feedback and improve test quality

②. Automation is suitable for regression and smoke, not for finding bugs

③. The recording and playback function is tasteless, and visualization is not a good practice

ps: mainly when appium is doing AAPUI automation

④. Not all functions of all systems are suitable for automated testing

7. Automated test classification

1) web automated testing

Let the program replace the process of manually verifying the function of the WEB project

Many testing tools

ATP(UFT) payment

Robot framework

Selenium Mainstream:

Features: 1) It can support multiple languages, such as java/python/ruby

2) Free and open source framework

3) Can support multiple browsers, such as firefox/chrom/ie

  1. Multiple concurrent tests can be performed, and selenium can perform concurrent tests on multiple machines, thereby improving test efficiency. increase resource utilization

Winrunner

Testcomplete

Waiter…

Scenario: web page - for example: Ctrip

Slider verification in Ctrip login

2) App automated testing

tool:

The test tool that comes with Monkeyrunner AndroidSDK can support custom data and events for testing, and only supports Android 4.1 and above

The test tool that comes with Monkey AndroidSDK only supports random data and events, and generally performs stress testing on software

Ui automator Android's built-in test framework is based on java, and the code structure is relatively simple, but sdk16 (Android4.1 version)

Appium is an ios, android testing framework, supports multiple languages, can support multiple platforms, and can call selenium's api

3) Interface automated testing

Tools: postman/jmeter/soapui

Code: python+requests

8. Application scenarios of automated testing

UI自动化:主要用于回归测试,对系统流程进行校验

rf、airtest、selenium IDE4

Functional test: verify all functional modules of all processes, and verify functions through boundary values ​​and equivalence classes through various scenarios to ensure that the functions meet the requirements

In this process, more is to do process verification, every time a version is released, a new version returns, and regression testing is done

From the development environment to the test environment, from the test environment to the simulation environment, from the simulation environment to the production environment, every time the product is released to a new environment in the whole process, it is necessary to verify whether the process is implemented normally

After ensuring the implementation of the process, check whether the branches of other functions are correct

The first step and the second step are fixed

接口自动化:主要用于后端接口地逻辑测试,确保系统内不运行是有一定质量保障的

Interface testing: testing should be carried out continuously as early as possible in the testing process, and the sooner the bug is found, the sooner the cost can be repaired by the end of September

9. Introduction of mainstream automated testing technologies

Mainstream automation technology: selenium

Represents the only automated testing technology in the testing industry

Selenium is an absolute technical application in the field of ui automation testing

The structure is an automated test based on selenium+webdriver

The mobile appium is implemented based on selenium

Interface automation: realized by python+requests (80%)

UI automation includes WebUI and AppUI,

The current mainstream web automation implementations:

Realized based on Selenium+WebDriver

App automation implementation: based on Appium+driver

10. Implementation of automated testing for selenium

1. Environment deployment

Build a programming environment python
build a selenium environment pip install selenium
If the installation fails, there are generally two types:

1. Upgrade your pip command

yellow warning

2. Timeout encountered

red timeout

pip install selenium --default-timeout=1000

Build webdriver environment
Browser driver installation, mainstream chrome/firefox/safari/ie/

Downloads | Selenium
insert image description here

If you want to download an older version

CNPM Binaries Mirror

(python要和webdriver版本要一一对应)

Put the downloaded driver file in the python installation root path

Ps: Automated execution, all browsers are installed in the default path

这个只是单纯的省去了配置环境变量的工作

To implement testing
安装pycharmas a code editing tool, you can use the community version when installing. The biggest difference between the professional version and the community version is the use of the Django framework

11. When using pycharm to create a new project, how to choose the virtual environment and the local environment
to facilitate switching between different projects, otherwise too many libraries are installed in one project, and some need to uninstall unnecessary libraries, and then install the required libraries, which is more troublesome, so There is a virtual environment,虚拟环境和本地环境的库互不干扰,独立存在的。

How to install libraries in physical environment and virtual environment

The physical environment installation library can be installed directly using the pip command (you can also install the library in the local environment directory),

To install a third-party library in a virtual environment, you can use the cmd command line in the virtual environment installation directory to use pip to install a third-party library
under venv/Scripts

or通过项目file-setting安装库

在新建项目时,选择虚拟环境或本地环境,当setting时安装的库就在对应环境中

12. Working principle of selenium+webdriver

insert image description here

13. Why should the functions that can be realized by linear code be realized through the framework (selenium+webdriver+keyword-driven+data-driven)

线性代码没有任何可以被优化被复用的价值的东西

Enterprise: The purpose of an automated test development is to develop 公司可复用的自动化测试框架, rather than write a test framework that only you can use

1、能够复用到各种需要的项目中的

2、能够让其他的测开人员可以一同维护

3、能够以最低的使用成本和学习成本来上手

4、能够方便于后期维护和更新修改

To achieve such an effect, the design and development of the test framework is required.

14. A simple selenium+webdriver linear code

Ps: The find_element_by_xpath/id/name method in the code has been disabled and changed to find_element()

case:

fromselenium.webdriver.common.byimportBy

element_agree=driver.find_element(By.CSS_SELECTOR,‘div.pop_footer>a.reg_btn.reg_agree’)

14. The design pattern of the automated testing framework implemented in the form of keyword-driven + data-driven

After reading the data, splicing is based on the data content in a fixed format. The spliced ​​content is passed in as the parameter required by the keyword, so as to execute the corresponding keyword, and then compare the result with the expected one to obtain a single test case The result of whether the execution is passed

15. Design concepts and ideas

Based on keyword-driven combined with data-driven, and then encapsulate some of the main commonly used content to form a class object, and then use this class object as a class provided by a tool, and then combine unitTest or ddt or pyTest to test cases An integration, or to make this data-driven file in the form of Excel or yaml, through this form of file reading and writing, combined with the call of keywords, to achieve a series of processes, and then through a write operation , make an assertion, then we have completed a set of basic test cases.

16. The difference between quit and close:

Quit is to close the browser and close the ChromeDriver process

close is to close the current tab

17. The realization of the mainstream po mode of the enterprise pageObject

The principle of po explains
PageObject mode:
PO mode is currently recognized as the best automation design mode in the testing industry. It is an upgrade from the keyword-driven mode.

A model based entirely on page implementation

PO design principle:
1. Extract the common method of selenium as the basic object

2. Associated design of page business process

3. Call the business process to realize the automated test

Advantage:

1. Encapsulate different pages into different page objects and manage them separately

2. Further reduce redundancy

3. The readability and maintainability of the code have been further improved

4. Test and develop the company's system, combined with the needs and business, combined with the actual project situation, design the corresponding test framework and test tools, provide testers to improve the power, and perform maintenance

Po mode realizes
po mode + unitTest realizes automation

18. Purpose of automated testing:

Regression and automatic execution of existing processes in the system

19. At this stage, the mainstream testing framework packaging forms in the testing industry:

1. Keyword-driven form
All commonly used Selenium.Appium.Request module functions are re-encapsulated, combined with data-driven to realize keyword calls, thereby realizing automated testing.

The keyword-driven class of the web: mainly used for secondary encapsulation of commonly used functions in Selenium.

Selenium is equivalent to a large supermarket, and the behavior of secondary packaging is equivalent to going to the supermarket to shop, purchasing all the commonly used functions home, so that they can be called directly from home when using

Common functions:

1. Access url

2. Create webdriver

3. Positioning elements

4. Input

5. Click

……

2. The page object model
POM.PO is currently an automated test design pattern widely used in the ui layer. The encapsulation framework based on this form is mainly used for the automated test coverage of a single system.

19. How do you promote the implemented automated testing framework in the company?

The application object of the automated testing framework is not yourself, but other test executives and developers of the R&D team.

1. Demo demonstration meeting, used to introduce the usage specifications and situation of the existing framework

2. Deploy the framework and combine it with the specified project for pilot

3. Generate a standardized document, and use the standardized definition of the process to learn and train all the roles that need to be implemented.

4. Continuously optimize the framework based on the company's actual business needs and project needs.

All promotional actions must pull a leader who has the right to speak.

20. What data-driven forms have you learned about, and what are their advantages and disadvantages?

Yaml:

Advantages: easy maintenance, data portability.

Disadvantages: The learning cost is too high, and the data writing is not intuitive.

Excel:

Advantages: Well maintained, when your framework is formed, you basically don’t need to touch the source code, you only need to understand the specification format, and you can use automation at zero cost.

Disadvantages: All behaviors need to be set with solidified content, which is relatively rigid and complicated.

Json:-

class: - similar to yaml

Text

……

21. How to optimize the existing testing framework?

It can be optimized from the perspective of the distributed structure design of the test framework and the efficiency of parallel processing of multiple use cases.

Change the running process of the original test framework from linear to parallel processing mode.

22. Selenium three waiting

The role of waiting:
in order to improve the success rate of automatic operation

Automation is operated by machines, converting what is performed by manual testing into machine execution

At the designated place, wait for the system to respond before proceeding

Three ways to wait:
1. Forced waiting
All automatic learners, the first contact with waiting is forced waiting (Sleep)

Function: When the code runs to the line of mandatory waiting, no matter what the situation is, it is mandatory to wait for the specified time

It needs to be imported through sleep in the time module to use

From time import sleep - sleep(2)

Import time - time.sleep(2)

Advantages: easy to get started, good at debugging

Disadvantage: It can only take effect for a single time, unable to make effective judgments, and will waste a lot of time

2. Implicit waiting
is set directly through the implcitly_wait() method of the WebDrive object

implcitly_wait(10)

Hidden waiting, one-time setting, valid for life, setting the waiting time for the current WebDriver object

Function: Set a global wait for the WebDriver object. For each operation, if a page is loaded, it will enter the implicit wait by default

If the element cannot be found, enter the implicit wait, and when the maximum waiting time is reached, continue to the subsequent code

Advantage: You can set it once, and no error will be reported

Disadvantage: You must wait for the page to be loaded before entering subsequent operations, or wait for a timeout before entering subsequent operations

3. Explicit waits
aboveboard, etc., dedicated to specified elements and objects

WebDriverWait(driver, 10, 0.5) finds the dirver object every 0.5 until the search time reaches 10s

By importing fromselenium.webdriver.support.waitimportWebDriverWait

Advantage: It is specially used to wait for a specified element

Disadvantage: It must be declared that it only takes effect for a single time, and the complexity is higher than other waits

WebDriverWait(driver,10,0.5).until(condition) found element enters filter condition

WebDriverWait(driver,10,0.5).until_not() can't find the element to enter the filter condition

When multiple waits are called at the same time, the waiting time of the system depends on the longest waiting time

23. What are the pitfalls that should be avoided in the automation process:

1. The element cannot be found (consider whether there is a switch between iframe and handle, the element positioning technology is not skilled, whether it is reasonable to wait for the addition)

Automatically generated id, copying is useless and needs to be written by hand

2. The code success rate is not high

3. Not knowing when to use which technology

Learn mainstream technology

24. The basic positioning of the eight elements of selenium

Element positioning:

All UI layer automation is based on element positioning.

All elements to be operated are WebElement objects (network element objects)

element = HTML tag

Basic format:

text

Label:

a: hyperlink

img: image

Input: input box, file upload

Button: button

Generic: div li span ...

In the actual system, the label type of an element is not determined by the appearance, but by the CSS style sheet.

When automating, it is to locate tags based on their attributes.

How to locate the precise element you need to operate:

Eight element positioning

1.id, locate based on the value of id in the element attribute

The id is similar to the ID number on people's ID card, and it will not be repeated without accident.

2.name, locate based on the value of name in the element attribute

name is similar to the name on people's ID card and will not be repeated.

3.link text, mainly based on hyperlinks for positioning

4. Partial link text, the fuzzy query version of link text, similar to like % in the database

When the fuzzy query matches multiple eligible elements, select the first one

5. classname, based on the element style for positioning, is very easy to encounter repetition.

6.tagname, the tag name is used for positioning, with the highest degree of repetition, and only when the positioning is required, the secondary deletion is performed

7. cssselector, a behavior with relatively many applications. Initially, the IE browser did not support xpath, and the positioning was completely based on the class attribute.

Webpage f12, select right click copy selector

8. xpath, the most widely used behavior at present, is based on the positioning of the page structure

Webpage f12, select the right button to copy xpath

Absolute path: do not write

Relative path: Find elements based on the matching system, and search according to the xpath syntax structure

For example: //*[@id="kw"]

//Indicates to start searching from the root path

*Any element

[] Indicates filter conditions (find function)

@ means to filter based on attributes, for example, id="kw" means to filter based on the condition that the id attribute value is kw.

Confirm that the xpath path is correct:

1. Use crl+f to find and judge on the element page of the developer tool

2. Enter $x() in the console to verify

If the element is positioned based on text

Add text()="text content" in [] to search, for example: //a[text()='login'

When you locate an element and cannot locate it directly, you can get the element by locating the child element and returning to the parent

//a[text()='login'/... locate the upper level through the child level

Linux back up one level.../

//input[@id=“kw”]

Function writing: //input[contains(@id,'kw')]

contains indicates further search, fuzzy search for matching items

//input[contains(text(),‘ddddd’)]

Further look for ddddd in the text in the input tag

25. Selenium understanding

At present, the core technology in the industry is automated testing.

The most mainstream automated testing technology is Selenium

Appium is implemented based on Selenium inheritance.

The core of Selenium is implemented based on JS Core.

In the current test, only Selenium+WebDriver will be used to realize automated testing.

26. Environment construction:

Install Selenium

1. Through Pip install selenium

2. Install through the interpreter in Pycharm (setting-project-…)

Mac :pip3 install selenium

Install WebDriver

Download and install through Baidu

FileFox: geckodrover, adaptation takes into account Selenium version and browser version

Regardless of the type of browser, be sure to turn off automatic updates

27. Practical explanation of Python+Selenium:

The operating principle of WebDriver+Selenium:
WebDriver is a server. When it is started, it starts a service, and the service uploads and sends instructions based on the HTTP protocol.

WebDriver is a server-side proxy. When automation ends, you need to remember to release resources

Driver.quit()

28. How can Driver.get() access url?

Because get() encapsulates an execute method

Pass a request to the browser driver through the Http network protocol, and this command is based on the command command in selenium, named GET, through this GET command to send commands to WebDriver, and access url when requesting

29. What functions should the components that constitute the framework have?

Log: log recording and management functions, set different log levels for different situations, to facilitate problem location;

Report: test report generation and management and instant notification, quick response to test results;

Source: The management of configuration files and static resources follows the principle of high cohesion and low coupling;

Common: The management of public functions, methods, and common operations follows the principle of high cohesion and low coupling;

TestCase: test case management function, one function point corresponds to one or more cases, and the coverage rate is improved as much as possible;

TestData: test data management function, separate data and scripts, reduce maintenance costs and improve portability;

TestSuite: test component management function, assemble and build different test frameworks for different scenarios and different needs, and follow the flexibility and scalability of the framework;

Statistics: test result statistics management function, statistics, analysis, comparison and feedback of the results of each test execution, data-driven, providing reference for software optimization and process improvement;

Continuous: The continuous integration environment, that is, the CI environment, includes functions such as test file submission, scan compilation, test execution, report generation and timely notification. Continuous integration is the core of automated testing!

Common frameworks all have common features: programming language + unit testing framework + scanning and compiling tools + continuous integration tools + database + project management tools.

python+selenium+unittest+Git+Jenkins+MySQL+testlink/redmine

Programming languages: writing test scripts, logging and output; python

Unit test framework: provide a series of configurations such as test script running and exception verification; unittest

Scanning and compiling tools: Scanning and compiling test files is generally better when used with continuous integration tools;

Continuous integration tool: Jenkins, the classic continuous integration tool;

Database: test data management; mysql

Project management tools: test result statistics management; git

30. Design of automated testing framework?

Reference document: Talking about the design of automated testing framework - Andrew213 - Blog Park

Design from the architectural level, adopt the common MVC framework idea of ​​java development, divided into logic control layer (Controller), data persistence layer (Model), page display layer (View)

ps: The logical control layer (Controller) that I understand is generally in the middle of the project, the interface layer, which plays a role of linking the past and the future. It can be connected to the persistence layer to obtain the required data through the sql statement in the persistence layer, and then pass the data through Some logic processing is passed to the page display through the interface

The data persistence layer (Model) is at the bottom of the project, connects to the database, and performs some simple additions, deletions, changes, and queries on the database, mainly to write some methods to operate the database

The page display layer (View) obtains data from the logic control layer, which is the interface level I understand, through the interface passed from the background, and outputs the style on the page

The MVC framework is based on java. There is no such statement in python at present. The meanings of keyword-driven + database-driven and POM architecture in python are the same.

Python is widely used in the field of automated testing, so I will use python as an example:

Logic control layer:Selenium适用于Web自动化,真实模拟用户操作浏览器;request应用于接口自动化;unittest是一个目前很流行实用的单元测试框架,有完善的用例管理模块。

Data persistence layer:用例测试数据的管理,可以通过csv文件,yaml等文档形式去管理测试数据,可以通过MySQLdb+MySQL的方式管理数据库。

#数据库连接
import MySQLdb
 
# 打开数据库连接
db = MySQLdb.connect("localhost", "testuser", "test123", "TESTDB", charset='utf8' )
 
# 使用cursor()方法获取操作游标 
cursor = db.cursor()
 
# 使用execute方法执行SQL语句
cursor.execute("SELECT VERSION()")
 
# 使用 fetchone() 方法获取一条数据
data = cursor.fetchone()
 
print "Database version : %s " % data
 
# 关闭数据库连接
db.close()

Python operation MySQL database reference document: Python operation MySQL database | rookie tutorial

What are the specifics of operating the database?

Page display layer: It is mainly the display of test reports, and the display of test reports is realized by using HTMLTestRunner.

ps: HTMLTestRunner is HTMLTestRunner.py file, not a framework nor a plug-in

The above is just understanding, specific design?

Combining the documents I wrote before, I can summarize myself:

Python connects to mysql (simple code)_mihxy's blog-CSDN blog_python connects to mysql database code

A link to the log is added here, and the process of displaying the test report will be displayed

Add another yaml file to read and write

Finally : In order to give back to the die-hard fans, I have compiled a complete software testing video learning tutorial for you. If you need it, you can get it for free【保证100%免费】

Software Testing Interview Documentation

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.

全部资料获取:

insert image description here

Guess you like

Origin blog.csdn.net/weixin_54696666/article/details/131524242
Recommended