Automated testing - selenium environment construction


insert image description here

1. What is automation

Automation is the use of software tools, scripts or programs to perform a series of tasks, operations or processes without human intervention or guidance.

Automated testing: Use automated tools and scripts to execute test cases to verify the correctness and stability of the software. Automated tests can be executed quickly and can be repeated to reduce the cost and time of manual testing. Common automated testing tools include Selenium, Appium, JUnit, PyTest, etc.

In areas such as software development, testing, and operations, automation can help improve efficiency, reduce costs, reduce human error, and enable large-scale and repetitive work to be performed.

2. Automated test classification

There are two types of automated testing:

  1. Interface automated testing
  2. UI automation testing
    • Mobile automated testing
    • Web-side automated testing

Web-side automated testing refers to the use of automated tools and scripts to simulate and execute web application user behavior, and automatically verify the functionality, performance, and user experience of web applications.

In web-side automated testing, various tools and frameworks can be used to write and execute automated test scripts. Common web-side automated testing tools include:

  1. Selenium: Selenium is one of the most commonly used tools for web-side automated testing. It supports multiple programming languages, such as Java, Python, C#, etc., and can simulate user interaction in a web browser, such as clicking, entering text, selecting a drop-down box, etc.
  2. Cypress: Cypress is a modern web-side automated testing tool that provides an easy-to-use API and rich functions. It enables real-time monitoring and debugging of test scripts with efficient performance and stability.
  3. Puppeteer: Puppeteer is a Chrome browser-based automated testing tool that provides full control over the Chrome browser. It can simulate user interaction, generate screenshots and PDF files, perform performance analysis, and more.
  4. TestCafe: TestCafe is a cross-browser automated testing framework that can execute automated tests in different browsers. It is automated through the browser's built-in driver and does not require additional browser plug-ins or drivers.

3. Selenium environment construction

Why choose selenium among many automation tools?

  1. open source free
  2. Support multiple browsers, such as: Chrome, IE, etc.
  3. Support multiple systems, such as "Linux, Windows, MacOS, etc."
  4. Supports multiple programming languages
  5. The selenium package provides many APIs for testing

This article mainly introduces the use of selenium in the Java environment

Three piece set:

  • JDK (version at least 8).
  • Browser (Chrome or Edge recommended)
  • browser driver

Browser driver download path: https://www.selenium.dev/documentation/webdriver/getting_started/install_drivers/

Take Chrome browser as an example:

Go to the page and click Downloads
insert image description here
insert image description here
. There are many versions in it. At this time, we need to check the version of our current browser.
insert image description here
My Chrome version is 114.0.5735.199. When downloading the driver, it is necessary to ensure that the driver version corresponds to the browser version as much as possible.

Just choose the corresponding system, and you can download it directly for the windows system. win32.zipAfter
insert image description here
the download is complete, there is one in the compressed package. chromedriver.exe
insert image description here
You need to drag this chromedriver.exeto the bin directory of the jdk.
insert image description here
The above is the environment for selenium in the Java environment.

4. Test selenium

First open IDEA, create a Maven project and
insert image description here
import selenium dependencies in pom.xml in the project

<dependency>
	<groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.0.0</version>
</dependency>

There are two directories under the src path, we need to testwrite the test code in the java directory under the directory
insert image description here

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class Demo1 {
    
    
    public static void test() throws InterruptedException {
    
    
        // 创建驱动
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        Thread.sleep(3000);
        // 访问baidu.com
        driver.get("https://www.bilibili.com/");
        Thread.sleep(3000);
        // 关闭页面,释放资源
        driver.quit();
    }
    
    public static void main(String[] args) throws InterruptedException {
    
    
        test();
    }
}

The result of the program running is: Open the Chrome browser, enter the B station after 3 seconds, and close the page after 3 seconds.
insert image description here
If it runs normally, it means that your environment is successfully built.

Thank you for watching! I hope this article can help you!
Column: "Software Testing" is constantly being updated, welcome to subscribe!
"Wish to encourage you and work together!"

insert image description here

Guess you like

Origin blog.csdn.net/m0_63463510/article/details/131748092