package com.zjty.autotest;

import com.google.common.collect.Lists;
import com.zjty.autotest.pojo.report.Measure;
import com.zjty.autotest.pojo.report.Report;
import com.zjty.autotest.pojo.test.Input;
import com.zjty.autotest.pojo.test.JavaScriptError;
import com.zjty.autotest.pojo.test.Project;
import com.zjty.autotest.service.impl.SeleniumExecutor;
import com.zjty.autotest.util.WebDriverUtil;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.logging.Logs;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.support.ui.Sleeper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

@Slf4j
@SpringBootTest
class AutotestApplicationTests {

    @Autowired
    SeleniumExecutor seleniumExecutor;

    @Test
    public void execute() throws InterruptedException {
        String url = "file:///C:/code/errorTest.html";
//        String url = "https://workbook.zjtys.com.cn/#/login";
        test1(url);



    }

    private void test1(String url){
        // GeckoDriver v0.26.0
        FirefoxOptions options = new FirefoxOptions();
        LoggingPreferences logPrefs = new LoggingPreferences();
        logPrefs.enable(LogType.BROWSER, Level.ALL);
//        options.setPageLoadStrategy(PageLoadStrategy.EAGER);
        options.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);

        WebDriver driver = new FirefoxDriver(options);
        driver.get(url);
        driver.findElement(By.xpath("/html/body/button[1]")).click();
        driver.findElement(By.xpath("/html/body/button[2]")).click();
        driver.findElement(By.xpath("/html/body/button[3]")).click();
        driver.findElement(By.xpath("/html/body/button[4]")).click();
        driver.findElement(By.xpath("/html/body/button[5]")).click();
        for (LogEntry logEntry : driver.manage().logs().get(LogType.BROWSER)) {
            System.out.println(String.format("[%s] [%s]", logEntry.getLevel(), logEntry.getMessage()));
        }
    }

    private void test2(String url) {
        List<Input> inputs = Lists.newArrayList();
        inputs.add(new Input(null, null, null, "caozuoyuan123"));
        inputs.add(new Input(null, null, null, "caozuoyuan"));

        Project project = new Project(
                null,
                null,
                "firefox",
                url,
                inputs
        );
        Report report = seleniumExecutor.execute(project);
        for (Measure measure : report.getMeasures()) {
            System.out.println(measure);
        }
    }



}
