Web自动化测试工具-selenium

mtain 2023年07月28日 133次浏览

说明

常见的Web自动化测试工具:selenium,playwright,htmlunit

selenium安装使用

以Java语言为例

1. 浏览器驱动下载

Edge浏览器
https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
下载文件:edgedriver_win64.zip,解压

2. Java项目增加Maven依赖

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.1.4</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>31.0.1-jre</version>
        </dependency>

3. 代码

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;

public class TestMain {
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.edge.whitelistedIps", "");
        System.setProperty("webdriver.edge.driver","D:/tools/msedgedriver.exe");
        EdgeOptions edgeOptions = new EdgeOptions();
        edgeOptions.addArguments("--remote-allow-origins=*");//解决 403 出错问题

        WebDriver driver = new EdgeDriver(edgeOptions);
        driver.get("https://www.baidu.com/");
        driver.findElement(By.id("kw")).sendKeys("selenium");
        driver.findElement(By.id("su")).click();


        Thread.sleep(10000);

        driver.quit();

    }
}