Table of Contents
1 Automated testing
1.1 Unit testing
1.2 Interface testing
1.3 UI Test
1.3.1 Advantages of UI automated testing
1.3.2 Applicable objects for UI automated testing
1.4 Automated testing process
2 selenium
3 selenium IDE recording script
Home Backend Development Python Tutorial How to use selenium, the Python automated testing tool

How to use selenium, the Python automated testing tool

May 17, 2023 am 10:43 AM
python selenium

    1 Automated testing

    Automated testing refers to the automation of software testing, running applications or systems under preset conditions. Preset conditions include normal and abnormal , and finally evaluate the running results. The process of converting human-driven testing behavior into machine execution.

    How to use selenium, the Python automated testing tool

    Automated testing includes UI automation, interface automation, and unit test automation. Automated test planning according to this pyramid model can produce the best automated test output-to-input ratio (ROI) and obtain good benefits with less investment.

    1.1 Unit testing

    The biggest investment should be in unit testing, and unit testing should be run more frequently.

    Java’s unit testing framework is Junit.

    1.2 Interface testing

    Interface testing is API testing. Compared with UI automation, API automation is easier to implement and more stable to execute.

    Interface automation has the following characteristics:

    • It can be intervened in the early stage of the product and after the interface is completed

    • The amount of use case maintenance is small

    • Suitable for projects with small interface changes and frequent interface changes

    Common interface automation testing tools include RobotFramework, JMeter, SoapUI, TestNG HttpClient, Postman, etc.

    1.3 UI Test

    Although the testing pyramid tells us to do as much automated testing of the API layer as possible, automated testing of the UI layer is closer to the needs of users and the actual business of the software system . And sometimes we have to perform UI layer testing.

    Features of UI automation:

    • Large amount of use case maintenance

    • The page is highly relevant and must be developed later on the project page Later intervention

    • UI testing is suitable for projects with small interface changes

    There are many testing frameworks for the UI layer, such as Windows client testing AutoIT, selenium for web testing and TestPlant, eggPlant, Robot framework, QTP, etc.

    1.3.1 Advantages of UI automated testing

    Reduce the human investment in large-scale regression testing caused by changes or multi-phase development of large systems. This may be the most important task of automated testing. Especially when the program is modified frequently, the effect is very obvious. In the early stage of automated testing, more manpower is invested, but after entering the maintenance period, a lot of manpower can be saved, while in the later stage of manual testing, a lot of manpower is needed for regression testing

    • Reduce the time of repeated testing and achieve rapid regression testing

    • Create an excellent and reliable testing process and reduce human errors

    • Can run more and more tedious tests

    • Can perform some tests that are difficult or impossible to test manually

    • Better utilization Resources

    • Reusability of test scripts

    1.3.2 Applicable objects for UI automated testing

    Those who implement automated testing Prerequisites: requirements change infrequently, project cycle is long enough, and automated test scripts can be reused.

    Suitable for automation projects:

    • Product type projects. For product-type projects, the new version is an improvement on the old version, and the functions of the project are not changed much. However, the new and old functions of the project must be repeatedly regression tested. The advantage of automated testing is regression testing, which can effectively verify whether new defects have been introduced and whether old defects have been fixed. To a certain extent, automated testing tools can be called regression testing tools.

    • Test mechanically and frequently. In a long-term project, the same large amounts of data need to be entered over and over again. Such as compatibility testing.

    The following projects are not suitable for automated testing:

    • Projects with frequent demand changes, automated scripts cannot be reused, and maintenance costs are too high , low cost performance

    • The project cycle is short, the automated script is not used many times after completion, and the cost performance is low

    • A project with strong interaction, For projects that require manual intervention, automation cannot be implemented

    1.4 Automated testing process

    • Analysis: Overall grasp the system logic and analyze the core system of the system architecture.

    • Design: Design test cases. The test cases should be clear and clear enough, with broad and precise coverage.

    • Implementation: Implementation scripts, there are two The first requirement is assertion, and the second requirement is reasonable use of parameterization.

    • Execution: Executing the script is far from as simple as we imagined. Abnormalities during script execution require us to carefully analyze the causes.

    • Summary: Analysis of test results and summary of the test process are the keys to automated testing.

    • Maintenance: The maintenance of automated test scripts is a problem that is difficult to solve but must be solved.

    • Analysis: In-depth analysis of the coverage risks of automated use cases and the cost of script maintenance during the automated testing process.

    2 selenium

    Selenium is a UI-based automated testing framework for web applications, supporting multiple platforms, multiple browsers, and multiple languages.

    The early selenium RC has been replaced by the current webDriver, which can be simply understood as the selenium1.0 webdriver, and the current Selenium2.0. Typically, we use the term "Selenium" to refer to Selenium 2.0. Selenium includes three components: Selenium IDE, Webdriver and Selenium Grid.

    Let’s make an introduction respectively:

    Selenium IDE

    Selenium IDE is a complete integrated development environment for Selenium testing. It can directly record user operations in the browser, and can Playback, edit and debug test scripts. During debugging, you can step through the execution or adjust the execution speed, and view the log at the bottom for error information. The recorded test scripts can be exported in multiple languages, such as Java, C#, Python, Ruby, etc., making it easier for testers who master different languages ​​to operate. Webdriver

    Selenium RC When running JavaScript applications in a browser, there will be environmental sandbox issues, but WebDriver can jump out of the JavaScript sandbox and create more robust, distributed, and cross-platform applications for different browsers. Automated test scripts. Based on specific language (Java, C#, Python, Ruby, Perl, JavaScript, etc.) bindings to drive the browser to operate and validate web elements.

    How webdriver works:

    • After starting the browser, selenium-webdriver will bind the target browser to a specific port, and the started browser will act as webdriver's remote server.

    • The client (that is, the test script) uses ComandExecutor to send an HTTP request to the server (communication protocol: The WebDriver Wire Protocol. In the body of the HTTP request, the WebDriver Wire protocol will be used. A specified JSON-formatted string that tells Selenium what we want the browser to do next).

    • The Sever side needs to rely on native browser components and convert Web Service commands into browser native calls to complete the operation.

    selenium Grid

    selenium Grid is a server that provides a list of servers accessed by browser instances and manages the registration and status information of each node. Different test scripts can be executed on different servers at the same time.

    3 selenium IDE recording script

    Open Edge-plug-in-select selenium IDE:

    Create a new project, and there will be an Untitled test in the Test Case window on the left Case, right-click and rename it to "test"

    How to use selenium, the Python automated testing tool

    Click the recording button (little red dot) in the upper right part of the IDE to start manual recording

    In the address bar Enter the URL to be tested, such as http://www.baidu.com, search for keywords, and you can see that the IDE is recording.

    Right-click on the page to add checkpoints.

    After the recording is completed, click the recording button (little red dot) to end this manual recording. In selenium IDE, select a Test Case, right-click and select "Export as a test.py file.

    Run the script in python and debug it.

    # Generated by Selenium IDE
    import pytest
    import time
    import json
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.support import expected_conditions
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    class TestTest():
      def setup_method(self, method):
        self.driver = webdriver.Chrome()
        self.vars = {}
      def teardown_method(self, method):
        self.driver.quit()
      def test_test(self):
        self.driver.get("https://www.baidu.com/")
        self.driver.set_window_size(809, 864)
        self.driver.find_element(By.ID, "kw").click()
        self.driver.execute_script("window.scrollTo(0,0)")
        self.driver.find_element(By.ID, "kw").send_keys("四月是你的谎言")
        self.driver.find_element(By.ID, "su").click()
    Copy after login

    The above is the detailed content of How to use selenium, the Python automated testing tool. For more information, please follow other related articles on the PHP Chinese website!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    Java Tutorial
    1662
    14
    PHP Tutorial
    1262
    29
    C# Tutorial
    1235
    24
    PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

    PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

    Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

    PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

    PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

    PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

    Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

    Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

    How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

    To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

    Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

    Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

    Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

    VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

    How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

    Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

    See all articles