package com.zjty.automatedtesting.util;

import org.openqa.selenium.*;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class JavaEncapsulationUtil {

    public static String getCurrentSystemTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        return dateFormat.format(new Date());
    }

    public static void errorScreenshot(WebDriver driver, String path, String pictureFormat) throws IOException {
        String systemTime = JavaEncapsulationUtil.getCurrentSystemTime();
        String pictureName = systemTime + pictureFormat;
        File jpg = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    }

    public static WebElement elementException(WebDriver driver, By by, String elementName, String className) throws Exception {
        try {
            WebElement element = driver.findElement(by);
            element.isDisplayed();
            System.out.println(elementName + "元素存在");
            return element;
        } catch (Exception e) {
            System.out.println(elementName + "元素不存在,浏览器关闭，请查看截图并查找原因");
            throw (e);
        }
    }
}
