Home  >  Article  >  Backend Development  >  How to use selenium, the Python automated testing tool

How to use selenium, the Python automated testing tool

WBOY
WBOYforward
2023-05-17 10:43:371320browse

    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()

    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:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete