解决在 Selenium 4.1.0 log问题

在 Selenium 4.1.0 中,ChromeDriverService.Builder不再提供withOptions()方法来直接设置ChromeOptions。相反,你可以通过ChromeDriverService.Builder的其他方法来实现类似的效果。

以下是在 Selenium 4.1.0 中将 ChromeOptions 添加到 ChromeDriverService 的示例代码:

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

public class ChromeDriverExample {
    public static void main(String[] args) {
        // 创建 ChromeOptions 对象并进行配置
        ChromeOptions chromeOptions = new ChromeOptions();
        // 添加其他配置...

// 创建 ChromeDriverService.Builder 对象,并将 ChromeOptions 添加到其中 
ChromeDriverService.Builder serviceBuilder = new ChromeDriverService.Builder() .withLogOutput(System.out) .withVerbose(true) .withOptions(chromeOptions);
// 创建 ChromeDriverService ChromeDriverService service = serviceBuilder.build(); // 创建 ChromeDriver 对象,并将 ChromeOptions 和 ChromeDriverService 传递给它 ChromeDriver driver = new ChromeDriver(service, chromeOptions); // 执行你的 Chrome WebDriver 代码 // ... // 关闭 WebDriver driver.quit(); } }

 

在上述示例中,我们创建了一个 ChromeOptions 对象 chromeOptions 并对其进行配置。然后,我们创建了一个 ChromeDriverService.Builder 对象 serviceBuilder。在构建 ChromeDriverService 时,我们将 serviceBuilder 传递给 build() 方法。最后,我们创建了一个 ChromeDriver 对象 driver,并将 chromeOptionsservice 传递给它。

确保在代码中根据你的需求进行适当的配置和添加其他配置项。

package dev.selenium.browsers;

import com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.openqa.selenium.chromium.ChromiumDriverLogLevel;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.util.regex.Pattern;

public class ChromeTest {
    private ChromeDriver driver;
    private File logLocation;

    @AfterEach
    public void quit() {
        if (logLocation != null && logLocation.exists()) {
            logLocation.delete();
        }

        driver.quit();
    }

    @Test
    public void basicOptions() {
        ChromeOptions options = new ChromeOptions();
        driver = new ChromeDriver(options);
    }

    @Test
    public void arguments() {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized");
        driver = new ChromeDriver(options);
    }

    @Test
    public void excludeSwitches() {
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("excludeSwitches", ImmutableList.of("disable-popup-blocking"));

        driver = new ChromeDriver(options);
    }

    public void logsToFile() throws IOException {
        ChromeDriverService service = new ChromeDriverService.Builder()
                .withLogFile(getLogLocation())
                .build();

        driver = new ChromeDriver(service);

        String fileContent = new String(Files.readAllBytes(getLogLocation().toPath()));
        Assertions.assertTrue(fileContent.contains("Starting ChromeDriver"));
    }

    @Test
    public void logsToConsole() throws IOException {
        System.setOut(new PrintStream(getLogLocation()));

        ChromeDriverService service = new ChromeDriverService.Builder()
                .withLogOutput(System.out)
                .build();

        driver = new ChromeDriver(service);

        String fileContent = new String(Files.readAllBytes(getLogLocation().toPath()));
        Assertions.assertTrue(fileContent.contains("Starting ChromeDriver"));
    }

    @Test
    public void logsWithLevel() throws IOException {
        System.setProperty(ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY,
                getLogLocation().getAbsolutePath());

        ChromeDriverService service = new ChromeDriverService.Builder()
            .withLogLevel(ChromiumDriverLogLevel.DEBUG)
            .build();

        driver = new ChromeDriver(service);

        String fileContent = new String(Files.readAllBytes(getLogLocation().toPath()));
        Assertions.assertTrue(fileContent.contains("[DEBUG]:"));
    }

    @Test
    public void configureDriverLogs() throws IOException {
        System.setProperty(ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY,
                getLogLocation().getAbsolutePath());
        System.setProperty(ChromeDriverService.CHROME_DRIVER_LOG_LEVEL_PROPERTY,
                ChromiumDriverLogLevel.DEBUG.toString());

        ChromeDriverService service = new ChromeDriverService.Builder()
            .withAppendLog(true)
            .withReadableTimestamp(true)
            .build();

        driver = new ChromeDriver(service);

        String fileContent = new String(Files.readAllBytes(getLogLocation().toPath()));
        Pattern pattern = Pattern.compile("\\[\\d\\d-\\d\\d-\\d\\d\\d\\d", Pattern.CASE_INSENSITIVE);
        Assertions.assertTrue(pattern.matcher(fileContent).find());
    }

    @Test
    public void disableBuildChecks() throws IOException {
        System.setProperty(ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY,
                getLogLocation().getAbsolutePath());
        System.setProperty(ChromeDriverService.CHROME_DRIVER_LOG_LEVEL_PROPERTY,
                ChromiumDriverLogLevel.WARNING.toString());

        ChromeDriverService service = new ChromeDriverService.Builder()
                .withBuildCheckDisabled(true)
                .build();

        driver = new ChromeDriver(service);

        String fileContent = new String(Files.readAllBytes(getLogLocation().toPath()));
        String expected = "[WARNING]: You are using an unsupported command-line switch: --disable-build-check";
        Assertions.assertTrue(fileContent.contains(expected));
    }

     @Test
    public void extensionOptions() {
        ChromeOptions options = new ChromeOptions();
        Path path = Paths.get("src/test/resources/extensions/webextensions-selenium-example.crx");
        options.addExtensions(new File(path.toUri()));
        driver = new ChromeDriver(options);
        driver.get("https://www.selenium.dev/selenium/web/blank.html");
        WebElement injected = driver.findElement(By.id("webextensions-selenium-example"));
        Assertions.assertEquals("Content injected by webextensions-selenium-example", injected.getText());
    }

    private File getLogLocation() throws IOException {
        if (logLocation == null || !logLocation.exists()) {
            logLocation = File.createTempFile("chromedriver-", ".log");
        }

        return logLocation;
    }
}

 

posted @ 2023-07-03 16:31  锐洋智能  阅读(84)  评论(0编辑  收藏  举报