使用 Selenium IDE 生成 Java 自动化测试代码

  1. 首先使用 Selenium IDE 录制操作。

  2. 导出为 Java JUnit 测试文件。

  3. 创建 Maven 项目,并编辑 pom.xml 添加依赖:

    我使用的 Selenium IDE 版本为 3.17.2,其对应的 JUnit 版本为 4.x.x

    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <packaging>jar</packaging>
        <version>1.0-SNAPSHOT</version>
        <name>demo</name>
        <url>http://maven.apache.org</url>
    
        <dependencies>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13.2</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-java</artifactId>
                <version>4.27.0</version>
            </dependency>
    
            <dependency>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest</artifactId>
                <version>3.0</version>
                <scope>test</scope>
            </dependency>
    
        </dependencies>
    </project>
    
  4. 将生成的 JUnit 测试文件放到项目的测试目录。

  5. 运行测试:

    mvn test
    

参见:WebDriver | Selenium

posted @ 2025-01-03 22:17  Undefined443  阅读(7)  评论(0编辑  收藏  举报