
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.chrome.ChromeOptions;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
import java.util.List;
//import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

public class Chromedriver {

    WebDriver driver;

    public static void main(String[] args) {
        Chromedriver chromedriver = new Chromedriver();
        chromedriver.login();

//        1）Class选择：driver.findElement(By.className("s_ipt"));
//        WebElement element=driver.findElement(By.className("s_btn"));
//        System.out.println(element.getAttribute("value"));
//
//        2）ID选择：   driver.findElement(By.id("kw"));
//        WebElement element=driver.findElement(By.id("kw"));
//        System.out.println(element.getAttribute("class"));
//
//        3）name选择： driver.findElement(By.name("wd"));
//        WebElement element=driver.findElement(By.name("wd"));
//        System.out.println(element.getAttribute("maxlength"));
//
//        4）tag选择：  driver.findElements(By.tagName("input"));
//        List<WebElement> elements = driver.findElements(By.tagName("a"));
//        for (WebElement element:elements){
//            System.out.println(element.getText().trim());
//        }
//        5）link选择： driver.findElement(By.linkText("地图"));
//        WebElement elemen = driver.findElement(By.linkText("网盘"));
//        System.out.println(elemen.getText());
//        6）Partial link选择（a标签文本内容模糊匹配）：driver.findElement(By.partialLinkText("使用百"));
//        List<WebElement> elements = driver.findElements(By.partialLinkText("人"));
//        for (WebElement element:elements){
//            System.out.println(element.getText());
//        }
//        7）css选择器：driver.findElement(By.cssSelector("#kw"));
//        List<WebElement> elements = driver.findElements(By.cssSelector("#hotsearch-content-wrapper > li:nth-child(1)"));
//        for(WebElement element:elements){
//            System.out.println(element.getText());
//        }
//        8）xpath选择：driver.findElement(By.xpath("//*[@id=\"kw\"]"));
//        WebElement element = driver.findElement(By.xpath("//*[@id=\"kw\"]"));
//        System.out.println(element.getAttribute("value"));
//
//        driver.findElement(By.id("kw")).sendKeys("java");
//        driver.findElement(By.id("su")).click();
//    }
    }

    @Test
    public void login()
    {
        System.out.println("===========login");
        //设置驱动
        System.setProperty("webdriver.chrome.driver", "driver/chromedriver");
        //在当前浏览器打开
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("debuggerAddress", "127.0.0.1:9222");

        //创建驱动
        driver = new ChromeDriver(options);
        //与将要测试的网站建立连接
        driver.get("https://dev-h5.smart-gani.com/#/login");

//        Assert.assertNotNull(driver,"驱动创建失败！");

        // 强转js执行对象
        JavascriptExecutor javaScript = (JavascriptExecutor) driver;

        List<WebElement> inputs = driver.findElements(By.className("van-field__control"));
        String[] infos = new String[]{"13366660001","123456"};
        for (int i = 0; i < inputs.size(); i++) {
            WebElement input = inputs.get(i);
            javaScript.executeScript("arguments[0].value = '';", input);
            input.sendKeys(infos[i]);
        }
        WebElement loginBtn = driver.findElement(By.cssSelector("button[type=submit]"));
//        Assert.assertNotNull(loginBtn,"登录按钮获取失败");
        loginBtn.click();

        System.out.println("===========选择角色");

//        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10), Duration.ofSeconds(1));
//        WebElement cell = wait.until(new ExpectedCondition<WebElement>() {
//            public WebElement apply(WebDriver d) {
//                return d.findElement(By.cssSelector("div[class='pation blue']:nth-child(1)"));
//            }
//        });

//        Assert.assertNotNull(cell);

//        cell.click();

//        System.out.println(cell.getAttribute("outerHTML"));
    }

    @AfterTest
    public void afterLogin() {
        System.out.println("===========afterLogin");
        testShare();
    }

    public void testShare() {

//        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(1));

        WebElement element = driver.findElement(By.cssSelector("div[class='con-items']"));
//        Assert.assertNotNull(element,"元素获取失败");
        element.click();
    }
}
