package com.zjty.autotest;

import com.google.common.collect.Lists;
import com.zjty.autotest.common.action.Browser;
import com.zjty.autotest.pojo.report.Measure;
import com.zjty.autotest.pojo.report.Report;
import com.zjty.autotest.pojo.test.ElementFeature;
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.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.support.ui.Sleeper;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.Duration;
import java.util.List;
import java.util.Objects;

@Slf4j
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class AutotestApplicationTests {

    @Autowired
    SeleniumExecutor seleniumExecutor;

    @Test
    public void execute() throws InterruptedException {
        test3();
    }

    private void test1() {
        System.out.println("等待30秒...");
        try {
            Sleeper.SYSTEM_SLEEPER.sleep(Duration.ofSeconds(30));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        seleniumExecutor.quit();
        System.out.println("关闭测试...");
    }

    private void test2() {
        String url1 = "file:///C:/Users/C/Documents/WeChat%20Files/c18042003295/FileStorage/File/2020-03/errorFile/error7.html";
        String url2 = "https://www.google.com";
        String url3 = "http://www.zjtys.com.cn/zjty/view/home/index.html";
        WebDriver driver = new ChromeDriver();
        driver.get(url1);
        try {
            List<LogEntry> entries = driver.manage().logs().get(LogType.BROWSER).getAll();
            for (LogEntry entry : entries) {
                System.out.println(entry.getMessage());
            }
        } catch (Exception e) {
            log.error("出现异常：" + e);
        }
        driver.quit();
    }

    private void test3() {
        String url1 = "file:///C:/Users/C/Documents/WeChat%20Files/c18042003295/FileStorage/File/2020-03/errorFile/error1.html";
        String url2 = "https://workbook.zjtys.com.cn/#/login";
        String url3 = "http://www.zjtys.com.cn/zjty/view/home/index.html";
        String url4 = "https://bbs.colg.cn/forum.php";
        String url5 = "https://www.google.com/";
        String url6 = "https://mail.qq.com/";
        String url7 = "https://git.yfzx.zjtys.com.cn/users/sign_in";

        List<Input> inputs = Lists.newArrayList();
        inputs.add(new Input(null, null, null, "王永磊"));
        inputs.add(new Input(null, null, null, "qwer1234"));
        Project project = new Project(
                null,
                null,
                Browser.CHROME,
                url1,
                false,
                inputs
        );
    }

    private ElementFeature toFeature(WebElement element) {
        return new ElementFeature(
                element.getTagName(),
                element.getText(),
                element.getRect()
        );
    }


}
