The former senior test engineer of Meituan teaches you how to use web automation testing

1. Basic introduction to automated testing
1 Overview of automated testing:

What is automated testing? Generally speaking, all methods that can replace manual testing belong to automated testing, that is, the process of simulating human execution of use cases through tools and scripts.

2 The role of automated testing

Reduce software testing time and cost Improve software quality
Enhance testing efforts by increasing test coverage
Perform manual testing tasks that are difficult to complete, require higher cost, longer planning, and higher quality Iterative
updates are less, but testing is still required Maintained by personnel, liberate manpower through automation
3 main applications of automated testing:

Smoke test (main business process)
regression test
performance test
compatibility test (a set of test scripts, executed on multiple platforms)
to complete the work that cannot be done by manual test Unattended test after work
4 Goals achieved by web automation:

(1) Principles:

Write an automated test case library, and write test cases based on the use cases in the use case library.
Improve test efficiency and reduce test cost
Automated implementation of highly repetitive use cases
Rapid regression testing to improve the speed and quality of version releases
Functional coverage meets requirements
Tests are portable and repeatable
(2) Implementation strategy (continuous integration) :

Framework selection
Environment construction Case writing: extracting public modules, extracting public parameters, familiarizing with log output
of functional logic Report output Jenkins continuous integration: timing integration, sending email 2. Web automation tool 1 Web automation tool selection:




There are two types of automation tools on the market: open source and commercial paid. The following provides a comparison of two types of mainstream selenium and QTP

Final choice: selenium+IDEA (java+maven+testng)+jenkins

3. Introduction to Selenium
(1) Selenium test principle:

In the process of automated testing, there are three components: client script + browser driver + browser terminal.
Driver file, take geckodriver.exe as an example. After this executable driver file is started, it is equivalent to a server that exposes a series of interfaces and listens to a certain port.
Client operations (accessing pages, locating elements, inputting data, clicking buttons, etc.) are encapsulated into interface requests (eg: /session/xx/yy), and then submitted to the driver server.
After the driver server receives the request from the client, it interacts with the terminal browser.
The terminal browser acts accordingly (operating elements, and even the browser itself: screenshots, windows, installation of plug-in certificates).

(2) A brief introduction to the selenium tool suite

Selenium WebDriver: Object-oriented API.
Selenium IDE (Integrated Development Environment): FireFox plug-in, which is used to provide a graphical interface to record and playback scripts. The plug-in is only a tool for simulating prototypes, and test engineers are not expected to use this tool to run a large number of test scripts. This plug-in needs to use a third-party javaScript code base to support looping and conditional judgment.
Selenium-Grid can execute test scripts in multiple test environments in a concurrent manner to achieve concurrent execution of scripts and shorten the execution time of a large number of test scripts.
Four, Selenium WebDriver common API
(1) selenium WebDriver common basic API

(1) Browser operation

Load the browser driver and open the page:
driver = new FirefoxDriver();
String baseUrl = "http://oa2.midairen.com/index.html";
driver.get(baseUrl);

Close the browser:
driver.close();//Close the browser

Maximize the window:
driver.manage().window().maximize();

Go back to the previous page:
driver.navigate().back();

Forward to the next page:
driver.navigate().forward();

Refresh the page:
driver.navigate().refresh();

Get title and print
String title =driver.getTitle();

Kill the browser process of Windows Take
a screenshot of the current browser window (compared with screenshots)
Operate the browser's cookie
(2) Page operation

Get the source code of the page
Get the URL address of the page
Clear the original text in the input box
Enter the specified content in the input box
Click the button Double-
click an element
Operate the radio drop-down list Operate the radio
box
Operate the check box
to check the text content of the element Does it appear
Execute JS script
Manipulate page elements in iframe
Manipulate rich text
(3) Element positioning method:

5. TestNG
(1) Basic introduction of TestNG:

TestNG is a testing framework in Java. It is a very popular and practical unit testing framework. It has a complete use case management module and can easily manage third-party plug-ins with Maven. Using TestNG can be used for automated testing of functions, interfaces, units, and integrations. The most common is to combine selenium for functional automated testing, which uses Java annotations to write test methods.

Testers generally use TestNG to write automated tests, and developers generally use Junit to write unit tests. The main reason why TestNG is suitable for testers: TestNG is more suitable for complex integration tests.

(2) Features of testNG:

Annotations
TestNG uses Java and object-oriented features
Supports comprehensive class testing (eg, by default, there is no need to create a new test as an instance of the class for each test method)
Independent compile-time test code runtime configuration / data information
Flexible runtime configuration
Supports dependent test methods, parallel testing, load testing, partial faults
Flexible plug-in API
supports multi-threaded testing
(3) Notes:

The test case organization structure commonly used by TestNG consists of test Suite-test-test class-test method. A test suite consists of one or more tests, a test consists of one or more test classes, and a test class consists of one or more test methods. When using different levels of test cases, the class realizes the initialization work before the test, the execution work of the test case and the cleaning work after the test through different annotations.

Commonly used annotations are as follows:

(4) Dependency testing

     Some complex test scenarios need to execute test cases in a specific order to ensure that the test cases are executed in a specific order. This test scenario operation requirement is called dependency testing. With dependency testing, data and program state are shared between different test methods. Implemented using the dependsOnMethods parameter.

@Test(dependsOnMethods = {"testcase1"})

(5) Assertion

      When executing automated test cases, we need to automatically judge whether the output value obtained after the execution of the test case is consistent with the expected value. At this time, we need to use the assertion function. An Assert class is provided in TestNG: org.testng.AsserTestNG provides an Assert class, and the org.testng.Assert class is a container for a series of static methods of assertions.

Assert.assertTrue(select1.isDisplayed());//Assert to determine whether the select1 element exists on the page

Common assertions:

assertTrue: Determine whether it is true.
AssertFALSE: Determine whether it is FALSE.
AssertNull: Determine whether it is empty
AssertNoNull: Determine whether it is not empty
AssetEquals: Determine whether it is equal
AssertNoEquals: Determine whether it is unequal
 

Guess you like

Origin blog.csdn.net/2301_76643199/article/details/131067488