diff --git a/src/main/java/com/bonus/autoweb/base/AutoMain.java b/src/main/java/com/bonus/autoweb/base/AutoMain.java deleted file mode 100644 index 77ea779..0000000 --- a/src/main/java/com/bonus/autoweb/base/AutoMain.java +++ /dev/null @@ -1,392 +0,0 @@ -package com.bonus.autoweb.base; - -import org.openqa.selenium.By; -import org.openqa.selenium.JavascriptExecutor; -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.interactions.Actions; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.concurrent.TimeUnit; - - -/** - * 自动化操作 - * - * @author 吕继龙 - */ -public class AutoMain { - - Logger log = LoggerFactory.getLogger(AutoMain.class); - //浏览器驱动对象 - private WebDriver webDriver; - - public WebDriver getWebDriver() { - return webDriver; - } - - /** - * 初始化驱动 - */ - public void initDrive(int type) { - // chromedriver服务地址 - System.setProperty(DataConfig.DRIVE_NAME, DataConfig.drivePath); - ChromeOptions option = new ChromeOptions(); - //添加浏览器地址 个人本地-同砀山 - option.setBinary("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"); - - option.addArguments("--start-maximized"); - option.addArguments("--force-device-scale-factor=0.75"); - option.addArguments("disable-infobars"); -// if(type != 7) { -// //隐藏浏览器 -// option.addArguments("--headless"); -// } - webDriver = new ChromeDriver(option); - } - - /** - * 开始工作 - */ - public void startAuto(String userName, String pass) throws Exception { - openWeb(); - login(userName, pass); - openDuty(); - } - public void startYuJingAuto(String userName, String pass) throws Exception { - openWeb(); - log.info("打开网站成功"); - login(userName, pass); - log.info("登录成功"); - openYuJing(); - } - public void startYuJingActionAuto(String userName, String pass) throws Exception { - openWeb(); - log.info("打开网站成功"); - login(userName, pass); - log.info("登录成功"); - openYuJingAction(); - } - public void startCaoLianAuto(String userName, String pass) throws Exception { - openWeb(); - login(userName, pass); - openCheckTheDrills(); - } - - /** - * 打开网站 - */ - public void openWeb() throws InterruptedException { - //第一步打开指定网站 - webDriver.get(DataConfig.URL); - log.info("打开宿州应急指挥中心网站------------"); - Thread.sleep(2000); - } - - /** - * 打开网站并登录系统 - */ - private void login(String userName, String pass) throws Exception { - //****************************************************************** - /** - * 第二步 用户登录 - */ - log.info("使用账号:" + userName + ",进行登录---------"); - //输入用户名 - webDriver.findElement(By.id("username")).sendKeys(userName); - log.info("输入用户名------------"); - Thread.sleep(300); - - //输入密码 - webDriver.findElement(By.id("password")).sendKeys(pass); - log.info("输入密码------------"); - Thread.sleep(300); - - //系统登录 - webDriver.findElement(By.id("submi")).click(); - log.info("系统登录------------"); - Thread.sleep(800); - isAlert(); - } - - - /** - * 判断是否有弹框,隐藏弹框 - */ - private void isAlert() { - try { - WebElement alert = webDriver.findElement(By.xpath("/html/body/div[5]")); - JavascriptExecutor js = (JavascriptExecutor) webDriver; - js.executeScript("arguments[0].setAttribute(arguments[1],arguments[2])", alert, "style", "display:none"); - Thread.sleep(3000); - - WebElement alert1 = webDriver.findElement(By.xpath("/html/body/div[2]")); - JavascriptExecutor js1 = (JavascriptExecutor) webDriver; - js1.executeScript("arguments[0].setAttribute(arguments[1],arguments[2])", alert1, "style", "display:none"); - Thread.sleep(3000); - }catch (Exception e){ - log.error("隐藏出错或者没有弹窗"); - } - - } - - /** - * 打开值班管理页面,并定位到值班管理的iframe - */ - private void openDuty() throws InterruptedException { - Thread.sleep(5000); -// -// try { -// WebElement alertDiv = webDriver.findElement(By.xpath("/html/body/div[2]/div/div[1]/div/div[1]/div[1]/span")); -// if(alertDiv.getText()) { -// -// } -// }catch (Exception e){ -// -// } - - try { - webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); - } catch (Exception e) { - e.printStackTrace(); - } - Thread.sleep(5000); - //执行鼠标悬停动作-管理 - Actions action = new Actions(webDriver); - WebElement wegl = webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div[1]/div[6]/div/div/div[6]/span/span" + - "/div")); - action.moveToElement(wegl).build().perform(); - Thread.sleep(300); - - //获取管理标签中的aria-describedby属性 - String attributeId = wegl.getAttribute("aria-describedby"); - log.info("管理标签的ID值为:" + attributeId); - Thread.sleep(300); - - - //执行鼠标悬停动作-常态值班 - String ctzbXpath = "//*[@id=\"" + attributeId + "\"]/div/div[1]/div[2]/div[4]"; - WebElement wectzb = webDriver.findElement(By.xpath(ctzbXpath)); - action.moveToElement(wectzb).build().perform(); - Thread.sleep(300); - - - //打开值班页面 - //*[@id=\"" + attributeId + "\"]/div/div[2]/div[2]/div[1]/div[2] - String zbglXpath = "//*[@id=\"" + attributeId + "\"]/div/div[2]/div[2]/div/div[1]"; - webDriver.findElement(By.xpath(zbglXpath)).click(); - log.info("打开值班管理页面------------"); - Thread.sleep(800); - - //定位值班管理iframe的标签 - WebElement dutyIframe = webDriver.findElement(By.id("zbgln")); - webDriver.switchTo().frame(dutyIframe); - log.info("定位到值班管理iframe-----------"); - Thread.sleep(3000); - - //将日期改为今天的日期 - webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/section/main/div/div[1]/div[1]/div[3]/div[1]/div[2]/div/button[2]")).click(); - log.info("将日期选择回当日----------"); - Thread.sleep(300); - } - - private void openYuJing() throws InterruptedException{ - try { - webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); - } catch (Exception e) { - e.printStackTrace(); - } - Thread.sleep(5000); - //执行鼠标悬停动作-管理 - Actions action = new Actions(webDriver); - WebElement wegl = webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div[1]/div[6]/div/div/div[2]/span/span" + - "/div")); - action.moveToElement(wegl).build().perform(); - Thread.sleep(300); - log.info("执行鼠标悬停动作-管理"); - //获取管理标签中的aria-describedby属性 el-popover-7909 - String attributeId = wegl.getAttribute("aria-describedby"); - log.info("管理标签的ID值为:" + attributeId); - Thread.sleep(300); - - -// //执行鼠标悬停动作-常态值班 -// String ctzbXpath = "//*[@id=\"" + attributeId + "\"]/div/div[1]/div[2]/div[3]"; -// WebElement wectzb = webDriver.findElement(By.xpath(ctzbXpath)); -// action.moveToElement(wectzb).build().perform(); -// Thread.sleep(300); - - - //打开预警响应管理页面 - //*[@id=\"" + attributeId + "\"]/div/div[2]/div[2]/div[1]/div[2] - String zbglXpath = "//*[@id=\"" + attributeId + "\"]/div/div/div[2]/div[3]"; - webDriver.findElement(By.xpath(zbglXpath)).click(); - log.info("打开预警页面------------"); - Thread.sleep(800); - - //定位值班管理iframe的标签 - WebElement dutyIframe = webDriver.findElement(By.id("warningResTicket")); - webDriver.switchTo().frame(dutyIframe); - log.info("定位到预警响应管理iframe-----------"); - Thread.sleep(500); - } - private void openYuJingAction() throws InterruptedException{ - try { - webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); - } catch (Exception e) { - e.printStackTrace(); - } - Thread.sleep(5000); - //执行鼠标悬停动作-管理 - Actions action = new Actions(webDriver); - WebElement wegl = webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div[1]/div[6]/div/div/div[2]/span/span" + - "/div")); - action.moveToElement(wegl).build().perform(); - Thread.sleep(300); - log.info("执行鼠标悬停动作-管理"); - //获取管理标签中的aria-describedby属性 el-popover-7909 - String attributeId = wegl.getAttribute("aria-describedby"); - log.info("管理标签的ID值为:" + attributeId); - Thread.sleep(300); - - -// //执行鼠标悬停动作-预警行动 -// String ctzbXpath = "//*[@id=\"" + attributeId + "\"]/div/div[1]/div[2]/div[4]"; -// WebElement wectzb = webDriver.findElement(By.xpath(ctzbXpath)); -// action.moveToElement(wectzb).build().perform(); -// Thread.sleep(300); - - - //打开预警响应管理页面 - //*[@id=\"" + attributeId + "\"]/div/div[2]/div[2]/div[1]/div[2] - String zbglXpath = "//*[@id=\"" + attributeId + "\"]/div/div/div[2]/div[4]"; - webDriver.findElement(By.xpath(zbglXpath)).click(); - log.info("打开预警页面------------"); - Thread.sleep(800); - - //定位值班管理iframe的标签 - WebElement dutyIframe = webDriver.findElement(By.id("yjczrwgl")); - webDriver.switchTo().frame(dutyIframe); - log.info("定位到预警响应处置任务管理iframe-----------"); - Thread.sleep(500); - } - - /** - * 打开检查操练管理页面,并定位到日常操练的iframe - */ - private void openCheckTheDrills() throws InterruptedException { - try { - webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); - } catch (Exception e) { - e.printStackTrace(); - } - Thread.sleep(3000); - //执行鼠标悬停动作-管理 - Actions action = new Actions(webDriver); - WebElement wegl = webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div[1]/div[6]/div/div/div[6]/span/span" + - "/div")); - action.moveToElement(wegl).build().perform(); - Thread.sleep(300); - - //获取管理标签中的aria-describedby属性 - String attributeId = wegl.getAttribute("aria-describedby"); - log.info("管理标签的ID值为:" + attributeId); - Thread.sleep(300); - - //执行鼠标悬停动作-检查操练管理 - String ctzbXpath = "//*[@id=\"" + attributeId + "\"]/div/div[1]/div[2]/div[5]"; - webDriver.findElement(By.xpath(ctzbXpath)).click(); -// WebElement wectzb = webDriver.findElement(By.xpath(ctzbXpath)); -// action.moveToElement(wectzb).build().perform(); - Thread.sleep(300); - - //打开日常操练页面 - //*[@id=\"" + attributeId + "\"]/div/div[2]/div[2]/div[1]/div[2] -// String zbglXpath = "//*[@id=\"" + attributeId + "\"]/div/div[2]/div[2]/div/div[1]"; -// webDriver.findElement(By.xpath(zbglXpath)).click(); -// log.info("打开日常操练页面------------"); -// /* TODO 800 --> 1500 */ - Thread.sleep(1500); - - //定位日常操练iframe的标签 - WebElement dutyIframe = webDriver.findElement(By.id("rccl")); - webDriver.switchTo().frame(dutyIframe); - log.info("定位到日常操练iframe-----------"); - Thread.sleep(8000); - - //点击抽查按钮 - webDriver.findElement(By.xpath("/html/body/div/section/div[1]/div[1]/div/div/div/div[3]")).click(); - - //定位日常操练iframe的标签 - WebElement insideIframe = webDriver.findElement(By.xpath("/html/body/div/section/div[1]/div[2]/div[1]/iframe")); - webDriver.switchTo().frame(insideIframe); - log.info("定位到日常操练内部iframe-----------"); - Thread.sleep(3000); - } - - /** - * 关闭浏览器驱动 - */ - public void closeDrive() { - log.info("工作完成,退出浏览器"); - webDriver.quit(); - } - - public void startAddExercisePlan(String userName, String pass) throws Exception { - openWeb(); - login(userName, pass); - startAddExercisePlanAuto(); - } - - private void startAddExercisePlanAuto() throws InterruptedException { - try { - webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); - } catch (Exception e) { - e.printStackTrace(); - } - Thread.sleep(5000); - //执行鼠标悬停动作-管理 - Actions action = new Actions(webDriver); - WebElement wegl = webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div[1]/div[6]/div/div/div[6]/span/span" + - "/div")); - action.moveToElement(wegl).build().perform(); - Thread.sleep(300); - - //获取管理标签中的aria-describedby属性 - String attributeId = wegl.getAttribute("aria-describedby"); - log.info("管理标签的ID值为:" + attributeId); - Thread.sleep(300); - - //执行鼠标悬停动作-检查操练管理 - String ctzbXpath = "//*[@id=\"" + attributeId + "\"]/div/div[1]/div[2]/div[5]"; - webDriver.findElement(By.xpath(ctzbXpath)).click(); -// WebElement wectzb = -// action.moveToElement(wectzb).build().perform(); - Thread.sleep(300); - -// //打开检查计划页面 -// //*[@id=\"" + attributeId + "\"]/div/div[2]/div[2]/div[1]/div[2] -// String zbglXpath = "//*[@id=\"" + attributeId + "\"]/div/div[2]/div[2]/div/div[1]"; -// webDriver.findElement(By.xpath(zbglXpath)).click(); -// log.info("打开检查计划页面------------"); -// /* TODO 800 --> 1500 */ - Thread.sleep(1500); - - //定位检查计划iframe的标签 - WebElement dutyIframe = webDriver.findElement(By.id("rccl")); - webDriver.switchTo().frame(dutyIframe); - log.info("定位到检查计划iframe-----------"); - Thread.sleep(500); - //点击抽查按钮 - webDriver.findElement(By.xpath("/html/body/div/section/div[1]/div[1]/div/div/div/div[3]")).click(); - -// //定位日常操练iframe的标签 -// WebElement insideIframe = webDriver.findElement(By.xpath("/html/body/div/section/div[1]/div[2]/div[1]/iframe")); -// webDriver.switchTo().frame(insideIframe); -// log.info("定位到日常操练内部iframe-----------"); -// Thread.sleep(3000); - } -}