package com.zjty.autotest;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
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.Project;
import com.zjty.autotest.service.impl.SeleniumExecutor;
import com.zjty.autotest.util.WebDriverUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Sleeper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Slf4j
@SpringBootTest
class AutotestApplicationTests {

    @Autowired
    SeleniumExecutor seleniumExecutor;

    @Test
    public void execute() throws InterruptedException {
        List<Input> inputs = Lists.newArrayList();
        inputs.add(new Input(null, null, null, "caozuoyuan"));
        inputs.add(new Input(null, null, null, "caozuoyuan123"));

        String url = "http://192.168.1.244:8080/person/#/login";
        Project project = new Project(
                null,
                null,
                "firefox",
                url,
                inputs
        );
//        String url2 = "file:///C:/code/wps/index.html";
//        WebDriver driver = WebDriverUtil.getWebDriver("firefox");
//        driver.manage().timeouts().pageLoadTimeout(4L, TimeUnit.SECONDS);
//        driver.get(url);
//        Sleeper.SYSTEM_SLEEPER.sleep(Duration.ofSeconds(2));
//        String source1 = driver.getPageSource();
//        driver.get(url2);
//        driver.get(url);
//        Sleeper.SYSTEM_SLEEPER.sleep(Duration.ofSeconds(2));
//        String source2 = driver.getPageSource();
//        System.out.println(Objects.equals(source1,source2));
        Report report = seleniumExecutor.execute(project);
        for (Measure measure : report.getMeasures()) {
            System.out.println(measure);
        }

//        Document document = Jsoup.parse(driver.getPageSource());
//        List<Element> elements = document.getAllElements().stream()
//                .filter(element -> element.childrenSize() == 0)
//                .collect(Collectors.toList());
//        for (Element element : elements) {
//            System.out.println(element.attributes());
//        }

    }
}
