V01
package common; import java.awt.AWTException; import java.awt.Robot; import java.awt.event.KeyEvent; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Random; import java.util.Set; import java.util.regex.Pattern; import org.apache.log4j.Logger; import org.apache.poi.poifs.crypt.dsig.KeyInfoKeySelector; import org.apache.tika.metadata.WordPerfect; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.Cookie; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.interactions.SendKeysAction; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.Select; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import pojo.Page; import pojo.UIElement; import utils.BrowserUtils; import utils.RegexUtils; import utils.XmlUtils; public class BasePage { private final static Logger logger = Logger.getLogger(BasePage.class); /** * 获取浏览器 * @return 浏览器 */ public static WebDriver getBroswer() { return BrowserUtils.getDriver(); } /** * 获取JavascriptExecutor * @return JavascriptExecutor */ public static JavascriptExecutor getJs() { return BrowserUtils.getJavascriptExecutor(); } /** * 获取WebDriverWait * @return WebDriverWait */ public static WebDriverWait getWait() { return BrowserUtils.getWebDriverWait(); } /** * 创建本地浏览器或者远程浏览器 * @param type type→本地/远程 * @param browserType 浏览器类型 */ public static void createBroswer(String type,String browserType) { logger.info("================================打开"+type+"浏览器("+browserType+") ========================================"); if (type.contains("本地")) { BrowserUtils.openBrowser(browserType);; }else if (type.contains("远程")) { BrowserUtils.openRemoteBrowser(browserType); } } /** * 创建本地浏览器 * @param browserType 浏览器类型 */ public static void createBrowser(String browserType) { logger.info("================================打开远程端浏览器("+browserType+") ========================================"); BrowserUtils.openBrowser(browserType); } /** * 创建远程浏览器 * @param browserType 浏览器类型 */ public static void createRemoteBrowser(String browserType) { logger.info("================================打开本地浏览器("+browserType+") ========================================"); BrowserUtils.openRemoteBrowser(browserType); } /** * 清楚缓存 */ public static void ClearCache () { logger.info("================================开始清除缓存 ========================================"); Set<Cookie> coo = getBroswer().manage().getCookies(); //打印Cookie logger.info("cookies信息:【"+coo.toString()+"】"); //清除所有的缓存 getBroswer().manage().deleteAllCookies(); } /** * 跳转到指定的url * @param url 需跳转的url地址 */ public static void getUrl(String url) { logger.info("浏览器跳转到:【"+url+"】"); getBroswer().get(url); } /** * 硬性等待 * @param time 毫秒 */ public static void waitTime(int time) { try { logger.info("线程等待:【"+(double)time/1000+"】秒"); Thread.sleep(time); } catch (InterruptedException e) { e.printStackTrace(); } } /** * 最大化窗口 */ public static void maxWindow() { logger.info("window窗口最大化"); getBroswer().manage().window().maximize(); } /** * 根据获取的标题来切换句柄 获取的标题包含预期的标题才能切换 * @param title 标题 */ public void switchWindowByTitle(String title) { Set<String> handles = getBroswer().getWindowHandles(); logger.info("获取【handles大小】为:【"+handles.size()+"】,【获取的handles】为:【"+handles.toString()+"】"); for (String handle : handles) { if (getBroswer().getTitle().contains(title)) { logger.info("当前句柄的【标题】:【"+getBroswer().getTitle()+"】,符合预期的标题,切换成功"); break; }else { logger.info("当前句柄的【标题】:【"+getBroswer().getTitle()+"】,切换句柄,寻找符合标题的句柄"); getBroswer().switchTo().window(handle); } } } //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== public static void main(String[] args) { createBrowser( "chrome"); maxWindow(); getBroswer().manage().deleteAllCookies(); getBroswer().get("http://egis-cspi-padis-dmzstg1.pingan.com.cn/cspi-internet/palogin.jsp"); input(By.cssSelector("#userName"), "01713801151301"); input(By.cssSelector("#passwordInput"), "a8888888"); try { Thread.sleep(25000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // if (assertEleContains(By.cssSelector("#center"), "没有找到相应信息")) { // getBroswer().navigate().refresh(); // waitTime(2000); // getBroswer().switchTo().alert().accept(); // waitTime(2000); // input(By.cssSelector("#passwordInput"), "a8888888"); // try { // Thread.sleep(25000); // } catch (InterruptedException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } getBroswer().switchTo().defaultContent(); try { getBroswer().switchTo().frame(getBroswer().findElement(By.cssSelector("#EM_PAGE"))); }catch(Exception e) { if (assertEleContains(By.cssSelector("#center"), "没有找到相应信息")) { getBroswer().navigate().refresh(); waitTime(2000); boolean s ; try { s = assertEleContains(By.cssSelector("#warmTips"), "温馨提示"); } catch (Exception e2) { s = false; } if (s){ getBroswer().switchTo().frame(getBroswer().findElement(By.cssSelector("#EM_PAGE"))); getBroswer().switchTo().frame(getBroswer().findElement(By.cssSelector("#content"))); }else { getBroswer().switchTo().alert().accept(); waitTime(2000); input(By.cssSelector("#passwordInput"), "a8888888"); try { Thread.sleep(25000); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } getBroswer().switchTo().frame(getBroswer().findElement(By.cssSelector("#EM_PAGE"))); getBroswer().switchTo().frame(getBroswer().findElement(By.cssSelector("#content"))); } } /** * 点击提示关闭按钮 */ waitVisibilityOfElementLocated(By.cssSelector("#warmTips #closeWarmTips")); clickByJs(getBroswer().findElement(By.cssSelector("#warmTips #closeWarmTips"))); /** * 通过键盘按钮点击回车键 */ try { Thread.sleep(6000); Robot robot = new Robot(); robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); robot.delay(500); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } /** * 点击alter弹框 */ // getBroswer().switchTo().alert().accept(); /** * 点击提示关闭按钮 */ clickByJs( waitVisibilityOfElementLocated(By.cssSelector("#warmTips #closeWarmTips"))); getBroswer().switchTo().defaultContent(); getBroswer().switchTo().frame(getBroswer().findElement(By.cssSelector("#EM_PAGE"))); /** * 点击悬浮保全处理查询 */ new Actions(getBroswer()) .moveToElement( waitVisibilityOfElementLocated(By.xpath("//a[contains(text(),'保全处理查询')]"))).perform(); /** * 点击余额管理 */ clickByJs(waitVisibilityOfElementLocated(By.xpath("//a[contains(text(),'余额管理')]"))); getBroswer().switchTo().frame(getBroswer().findElement(By.cssSelector("#content"))); /** * 保单号 */ String policyNo = "GP02002024242842"; /** * 输入保单号 */ waitVisibilityOfElementLocated(By.cssSelector("#queryPolNoT")).sendKeys(policyNo); /** * 点击查询 */ waitElementToBeClickable(By.cssSelector("[onclick*='queryPolInfoByPolNo()']")).click(); String beforeText = waitVisibilityOfElementLocated(By.xpath("//th[contains(text(),'保单号')]/parent::tr/following-sibling::tr")).getText(); String beforeMOney = RegexUtils.regexSingleMatch("(?<=\\s)\\d+\\.\\d+(?=\\s)", beforeText); Double beforeMOney1 = Double.parseDouble(beforeMOney); System.out.println(beforeText+" "+beforeMOney1); /** * 验证查询后数据是否正确 assertPolInfoLink * 是否包含查询的保单号 */ boolean assertContains = assertRowContains(By.xpath("//th[contains(text(),'保单号')]/parent::tr/following-sibling::tr"),policyNo); /** * 查询出来的保单号是否唯一 */ System.out.println("保单查询后是否包含:【"+assertContains+"】"); boolean assertInfoCountOne = assertInfoCountOne(By.xpath("//th[contains(text(),'保单号')]/parent::tr/following-sibling::tr") ); System.out.println("保单查询后是否唯一:【"+assertInfoCountOne+"】"); /** * 点击保单链接 */ waitElementToBeClickable(By.cssSelector("a[onclick*=displayDetail]")).click(); /** * 验证保单链接功能 * 包含 团体保单详情页 * 保单信息 * 投保人信息 * 保障信息 * 层级信息 */ boolean assertEleContains2 = assertEleContains(By.cssSelector("#dialogContent.process-content "), "block", "style"); System.out.println("是否包含属性值:"+assertEleContains2); boolean assertEleContains = assertEleContains(By.cssSelector("#dialogContent.process-content "),"团体保单详情页" ); System.out.println("元素文本值是否包含:"+assertEleContains); assertEleContains = assertEleContains(By.cssSelector("#dialogContent.process-content "),"保单信息" ); System.out.println("元素文本值是否包含:"+assertEleContains); assertEleContains = assertEleContains(By.cssSelector("#dialogContent.process-content "),"投保人信息" ); System.out.println("元素文本值是否包含:"+assertEleContains); assertEleContains = assertEleContains(By.cssSelector("#dialogContent.process-content "),"保障信息" ); System.out.println("元素文本值是否包含:"+assertEleContains); assertEleContains = assertEleContains(By.cssSelector("#dialogContent.process-content "),"层级信息" ); System.out.println("元素文本值是否包含:"+assertEleContains); /** * 滑动到层级信息 */ scrollIntoTopView(waitPresenceOfElementLocated(By.xpath("//*[contains(text(),'层级信息')]"))); waitTime(5000); System.out.println(" 已经滑懂了"); // scrollInTobottomView(waitPresenceOfElementLocated(By.xpath("//*[contains(text(),'层级信息')]"))); // waitTime(5000); /** * 点击职业链接 */ waitElementToBeClickable(By.cssSelector("[onclick*='showProfCode']")).click(); /** * 点击职业层级链接后显示的层级详情 */ assertEleContains2 = assertEleContains(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]"), "", "style"); System.out.println("是否包含属性值:"+assertEleContains2); assertEleContains2 = assertEleContains(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]"), "职业描述"); System.out.println("是否包含文本值:"+assertEleContains2); assertEleContains2 = assertEleContains(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]"), "职业代码"); System.out.println("是否包含文本值:"+assertEleContains2); assertEleContains2 = assertInfoCountBigOne(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]//td[2][not(contains(text(),'职业'))]/parent::tr")); System.out.println("元素是否大于等于1个元素:"+assertEleContains2); /** * 点击层级明细链接 * showDetailNew */ waitElementToBeClickable(By.cssSelector("[onclick*='showDetailNew']")).click(); assertEleContains2 = assertEleContains(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]"), "", "style"); System.out.println("是否包含属性值:"+assertEleContains2); assertEleContains2 = assertEleContains(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]"), "保险金额"); System.out.println("是否包含文本值:"+assertEleContains2); assertEleContains2 = assertEleContains(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]"), "赔付比例"); System.out.println("是否包含文本值:"+assertEleContains2); assertEleContains2 = assertInfoCountBigOne(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]//td[2][not(contains(text(),'保险'))]/parent::tr")); System.out.println("元素是否大于等于1个元素:"+assertEleContains2); /** * 点击关闭保单详情按钮 */ clickByJs(waitElementToBeClickable(By.cssSelector("[onclick*='closetxt']"))); /** * 点击转出按钮 */ clickByJs(waitElementToBeClickable(By.cssSelector("[onclick*='queryFundAmount']"))); /** * 验证传出按钮是否跳余额转出页面 */ assertEleContains2 = assertEleContains(By.cssSelector("div#returnsDiv"), "单位对公账户"); System.out.println("是否包含文本值:"+assertEleContains2); assertEleContains2 = assertEleContains(By.cssSelector("div#returnsDiv"), "余额转出明细"); System.out.println("是否包含文本值:"+assertEleContains2); /** * 点击余额转出明细 */ click(By.cssSelector("[onclick*='historyRefundData']")); /** * 输入 余额转出明细 - 开始日期输入框 */ modifyValueByJs(waitVisibilityOfElementLocated(By.cssSelector("input#beginDate")), "2021-01-01"); /** * 输入 余额转出明细 - 结束日期输入框 */ modifyValueByJs(waitVisibilityOfElementLocated(By.cssSelector("input#endDate")), "2021-03-13"); /** * 选择 余额转出明细 - 转出状态 */ new Select(waitVisibilityOfElementLocated(By.cssSelector("select#applyStatus"))).selectByValue("P"); /** * 点击余额转出明细 - 点击查询 */ clickByJs(waitVisibilityOfElementLocated(By.cssSelector("[onclick*='submitQuery']"))); // click(By.cssSelector("[onclick*='submitQuery']")); /** * 断言查询后展示的数>=1 */ waitTime(2000); assertEleContains2 = assertInfoCountBigOne(By.cssSelector(".use_money_tr")); System.out.println("元素是否大于等于1个元素:"+assertEleContains2); /** * 判断余额转出明细详情页 是否包含 div#history_data_display * 流水号 * 转移金额 */ assertEleContains2 = assertEleContains(By.cssSelector("div#history_data_display"), "流水号"); System.out.println("是否包含文本值:"+assertEleContains2); assertEleContains2 = assertEleContains(By.cssSelector("div#history_data_display"), "转移金额"); System.out.println("是否包含文本值:"+assertEleContains2); /** * 余额转出明细详情页 返回按钮[onclick*='goBack()'] */ click(By.cssSelector("[onclick*='goBack()']")); /** * 点击 单位对公账户链接 */ click(By.cssSelector("[onclick*='setBankAcounts']")); /** * 单位对公账户链接 后显示的用户须知 */ assertEleContains2 = assertEleContains(By.cssSelector("div#infoDiv"),"用户告知"); System.out.println("是否包含文本值:"+assertEleContains2); assertEleContains2 = assertEleContains(By.cssSelector("div#infoDiv"),"注意事项"); System.out.println("是否包含文本值:"+assertEleContains2); assertEleContains2 = assertEleContains(By.cssSelector("div#infoDiv"),"责任免除"); /** * 单位对公账户链接 接受按钮 */ click(By.cssSelector("[onclick*='goToNext']")); /** * 账户信息维护页-开户银行 */ waitVisibilityOfElementLocated(By.cssSelector("#bankName")); // input(By.cssSelector("input#bankName"), "a"); try { Robot r = new Robot(); waitTime(1000); r.keyPress(KeyEvent.VK_TAB); r.keyRelease(KeyEvent.VK_TAB); r.delay(200); waitTime(1000); r.keyPress(KeyEvent.VK_TAB); r.keyRelease(KeyEvent.VK_TAB); r.delay(200); waitTime(1000); r.keyPress(KeyEvent.VK_B); r.keyRelease(KeyEvent.VK_B); r.delay(200); waitTime(1000); waitTime(1000); r.keyPress(KeyEvent.VK_ENTER); r.keyRelease(KeyEvent.VK_ENTER); r.delay(200); waitTime(1000); r.keyPress(KeyEvent.VK_DOWN); r.keyRelease(KeyEvent.VK_DOWN); r.delay(200); waitTime(1000); r.keyPress(KeyEvent.VK_ENTER); r.keyRelease(KeyEvent.VK_ENTER); r.delay(200); waitTime(1000); /** * 输入账户信息维护-分行 */ waitTime(1000); r.keyPress(KeyEvent.VK_TAB); r.keyRelease(KeyEvent.VK_TAB); r.delay(200); waitTime(1000);waitTime(1000); r.keyPress(KeyEvent.VK_B); r.keyRelease(KeyEvent.VK_B); r.delay(200); waitTime(1000); r.keyPress(KeyEvent.VK_ENTER); r.keyRelease(KeyEvent.VK_ENTER); r.delay(200); waitTime(1000); waitTime(1000); r.keyPress(KeyEvent.VK_DOWN); r.keyRelease(KeyEvent.VK_DOWN); r.delay(200); waitTime(1000); r.keyPress(KeyEvent.VK_ENTER); r.keyRelease(KeyEvent.VK_ENTER); r.delay(200); waitTime(1000); /** * 账户信息维护-支行 * 输入支行 */ input(By.cssSelector("#subbranchBank"), "自动化支行"+new Random().nextInt(1000)); /** * 账户信息维护-银行账户 * bankAccount */ input(By.cssSelector("#bankAccount"), "6217855300003734772"); /** * 点击账户信息维护-确认按钮 doSaveButton */ click(By.cssSelector("#doSaveButton")); /** * 点击账户信息维护-弹框确定按钮 */ boolean assertAlterContains = assertAlterContains("银行账号信息保存成功"); System.out.println("assertAlterContains:"+assertAlterContains); waitTime(1000); r.keyPress(KeyEvent.VK_ENTER); r.keyRelease(KeyEvent.VK_ENTER); r.delay(200); waitTime(1000); } catch (Exception e2) { // TODO Auto-generated catch block e2.printStackTrace(); } /** * 点击转出按钮 */ clickByJs(waitElementToBeClickable(By.cssSelector("[onclick*='queryFundAmount']"))); /** * 点击 单位对公账户链接 */ click(By.cssSelector("[onclick*='setBankAcounts']"));/** * 单位对公账户链接 接受按钮 */ click(By.cssSelector("[onclick*='goToNext']")); /** * 历史维护账户信息 历史维护账户信息按钮 */ click(By.cssSelector("[onclick*='queryHistoryBank']")); /** * 历史维护账号信息-历史信息勾选框 * 判断是否包含页面带出历史账户信息提示 */ boolean assertEleContains3 = assertEleContains(By.cssSelector("#bankDiv2"),"页面带出历史账户信息"); System.out.println(assertEleContains3); /** * 判断历史账户信息行唯一 */ boolean assertInfoCountOne2 = assertInfoCountOne(By.cssSelector("#bankDiv2 [id*='items']")); System.out.println("assertInfoCountOne2:"+assertInfoCountOne2); String text = waitVisibilityOfElementLocated(By.cssSelector("#bankno1")).getText(); System.out.println(text); /** * 点击历史维护账号信息-历史账户勾选框 */ click(By.cssSelector("#check1")); waitTime(2000); /** * 断言历史维护账号信息-跳转到 历史维护账号信息 */ boolean assertEleContains4 = assertEleContains(By.cssSelector("#bankDiv"),"账户信息维护"); System.out.println(assertEleContains4); /** * 账户信息维护-确认按钮 * */ click(By.cssSelector("#doSaveButton")); /** * 点击账户信息维护-弹框确定按钮 */ boolean assertAlterContains = assertAlterContains("银行账号信息保存成功"); System.out.println("assertAlterContains:"+assertAlterContains); waitTime(1000); Robot r; try { r = new Robot(); r.keyPress(KeyEvent.VK_ENTER); r.keyRelease(KeyEvent.VK_ENTER); r.delay(200); waitTime(1000); } catch (Exception e3) { // TODO Auto-generated catch block e3.printStackTrace(); } /** * 点击转出按钮 */ clickByJs(waitElementToBeClickable(By.cssSelector("[onclick*='queryFundAmount']"))); /** * 余额转出-可用转出金额输入框 */ String refundMoney = "1"; modifyValueByJs(waitVisibilityOfElementLocated(By.cssSelector("input#amount")), refundMoney); /** * [onclick*='callRefundSubmit'] * 余额转出-确定转出按钮 */ click(By.cssSelector("[onclick*='callRefundSubmit']")); /** * 判断余额转出-确定转出后-处理完成提示框是否包含转出的价格 * */ boolean assertEleContains5 = assertEleContains(By.cssSelector("div#suPag"),"余额"+refundMoney+"元"); System.out.println("assertEleContains5:"+assertEleContains5); /** * 余额转出-确定转出后-处理完成提示框-确定按钮 */ click(By.cssSelector("[onclick*='gotoView']")); /** * 余额管理-转入按钮 */ click(By.cssSelector("[onclick*='findUseableBalacePol']")); /** * 余额管理-余额转入 * detail_useable_div * 判断 余额转入是否包含 余额转入详情页 */ assertEleContains(By.cssSelector("div#detail_useable_div"),"余额转入"); assertEleContains(By.cssSelector("div#detail_useable_div"),policyNo); assertEleContains(By.cssSelector("div#detail_useable_div"),"转移余额"); assertEleContains(By.cssSelector("div#detail_useable_div"),"可用余额"); /** * 余额管理-余额转入-余额互转明细链接 */ click(By.cssSelector("[onclick*='historyData']")); //==================================================== /** * 输入 余额互转明细 - 开始日期输入框 */ modifyValueByJs(waitVisibilityOfElementLocated(By.cssSelector("input#applyBeginDate")), "2021-01-01"); /** * 输入 余额互转明细 - 结束日期输入框 */ modifyValueByJs(waitVisibilityOfElementLocated(By.cssSelector("input#applyEndDate")), "2021-03-13"); /** * 选择 余额互转明细 - 支付模式 */ new Select(waitVisibilityOfElementLocated(By.cssSelector("select#payMode"))).selectByValue("2"); /** * 点击余额互转明细 - 点击查询 */ clickByJs(waitVisibilityOfElementLocated(By.cssSelector("[onclick*='histMovePage']"))); // click(By.cssSelector("[onclick*='submitQuery']")); /** * 断言查询后展示的数>=1 */ waitTime(2000); assertEleContains2 = assertInfoCountBigOne(By.cssSelector(".use_money_tr")); System.out.println("元素是否大于等于1个元素:"+assertEleContains2); /** * 判断余额转出明细详情页 是否包含 div#history_data_display * 流水号 * 转移金额 */ assertEleContains2 = assertEleContains(By.cssSelector("div#history_data_display"), "流水号"); System.out.println("是否包含文本值:"+assertEleContains2); assertEleContains2 = assertEleContains(By.cssSelector("div#history_data_display"), "转移金额"); System.out.println("是否包含文本值:"+assertEleContains2); /** * 余额管理查询页 - 查询结果行 boolean assertContains = assertRowContains(By.cssSelector("#user_pol_div_id .img-position tr"),policyNo); */ String text2 = waitVisibilityOfElementLocated(By.xpath("//th[contains(text(),'保单号')]/parent::tr/following-sibling::tr")).getText(); String regexSingleMatch = RegexUtils.regexSingleMatch("(?<=\\s)\\d+\\.\\d+(?=\\s)", text2); Double d = Double.parseDouble(regexSingleMatch); System.out.println(text2+" "+d); if (beforeMOney1==(d+Double.parseDouble(refundMoney))) { System.out.println("true"); }else { System.out.println("false"); } /** * 点击余额互转明细 - 返回 ackWay(); */ clickByJs(waitVisibilityOfElementLocated(By.cssSelector("[onclick*='backWay']"))); /** * 暂时没有转入订单 余额转入-确定转入按钮 */ clickByJs(waitVisibilityOfElementLocated(By.cssSelector("[onclick*='submitConvertMoney']"))); } } //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== public void yuEGuanLiBase() { } //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== /** * 判断弹框文本是否包含关键字 * @param key * @return 包含返回true,否则false */ private static boolean assertAlterContains(String key) { waitTime(3000); System.out.println(getBroswer().switchTo().alert().getText()); return getBroswer().switchTo().alert().getText().contains(key); } /** * 判断元素的属性值是否包含预期的属性值 * @param by * @param value * @param name * @return 包含返回true,不包含返回false */ public static boolean assertEleContains(By by,String value,String name) { return waitVisibilityOfElementLocated(by).getAttribute(name).contains(value); } /** * 判断元素的文本是否包含关键字 * @param by * @param key * @return 包含返回true,不包含返回false */ public static boolean assertEleContains(By by,String key) { return waitVisibilityOfElementLocated(by).getText().contains(key); } /** * 判断所有元素获取的文本是否包含指定关键字 * @param by * @param key * @return 包含返回true,不包含false */ private static boolean assertRowContains(By by,String key) { List<WebElement> list = waitVisibilityOfAllElementsLocatedBy(by); for (WebElement webElement : list) { if (webElement.getText().contains(key)) { return true; } } return false; } /** * 判断所有元素个数是否一个 * @param by * @param key * @return 是一个true,不是唯一返回false */ private static boolean assertInfoCountOne(By by) { List<WebElement> list = waitVisibilityOfAllElementsLocatedBy(by); if (list.size()==1) { return true; } return false; } /** * 判断所有元素个数是否大于等于一个 * @param by * @param key * @return 是一个true,不是唯一返回false */ private static boolean assertInfoCountBigOne(By by) { List<WebElement> list = waitVisibilityOfAllElementsLocatedBy(by); if (list.size()>=1) { return true; } return false; } /** * 根据获取的url来切换句柄 获取的标题包含预期的标题才能切换 * @param url 标题 */ public void switchWindowByCurrentUrl(String url) { Set<String> handles = getBroswer().getWindowHandles(); logger.info("获取【handles大小】为:【"+handles.size()+"】,【获取的handles】为:【"+handles.toString()+"】"); for (String handle : handles) { if (getBroswer().getCurrentUrl().contains(url)) { logger.info("当前句柄的【url】:【"+getBroswer().getCurrentUrl()+"】,符合预期的标题,切换成功"); break; }else { logger.info("当前句柄的【url】:【"+getBroswer().getCurrentUrl()+"】,切换句柄,寻找符合标题的句柄"); getBroswer().switchTo().window(handle); } } } /** * 等待单个元素存在在dom对象中,但不一定可见 * @param locator By 对象定位信息 * @return 返回存在dom 单个WebElement中元素,否则抛出异常 */ public static WebElement waitPresenceOfElementLocated(By locator) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,等待元素在dom中存在"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.presenceOfElementLocated(locator)).toString()+"】"); return getWait().until(ExpectedConditions.presenceOfElementLocated(locator)); } /** * 等待单个元素存在在dom对象中,但不一定可见 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @return 返回存在dom 单个WebElement中元素,否则抛出异常 */ public static WebElement waitPresenceOfElementLocated(String expectedPageKeyword,String expectedUIElementKeyword) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,等待元素在dom中存在"); By by = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【元素定位信息】为:【"+by.toString()+"】"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.presenceOfElementLocated(by)).toString()+""); return getWait().until(ExpectedConditions.presenceOfElementLocated(by)); } /** * 等待多个个元素存在在dom对象中,但不一定可见 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @return 返回存在dom 多个WebElement中元素-List<WebElement>,否则抛出异常 */ public static List<WebElement> waitPresenceOfAllElementsLocatedBy(String expectedPageKeyword,String expectedUIElementKeyword) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,等待元素们在dom中存在"); By by = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【元素定位信息】为:【"+by.toString()+"】"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.presenceOfAllElementsLocatedBy(by)).toString()+"】,其个数为【"+getWait().until(ExpectedConditions.presenceOfAllElementsLocatedBy(by)).size()+"】"); return getWait().until(ExpectedConditions.presenceOfAllElementsLocatedBy(by)); } /** * 等待多个个元素存在在dom对象中,但不一定可见 * @param locator By 对象定位信息 * @return 返回存在dom 多个WebElement中元素-List<WebElement>,否则抛出异常 */ public static List<WebElement> waitPresenceOfAllElementsLocatedBy(By locator) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,等待元素们在dom中存在"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.presenceOfAllElementsLocatedBy(locator)).toString()+"】,其个数为【"+getWait().until(ExpectedConditions.presenceOfAllElementsLocatedBy(locator)).size()+"】"); return getWait().until(ExpectedConditions.presenceOfAllElementsLocatedBy(locator)); } //=================== /** * 等待单个元素在页面可见 * @param locator By 对象定位信息 * @return 返回页面可见的 单个WebElement中元素,否则抛出异常 */ public static WebElement waitVisibilityOfElementLocated(By locator) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,等待元素在页面可见"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.visibilityOfElementLocated(locator)).toString()+"】"); return getWait().until(ExpectedConditions.visibilityOfElementLocated(locator)); } /** * 等待单个元素在页面可见 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @return 返回页面可见的 单个WebElement中元素,否则抛出异常 */ public static WebElement waitVisibilityOfElementLocated(String expectedPageKeyword,String expectedUIElementKeyword) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,等待元素在在页面可见"); By by = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【元素定位信息】为:【"+by.toString()+"】"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.visibilityOfElementLocated(by)).toString()+""); return getWait().until(ExpectedConditions.visibilityOfElementLocated(by)); } /** * 等待单个元素在页面可见 * @param element 需要等待的元素 * @return 返回页面可见的 单个WebElement中元素,否则抛出异常 */ public static WebElement waitVisibilityOfElementLocated(WebElement element) { logger.info("获取【元素】为:【"+element.toString()+"】,等待页面可见"); return getWait().until(ExpectedConditions.visibilityOf(element)); } /** * 等待多个个元素在页面可见 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @return 返回存在页面 多个WebElement中元素-List<WebElement>,否则抛出异常 */ public static List<WebElement> waitVisibilityOfAllElementsLocatedBy(String expectedPageKeyword,String expectedUIElementKeyword) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,等待元们素在页面可见"); By by = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【元素定位信息】为:【"+by.toString()+"】"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.visibilityOfAllElementsLocatedBy(by)).toString()+"】,其个数为【"+getWait().until(ExpectedConditions.visibilityOfAllElementsLocatedBy(by)).size()+"】"); return getWait().until(ExpectedConditions.visibilityOfAllElementsLocatedBy(by)); } /** * 等待多个个元素在页面可见 * @param locator By 对象定位信息 * @return 返回存在页面 多个WebElement中元素-List<WebElement>,否则抛出异常 */ public static List<WebElement> waitVisibilityOfAllElementsLocatedBy(By locator) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,等待元素们在页面可见"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator)).toString()+"】,其个数为【"+getWait().until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator)).size()+"】"); return getWait().until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator)); } /** * 等待多个个元素在页面可见 * @param locator By 对象定位信息 * @return 返回存在页面 多个WebElement中元素-List<WebElement>,否则抛出异常 */ public static List<WebElement> waitVisibilityOfAllElementsLocatedBy(WebElement ... elements) { logger.info("获取【locator定位信息】为:【"+elements.toString()+"】,等待元素们在页面可见"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.visibilityOfAllElements(elements)).toString()+"】,其个数为【"+getWait().until(ExpectedConditions.visibilityOfAllElements(elements)).size()+"】"); return getWait().until(ExpectedConditions.visibilityOfAllElements(elements)); } /** * 等待单个元素可被点击 * @param locator By 对象定位信息 * @return 返回页面可点击单个WebElement中元素,否则抛出异常 */ public static WebElement waitElementToBeClickable(By locator) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,等待元素可被点击"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.elementToBeClickable(locator)).toString()+"】"); return getWait().until(ExpectedConditions.elementToBeClickable(locator)); } /** * 等待单个元素可被点击 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @return 返回页面可点击单个WebElement中元素,否则抛出异常 */ public static WebElement waitElementToBeClickable(String expectedPageKeyword,String expectedUIElementKeyword) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,等待元素可被点击"); By by = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【元素定位信息】为:【"+by.toString()+"】"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.elementToBeClickable(by)).toString()+""); return getWait().until(ExpectedConditions.elementToBeClickable(by)); } /** * 等待单个元素可被点击 * @param element 需要等待的元素 * @return 返回页面可点击单个WebElement中元素,否则抛出异常 */ public static WebElement waitElementToBeClickable(WebElement element) { logger.info("获取【元素】为:【"+element.toString()+"】,等待元素可被点击"); return getWait().until(ExpectedConditions.elementToBeClickable(element)); } /** * 等待Frame可以跳转并跳转进去 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @return 返回webDriver实例 */ public static WebDriver waitFrameToBeAvailableAndSwitchToIt(String expectedPageKeyword,String expectedUIElementKeyword) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,等待Frame可以跳转并跳转进去"); By by = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【元素定位信息】为:【"+by.toString()+"】"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(by)).toString()+""); return getWait().until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(by)); } /** * 等待Frame可以跳转并跳转进去 * @param element 需要等待的元素 * @return 返回webDriver实例 */ public static WebDriver waitFrameToBeAvailableAndSwitchToIt(WebElement element) { logger.info("获取【元素】为:【"+element.toString()+"】,等待Frame可以跳转并跳转进去"); return getWait().until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(element)); } /** * 等待Frame可以跳转并跳转进去 * @param frameLocator (id or name) * @return 返回webDriver实例 */ public static WebDriver waitFrameToBeAvailableAndSwitchToIt(String frameLocator) { logger.info("获取【frame (id or name)】为:【"+frameLocator+"】,等待Frame可以跳转并跳转进去"); return getWait().until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frameLocator)); } /** * 等待Frame可以跳转并跳转进去 * @param frameLocator index * @return 返回webDriver实例 */ public static WebDriver waitFrameToBeAvailableAndSwitchToIt(int frameLocator) { logger.info("获取【frame (index)】为:【"+frameLocator+"】,等待Frame可以跳转并跳转进去"); return getWait().until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frameLocator)); } /** * 等待Frame可以跳转并跳转进去 * @param locator By 对象定位信息 * @return 返回webDriver实例 */ public static WebDriver waitFrameToBeAvailableAndSwitchToIt(By locator) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,等待Frame可以跳转并跳转进去"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(locator)).toString()+"】"); return getWait().until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(locator)); } /** * 点击某个元素 * @param locator By定位对象 */ public static void click(By locator ) { try { logger.info("点击了等待可以点击的元素,其定位信息【"+locator.toString()+"】"); waitElementToBeClickable(locator).click(); } catch (Exception e) { try { logger.info("点击了等待可以点击的元素失败,通过js点击,其定位信息【"+locator.toString()+"】"); clickByJs(waitPresenceOfElementLocated(locator)); } catch (Exception e2) { e.printStackTrace(); } } } /** * 点击某个元素 * @param element 元素对象 */ public static void click(WebElement element ) { try { logger.info("点击了等待可以点击的元素,其元素信息【"+element.toString()+"】"); waitElementToBeClickable(element).click(); } catch (Exception e) { try { logger.info("点击了等待可以点击的元素失败,通过js点击,其元素信息【"+element.toString()+"】"); clickByJs(element); } catch (Exception e2) { e.printStackTrace(); } } } /** * 点击某个元素 * @param expectedPageKeyword 预期页面 * @param expectedUIElementKeyword 预期关键字 */ public static void click(String expectedPageKeyword,String expectedUIElementKeyword ) { try { logger.info("点击了【关键页面】:【"+expectedPageKeyword+"】【关键元素】:【"+expectedUIElementKeyword+"】"); waitElementToBeClickable(expectedPageKeyword, expectedUIElementKeyword).click(); } catch (Exception e) { try { logger.info("点击了【关键页面】:【"+expectedPageKeyword+"】【关键元素】:【"+expectedUIElementKeyword+"】失败,现通过js点击"); clickByJs(waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword)); } catch (Exception e2) { e.printStackTrace(); } } } /** * 输入信息 * @param locator By定位对象 * @param keysToSend 输入的值 */ public static void input(By locator,String keysToSend) { try { logger.info("对dom存在的元素进行输入,其定位信息【"+locator.toString()+"】,【输入的内容】为:【"+keysToSend+"】"); waitPresenceOfElementLocated(locator).sendKeys(keysToSend); if (!checkTextToBePresentInElementValue(locator, keysToSend)) { logger.info("对dom存在的元素进行输入,输入的信息和value值不一致,通过js直接改变value值,其定位信息【"+locator.toString()+"】,【输入的内容】为:【"+keysToSend+"】"); modifyValueByJs(waitVisibilityOfElementLocated(locator), keysToSend); } } catch (Exception e) { e.printStackTrace(); } } /** * 输入信息 * @param element 元素对象 * @param keysToSend 输入的值 */ public static void input(WebElement element,String keysToSend ) { try { logger.info("对dom存在的元素进行输入,其定位信息【"+element.toString()+"】,【输入的内容】为:【"+keysToSend+"】"); element.sendKeys(keysToSend); if (!checkTextToBePresentInElementValue(element, keysToSend)) { logger.info("对dom存在的元素进行输入,输入的信息和value值不一致,通过js直接改变value值,其定位信息【"+element.toString()+"】,【输入的内容】为:【"+keysToSend+"】"); modifyValueByJs(element, keysToSend); } } catch (Exception e) { e.printStackTrace(); } } /** * 点击某个元素 * @param expectedPageKeyword 预期页面 * @param expectedUIElementKeyword 预期关键字 * @param keysToSend 输入的值 */ public static void input(String expectedPageKeyword,String expectedUIElementKeyword,String keysToSend ) { try { logger.info("向【关键页面】:【"+expectedPageKeyword+"】【关键元素】:【"+expectedUIElementKeyword+"】输入【 "+keysToSend+"】"); waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword).sendKeys(keysToSend); if (!checkTextToBePresentInElementValue(waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword), keysToSend)) { logger.info("对dom存在的元素进行输入,输入的信息和value值不一致,通过js直接改变value值,其关键页面【"+expectedPageKeyword+"】,【关键元素】:【"+ expectedUIElementKeyword+"】,【输入的内容】为:【"+keysToSend+"】"); modifyValueByJs(waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword), keysToSend); } }catch (Exception e) { e.printStackTrace(); } } /** * 取消选择所有select * @param locator By 对象定位信息 */ public static void deselectAll(By locator) { logger.info("取消所有的select选项,select定位是:【"+locator.toString()+"】"); Select select = new Select(waitPresenceOfElementLocated(locator)); select.deselectAll(); } /** * 取消选择所有select * @param element WebElement对象 */ public static void deselectAll(WebElement element) { logger.info("取消所有的select选项,select定位是:【"+element.toString()+"】"); Select select = new Select(element); select.deselectAll(); } /** * 取消选择所有select * @param expectedPageKeyword 预期页面 * @param expectedUIElementKeyword 预期关键字 */ public static void deselectAll(String expectedPageKeyword,String expectedUIElementKeyword) { logger.info("取消所有的select选项"); Select select = new Select(waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword)); select.deselectAll(); } /** * 通过索引来选择select * @param locator By 对象定位信息 * @param index 索引 */ public static void selectByIndex(By locator,int index) { logger.info("通过索引来选择select,select定位是:【"+locator.toString()+"】,【索引值】:【"+index+"】"); Select select = new Select(waitPresenceOfElementLocated(locator)); select.selectByIndex(index); } /** * 通过索引来选择select * @param element WebElement对象 * @param index 索引 */ public static void selectByIndex(WebElement element,int index) { logger.info("通过索引来选择select,select定位是:【"+element.toString()+"】,【索引值】:【"+index+"】"); Select select = new Select(element); select.selectByIndex(index); } /** * 通过索引值来选择select * @param expectedPageKeyword 预期页面 * @param expectedUIElementKeyword 预期关键字 * @param index 索引值 */ public static void selectByIndex(String expectedPageKeyword,String expectedUIElementKeyword,int index) { logger.info("通过索引值来选择select,select所在关键页面是:【"+expectedPageKeyword+"】,所在关键元素是【"+expectedUIElementKeyword+"】,【索引值】:【"+index+"】"); Select select = new Select(waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword)); select.selectByIndex(index); } /** * 通过value值来选择select * @param locator By 对象定位信息 * @param value value值 */ public static void selectByValue(By locator,String value) { logger.info("通过value值来选择select,select定位是:【"+locator.toString()+"】,【value值值】:【"+value+"】"); Select select = new Select(waitPresenceOfElementLocated(locator)); select.selectByValue(value); } /** * 通过value值来选择select * @param element WebElement对象 * @param value value值 */ public static void selectByValue(WebElement element,String value) { logger.info("通过value值来选择select,select定位是:【"+element.toString()+"】,【value值值】:【"+value+"】"); Select select = new Select(element); select.selectByValue(value); } /** * 通过value值来选择select * @param expectedPageKeyword 预期页面 * @param expectedUIElementKeyword 预期关键字 * @param value value值 */ public static void selectByValue(String expectedPageKeyword,String expectedUIElementKeyword,String value) { logger.info("通过value值来选择select,select所在关键页面是:【"+expectedPageKeyword+"】,所在关键元素是【"+expectedUIElementKeyword+"】,【value值值】:【"+value+"】"); Select select = new Select(waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword)); select.selectByValue(value); } // /** * 通过可见文本值来选择select * @param locator By 对象定位信息 * @param text 可见文本值 */ public static void selectByVisibleText(By locator,String text) { logger.info("通过可见文本值来选择select,select定位是:【"+locator.toString()+"】,【可见文本值】:【"+text+"】"); Select select = new Select(waitPresenceOfElementLocated(locator)); select.selectByVisibleText(text); } /** * 通过可见文本值来选择select * @param element WebElement对象 * @param text 可见文本值 */ public static void selectByVisibleText(WebElement element,String text) { logger.info("通过可见文本值来选择select,select定位是:【"+element.toString()+"】,【可见文本值】:【"+text+"】"); Select select = new Select(element); select.selectByVisibleText(text); } /** * 通过可见文本值来选择select * @param expectedPageKeyword 预期页面 * @param expectedUIElementKeyword 预期关键字 * @param text 可见文本值 */ public static void selectByVisibleText(String expectedPageKeyword,String expectedUIElementKeyword,String text) { logger.info("通过可见文本值来选择select,select所在关键页面是:【"+expectedPageKeyword+"】,所在关键元素是【"+expectedUIElementKeyword+"】,【可见文本值】:【"+text+"】"); Select select = new Select(waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword)); select.selectByVisibleText(text); } /** * 确认alter弹框 */ public static void acceptAlert() { logger.info("确认alter弹框"); Alert alert = getBroswer().switchTo().alert(); alert.accept(); } /** * 关闭浏览器 */ public static void closeBrowser() { logger.info("关闭浏览器"); getBroswer().quit(); } /** * 取消alter弹框 */ public static void dismissAlert() { logger.info("取消alter弹框"); Alert alert = getBroswer().switchTo().alert(); alert.dismiss(); } /** * 检查给定元素是否被选中的期望值。 * @param locator By 对象定位信息 * @param selected 预期的选中状态 * @return 返回true,实际选中状态和预期选中状态一致,反之返回false */ public static Boolean checkElementSelectionStateToBe(By locator,boolean selected) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,检查select是否被选中,【预期选中状态】:【"+selected+"】"); logger.info("返回的实际选中状态【"+ getWait().until(ExpectedConditions.elementSelectionStateToBe(locator, selected))+"】"); return getWait().until(ExpectedConditions.elementSelectionStateToBe(locator, selected)); } /** * 检查给定元素是否被选中的期望值。 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @return 返回true,实际选中状态和预期选中状态一致,反之返回false */ public static Boolean checkElementSelectionStateToBe(String expectedPageKeyword,String expectedUIElementKeyword,boolean selected ) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,检查select是否被选中"); By locator = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,检查select是否被选中"); logger.info("返回的实际选中状态【"+ getWait().until(ExpectedConditions.elementSelectionStateToBe(locator, selected))+"】"); return getWait().until(ExpectedConditions.elementSelectionStateToBe(locator, selected)); } /** * 检查给定元素是否被选中的期望值。 * @param element 需要判断的元素 * @param selected 预期的选中状态 * @return 返回true,实际选中状态和预期选中状态一致,反之返回false */ public static Boolean checkElementSelectionStateToBe(WebElement element,boolean selected) { logger.info("获取【element元素信息】为:【"+element.toString()+"】,检查select是否被选中,【预期选中状态】:【"+selected+"】"); logger.info("返回的实际选中状态【"+ getWait().until(ExpectedConditions.elementSelectionStateToBe(element, selected))+"】"); return getWait().until(ExpectedConditions.elementSelectionStateToBe(element, selected)); } /** * 检查给定元素是否被选中。 * @param locator By 对象定位信息 * @return 已选择返回true,反之返回false */ public static Boolean checkElementToBeSelected(By locator) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,检查select被选中"); logger.info("返回的实际选中状态【"+ getWait().until(ExpectedConditions.elementToBeSelected(locator))+"】"); return getWait().until(ExpectedConditions.elementToBeSelected(locator)); } /** * 检查给定元素是否被选中。 * @param element 需要判断的元素 * @param selected 预期的选中状态 * @return 已选择返回true,反之返回false */ public static Boolean checkElementToBeSelected(WebElement element) { logger.info("获取【element元素信息】为:【"+element.toString()+"】,检查select被选中"); logger.info("返回的实际选中状态【"+ getWait().until(ExpectedConditions.elementToBeSelected(element))+"】"); return getWait().until(ExpectedConditions.elementToBeSelected(element)); } /** * 检查给定元素是否被选中。 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @return 已选择返回true,反之返回false */ public static Boolean checkElementToBeSelected(String expectedPageKeyword,String expectedUIElementKeyword ) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,检查select被选中"); By locator = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,检查select被选中"); logger.info("返回的实际选中状态【"+ getWait().until(ExpectedConditions.elementToBeSelected(locator))+"】"); return getWait().until(ExpectedConditions.elementToBeSelected(locator)); } /** * 获取指定元素的实际文本,是否【等于】预期的文本值 * @param locator By 对象定位信息 * @param value 预期的文本值 * @return 等于返回true,反之返回false */ public static Boolean checkTextToBe(By locator,String value) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,预期的文本值【"+value+"】,获取指定元素的实际文本,是否【等于】预期的文本值"); logger.info("获取【实际文本值】为:【"+waitPresenceOfElementLocated(locator).getText()+"】"); logger.info("返回的文本与预期的文本是否相等状态:【"+ getWait().until(ExpectedConditions.textToBe(locator,value ))+"】"); return getWait().until(ExpectedConditions.textToBe(locator, value)); } /** * 获取指定元素的实际文本,是否【等于】预期的文本值 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @param value 预期的文本值 * @return 等于返回true,反之返回false */ public static Boolean checkTextToBe(String expectedPageKeyword,String expectedUIElementKeyword,String value) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,获取指定元素的实际文本,是否【等于】预期的文本值"); By locator = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,获取指定元素的实际文本,是否等于预期的文本值"); logger.info("获取【实际文本值】为:【"+waitPresenceOfElementLocated(locator).getText()+"】"); logger.info("返回的文本与预期的文本是否相等状态:【"+ getWait().until(ExpectedConditions.textToBe(locator,value ))+"】"); return getWait().until(ExpectedConditions.textToBe(locator, value)); } /** * 获取指定元素的实际文本,是否【包含】预期的文本值 * @param locator By 对象定位信息 * @param value 预期的文本值 * @return 等于返回true,反之返回false */ public static Boolean checkTextToBePresentInElementLocated(By locator,String value) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,预期的文本值【"+value+"】,,获取指定元素的实际文本,是否【包含】预期的文本值"); logger.info("获取【实际文本值】为:【"+waitPresenceOfElementLocated(locator).getText()+"】"); logger.info("返回的文本与预期的文本是否【相等】状态:【"+ getWait().until(ExpectedConditions.textToBePresentInElementLocated(locator, value))+"】"); return getWait().until(ExpectedConditions.textToBePresentInElementLocated(locator, value)); } /** * 获取指定元素的实际文本,是否【包含】预期的文本值 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @param value 预期的文本值 * @return 等于返回true,反之返回false */ public static Boolean checkTextToBePresentInElementLocated(String expectedPageKeyword,String expectedUIElementKeyword,String value) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,获取指定元素的实际文本,是否【包含】等于预期的文本值"); By locator = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,获取指定元素的实际文本,是否【包含】等于预期的文本值"); logger.info("获取【实际文本值】为:【"+waitPresenceOfElementLocated(locator).getText()+"】"); logger.info("返回的文本与预期的文本是否【包含】状态:【"+ getWait().until(ExpectedConditions.textToBePresentInElementLocated(locator,value ))+"】"); return getWait().until(ExpectedConditions.textToBePresentInElementLocated(locator, value)); } /** * 获取指定元素的实际文本,是否【包含】预期的文本值 * @param element 指定元素 * @param value 预期的文本值 * @return 等于返回true,反之返回false */ public static Boolean checkTextToBePresentInElementLocated(WebElement element,String value) { logger.info("获取【element元素信息】为:【"+element.toString()+"】,预期的文本值【"+value+"】,获取指定元素的实际文本值获取指定元素的实际文本,是否【包含】等于预期的文本值"); logger.info("获取【实际文本值】为:【"+element.getText()+"】"); logger.info("返回的文本与预期的文本是否【包含】状态:【"+ getWait().until(ExpectedConditions.textToBePresentInElement(element, value))+"】"); return getWait().until(ExpectedConditions.textToBePresentInElement(element, value)); } /** * 获取指定元素的实际value属性值,是否【包含】预期的value属性值 * @param element 指定元素 * @param value 预期value * @return 等于返回true,反之返回false */ public static Boolean checkTextToBePresentInElementValue(WebElement element,String value) { logger.info("获取【element元素信息】为:【"+element.toString()+"】,预期的value值【"+value+"】,获取指定元素的实际value值获取指定元素的实际文本,是否【包含】等于预期的value值"); logger.info("获取【实际value值】为:【"+element.getAttribute("value")+"】"); logger.info("返回的文本与预期的文本是否【包含】状态:【"+ getWait().until(ExpectedConditions.textToBePresentInElementValue(element, value))+"】"); return getWait().until(ExpectedConditions.textToBePresentInElementValue(element, value)); } /** * 获取指定元素的实际value属性值,是否【包含】预期的value属性值 * @param locator By 对象定位信息 * @param value 预期value值 * @return 等于返回true,反之返回false */ public static Boolean checkTextToBePresentInElementValue(By locator,String value) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,预期的文本值【"+value+"】,获取指定元素的实际value值,获取指定元素的实际value属性值,是否【包含】等于预期的value属性值"); logger.info("获取【实际value值】为:【"+waitPresenceOfElementLocated(locator).getAttribute("value")+"】"); logger.info("返回的实际value值与预期的value值是否【包含】状态:【"+ getWait().until(ExpectedConditions.textToBePresentInElementValue(locator, value))+"】"); return getWait().until(ExpectedConditions.textToBePresentInElementValue(locator, value)); } /** * 获取指定元素的实际value属性值,是否【包含】预期的value属性值 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @param value 预期的文本值 * @return 等于返回true,反之返回false */ public static Boolean checkTextToBePresentInElementValue(String expectedPageKeyword,String expectedUIElementKeyword,String value) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,【预期的属性值】:【"+value+"】获取指定元素的实际value属性值,是否【包含】等于预期的value属性值"); By locator = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,获取指定元素的实际value属性值,是否【包含】等于预期的value属性值"); logger.info("获取【实际value值】为:【"+waitPresenceOfElementLocated(locator).getAttribute("value")+"】"); logger.info("返回的实际value值与预期的value值是否【包含】状态:【"+ getWait().until(ExpectedConditions.textToBePresentInElementValue(locator,value ))+"】"); return getWait().until(ExpectedConditions.textToBePresentInElementValue(locator, value)); } /** * 获取指定元素的实际属性,是否【包含】预期的属性值 * @param element 指定元素 * @param attribute 指定属性名 * @param value 预期value * @return 等于返回true,反之返回false */ public static Boolean checkAttributeContains(WebElement element,String attribute,String value) { logger.info("获取【element元素信息】为:【"+element.toString()+"】,指定的【属性名】:【"+attribute+"】,【预期的属性值】:"+value+",获取指定元素的实际属性是否【包含】等于预期的属性值"); logger.info("获取【实际属性值】为:【"+element.getAttribute(attribute)+"】"); logger.info("返回的文本与预期的文本是否【包含】状态:【"+ getWait().until(ExpectedConditions.attributeContains(element, attribute, value))+"】"); return getWait().until(ExpectedConditions.attributeContains(element, attribute, value)); } /** * 检查alter弹框是否存在 * @return true存在,false不存在 */ public boolean checkAlertIsPresent() { try { logger.info("开始检查alter是否存在"); getWait().until(ExpectedConditions.alertIsPresent()); logger.info("开始检查alter存在"); return true; }catch (Exception e) { logger.info("开始检查alter不存在"); return false; } } /** * 检查页面元素是否可见 * @param locator * @return 如果返回为true,页面不可见或者元素不再dom中,返回false,页面可见 */ public static boolean checkInvisibilityOfElementLocated(By locator) { logger.info("检查元素是否不可见或页面不存在,【元素信息】:【"+locator.toString()+"】"); logger.info("检查元素是否不可见或页面的状态:【"+getWait().until(ExpectedConditions.invisibilityOfElementLocated(locator))+"】"); return getWait().until(ExpectedConditions.invisibilityOfElementLocated(locator)); } /** * 获取指定元素的实际属性,是否【包含】预期的属性值 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @param attribute 指定属性名 * @param value 预期的文本值 * @return 等于返回true,反之返回false */ public static Boolean checkAttributeContains(String expectedPageKeyword,String expectedUIElementKeyword,String attribute,String value) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,指定的【属性名】:【"+attribute+"】,【预期的属性值】:"+value+",获取指定元素的实际属性是否【包含】等于预期的属性值"); By locator = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,获取指定元素的实际属性值,是否【包含】等于预期的属性值"); logger.info("获取【实际属性值】为:【"+waitPresenceOfElementLocated(locator).getAttribute(attribute)+"】"); logger.info("返回的实际属性值与预期的属性值是否【包含】状态:【"+ getWait().until(ExpectedConditions.attributeContains(locator, attribute, value))+"】"); return getWait().until(ExpectedConditions.attributeContains(locator, attribute, value)); } /** * 获取指定元素的实际属性,是否【包含】预期的属性值 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @param attribute 指定属性名 * @param value 预期的文本值 * @return 等于返回true,反之返回false */ public static Boolean checkAttributeContains(By locator,String attribute,String value) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,【指定的属性名】:【"+attribute+"】,【指定的预期属性值】:【"+value+"】,获取指定元素的实际属性值,是否【包含】等于预期的属性值"); logger.info("获取【实际属性值】为:【"+waitPresenceOfElementLocated(locator).getAttribute(attribute)+"】"); logger.info("返回的实际属性值与预期的属性值是否【包含】状态:【"+ getWait().until(ExpectedConditions.attributeContains(locator, attribute, value))+"】"); return getWait().until(ExpectedConditions.attributeContains(locator, attribute, value)); } /** * 获取指定元素的实际属性,是否【等于】预期的属性值 * @param element 指定元素 * @param attribute 指定属性名 * @param value 预期value * @return 等于返回true,反之返回false */ public static Boolean checkAttributeToBe(WebElement element,String attribute,String value) { logger.info("获取【element元素信息】为:【"+element.toString()+"】,指定的【属性名】:【"+attribute+"】,【预期的属性值】:"+value+",获取指定元素的实际属性是否【等于】等于预期的属性值"); logger.info("获取【实际属性值】为:【"+element.getAttribute(attribute)+"】"); logger.info("返回的文本与预期的文本是否【等于】状态:【"+ getWait().until(ExpectedConditions.attributeToBe(element, attribute, value))+"】"); return getWait().until(ExpectedConditions.attributeToBe(element, attribute, value)); } /** * 获取指定元素的实际属性,是否【等于】预期的属性值 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @param attribute 指定属性名 * @param value 预期的文本值 * @return 等于返回true,反之返回false */ public static Boolean checkAttributeToBe(String expectedPageKeyword,String expectedUIElementKeyword,String attribute,String value) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,指定的【属性名】:【"+attribute+"】,【预期的属性值】:"+value+",获取指定元素的实际属性是否【等于】等于预期的属性值"); By locator = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,获取指定元素的实际属性值,是否【等于】等于预期的属性值"); logger.info("获取【实际属性值】为:【"+waitPresenceOfElementLocated(locator).getAttribute(attribute)+"】"); logger.info("返回的实际属性值与预期的属性值是否【等于】状态:【"+ getWait().until(ExpectedConditions.attributeToBe(locator, attribute, value))+"】"); return getWait().until(ExpectedConditions.attributeToBe(locator, attribute, value)); } /** * 获取指定元素的实际属性,是否【等于】预期的属性值 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @param attribute 指定属性名 * @param value 预期的文本值 * @return 等于返回true,反之返回false */ public static Boolean checkAttributeToBe(By locator,String attribute,String value) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,【指定的属性名】:【"+attribute+"】,【指定的预期属性值】:【"+value+"】,获取指定元素的实际属性值,是否【等于】等于预期的属性值"); logger.info("获取【实际属性值】为:【"+waitPresenceOfElementLocated(locator).getAttribute(attribute)+"】"); logger.info("返回的实际属性值与预期的属性值是否【等于】状态:【"+ getWait().until(ExpectedConditions.attributeToBe(locator, attribute, value))+"】"); return getWait().until(ExpectedConditions.attributeToBe(locator, attribute, value)); } // public void test() throws InterruptedException { // //把“随意拖拽1-1”元素移动到“随意拖拽1-2”节点下去 // WebElement webElement1 = BrowserUtil.driver.findElement(By.id("treeDemo_2_span")); // WebElement webElement2= BrowserUtil.driver.findElement(By.id("treeDemo_3_span")); // //构建鼠标的一连串动作 // Actions actions = new Actions(BrowserUtil.driver); // actions.clickAndHold(webElement1).moveToElement(webElement2).release().build().perform(); // } public static By getXmlBy(String path ,String expectedPageKeyword,String expectedUIElementKeyword) { try { String uiElementBy = ""; String uiElementValue = ""; for(Page page:XmlUtils.loadXml(path)) { String actualPageKeyword = page.getKeyword(); if (expectedPageKeyword.equals(actualPageKeyword)) { for (UIElement uiElement : page.getUiElementList()) { String actualUiElement = uiElement.getKeyword(); if (expectedUIElementKeyword.equals(actualUiElement)) { uiElementBy = uiElement.getBy(); uiElementValue = uiElement.getValue(); } } } } Class<By> clazz = By.class; Method method = clazz.getMethod(uiElementBy, String.class); By by =(By) method.invoke(null, uiElementValue); return by;}catch (Exception e) { e.printStackTrace(); } return null; } public static By getXmlBy(String expectedPageKeyword,String expectedUIElementKeyword) { try { String uiElementBy = ""; String uiElementValue = ""; for(Page page:XmlUtils.loadXml("")) { String actualPageKeyword = page.getKeyword(); if (expectedPageKeyword.equals(actualPageKeyword)) { for (UIElement uiElement : page.getUiElementList()) { String actualUiElement = uiElement.getKeyword(); if (expectedUIElementKeyword.equals(actualUiElement)) { uiElementBy = uiElement.getBy(); uiElementValue = uiElement.getValue(); } } } } Class<By> clazz = By.class; Method method = clazz.getMethod(uiElementBy, String.class); By by =(By) method.invoke(null, uiElementValue); return by; }catch (Exception e) { e.printStackTrace(); } return null; } //=======================================Js部分========================================================= /** * Selenium定位的元素被图层遮挡,click无法操作,可以用此方法点击 * @param webElement 需要操作的元素 */ public static void clickByJs(WebElement webElement) { logger.info(webElement.toString()); getJs().executeScript("arguments[0].click();", webElement); } /** * 去除元素只读属性 * @param webElement 需要操作的元素 */ public static void removeAttributeReadonly(WebElement webElement) { getJs().executeScript("arguments[0].removeAttribute('readonly')",webElement); } /** * 文本输入框赋值 * @param webElement 需要操作的元素 * @param input 期望输入的值 * 注意: JS企图去控制输入框的值失效,是因为没有触发input或change事件(有些前端会设置) */ public static void modifyValueByJs(WebElement webElement,String input) { getJs().executeScript("arguments[0].value=arguments[1]",webElement,input); } /** * 设置富文本框的内容 * @param webElement 需要操作的元素 * @param input 期望输入的值 */ public static void modifyInnerHTMLByJs(WebElement webElement,String input) { getJs().executeScript("arguments[0].innerHTML = arguments[1]" ,webElement,input); } /** * 利用js代码取出关键字 * @param webElement 需要操作的元素 * @return 返回获取的value值 */ public static String getValueByJs(WebElement webElement) { return (String) getJs().executeScript("var result=arguments[0].value;return result",webElement); } /** * 用js判断页面加载完毕,返回complete * @return 返回complete */ public static String judgeLoadComplete() { return getJs().executeScript("return document.readyState").toString(); } /** * 获取富文本框内容 * @param webElement 需要操作的元素 * @return 返回获取的innerHTML */ public static String getInnerHTMLByJs(WebElement webElement) { return (String) getJs().executeScript("var result=arguments[0].innerHTML;return result" ,webElement); } /** * 滑动页面到指定元素的顶部对其 * @param webElement 需要操作的元素 */ public static void scrollIntoTopView(WebElement webElement) { getJs().executeScript("arguments[0].scrollIntoView(true);",webElement); } /** * 滑动页面到指定元素的顶部底部 * @param webElement 需要操作的元素 */ public static void scrollInTobottomView(WebElement webElement) { getJs().executeScript("arguments[0].scrollIntoView(false);",webElement); } /** * 元素滚动至可见位置_如果当前可见,则不会发生滚动 * @param webElement 需要操作的元素 */ public static void scrollIntoViewIfNeeded(WebElement webElement) { getJs().executeScript("arguments[0].scrollIntoViewIfNeeded(true);",webElement); } /** * 封装设置或者新增页面对象的属性值的方法 调用JS代码修改页面元素的属性值,arguments[0]~arguments[1]分别 * 会用后面的element,attributeName和value参数进行替换 * @param webElement 需要操作的元素 * @param attributeName 属性名 * @param attributeValue 属性值 */ public static void setAttribute(WebElement webElement,String attributeName,String attributeValue) { getJs().executeScript("arguments[0].setAttribute(\""+attributeName+"\",\""+attributeValue+"\")",webElement,attributeName,attributeValue); } /** * 删除元素指定元素属性 * @param webElement 需要操作的元素 * @param attributeName 属性名 */ public static void removeAttribute(WebElement webElement,String attributeName) { getJs().executeScript("arguments[0].removeAttribute(arguments[1])",webElement,attributeName); } /** * 获取指定元素对应的属性值 * @param webElement 需要操作的元素 * @param attributeName 属性名 * @return 返回指定元素对应的属性值 */ public static String getAttribute(WebElement webElement,String attributeName) { logger.info("浏览器信息和获取的元素信息:【"+webElement.toString()+"】,获取该元素属性名为【"+attributeName+"】,属性值值为【"+(String)getJs().executeScript("return arguments[0].getAttribute(arguments[1])",webElement,attributeName)+"】"); return (String)getJs().executeScript("return arguments[0].getAttribute(arguments[1])",webElement,attributeName); } /** * 元素高亮展示 #边框border:2px; blue红色 * @param webElement 需要操作的元素 */ public static void setHighLight(WebElement webElement) { logger.info("浏览器信息和获取的元素信息:【"+webElement.toString()+"】,设置了【style】属性,其属性值为【border: 10px solid blue;】,其目的是高亮显示元素"); setAttribute(webElement, "style", "border: 10px solid blue;"); } //=======================================正则表达式替换修改xml部分=========================================== /** * 替换${格式的信息} * @param expectedPageKeyword * @param expectedUIElementKeyword * @param modifyValue */ public static void replaceValue(String expectedPageKeyword,String expectedUIElementKeyword,String modifyValue) { // XmlUtils.(expectedPageKeyword, expectedUIElementKeyword, modifyValue, "\\$\\{.+\\}"); } /** * 替换中文字符为S{XXX} * @param expectedPageKeyword * @param expectedUIElementKeyword * @param modifyValue */ public static void replaceChinese(String expectedPageKeyword,String expectedUIElementKeyword,String modifyValue) { // XmlUtil.modifyXmlInfo(expectedPageKeyword, expectedUIElementKeyword, modifyValue, "[\\x{4e00}-\\x{9fa5}]+"); } //机动车综合险2020版 /** * 将中文字符替换为 S{XXX} * @param expectedPageKeyword * @param expectedUIElementKeyword * @param modifyValue */ public static void replaceChineseNum(String expectedPageKeyword,String expectedUIElementKeyword,String modifyValue) { // XmlUtil.modifyXmlInfo(expectedPageKeyword, expectedUIElementKeyword, modifyValue, "[0-9\\x{4e00}-\\x{9fa5}]+"); } /** * 匹配中文和带-的 * @param expectedPageKeyword * @param expectedUIElementKeyword * @param modifyValue */ public static void replaceChineseNum1(String expectedPageKeyword,String expectedUIElementKeyword,String modifyValue) { // XmlUtil.modifyXmlInfo(expectedPageKeyword, expectedUIElementKeyword, modifyValue, "[\\\\-\\x{4e00}-\\x{9fa5}]+"); } }
package common;
import java.awt.AWTException;import java.awt.Robot;import java.awt.event.KeyEvent;import java.lang.reflect.Method;import java.text.SimpleDateFormat;import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.List;import java.util.Random;import java.util.Set;import java.util.regex.Pattern;
import org.apache.log4j.Logger;import org.apache.poi.poifs.crypt.dsig.KeyInfoKeySelector;import org.apache.tika.metadata.WordPerfect;import org.openqa.selenium.Alert;import org.openqa.selenium.By;import org.openqa.selenium.Cookie;import org.openqa.selenium.JavascriptExecutor;import org.openqa.selenium.Keys;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.interactions.Actions;import org.openqa.selenium.interactions.SendKeysAction;import org.openqa.selenium.support.ui.ExpectedCondition;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.Select;import org.openqa.selenium.support.ui.WebDriverWait;import org.testng.Assert;
import pojo.Page;import pojo.UIElement;import utils.BrowserUtils;import utils.RegexUtils;import utils.XmlUtils;
public class BasePage {
private final static Logger logger = Logger.getLogger(BasePage.class);
/** * 获取浏览器 * @return浏览器 */public static WebDriver getBroswer() {return BrowserUtils.getDriver();}/** * 获取JavascriptExecutor * @returnJavascriptExecutor */public static JavascriptExecutor getJs() {return BrowserUtils.getJavascriptExecutor();}/** * 获取WebDriverWait * @returnWebDriverWait */public static WebDriverWait getWait() {return BrowserUtils.getWebDriverWait();}/** * 创建本地浏览器或者远程浏览器 * @param typetype→本地/远程 * @param browserType浏览器类型 */public static void createBroswer(String type,String browserType) {logger.info("================================打开"+type+"浏览器("+browserType+") ========================================");if (type.contains("本地")) {BrowserUtils.openBrowser(browserType);;}else if (type.contains("远程")) {BrowserUtils.openRemoteBrowser(browserType);}}/** * 创建本地浏览器 * @param browserType浏览器类型 */public static void createBrowser(String browserType) {logger.info("================================打开远程端浏览器("+browserType+") ========================================");BrowserUtils.openBrowser(browserType);}/** * 创建远程浏览器 * @param browserType浏览器类型 */public static void createRemoteBrowser(String browserType) {logger.info("================================打开本地浏览器("+browserType+") ========================================");BrowserUtils.openRemoteBrowser(browserType);}/** * 清楚缓存 */public static void ClearCache () { logger.info("================================开始清除缓存 ========================================"); Set<Cookie> coo = getBroswer().manage().getCookies(); //打印Cookie logger.info("cookies信息:【"+coo.toString()+"】"); //清除所有的缓存 getBroswer().manage().deleteAllCookies(); }/** * 跳转到指定的url * @param url需跳转的url地址 */public static void getUrl(String url) {logger.info("浏览器跳转到:【"+url+"】");getBroswer().get(url);
}
/** * 硬性等待 * @param time毫秒 */public static void waitTime(int time) {try {logger.info("线程等待:【"+(double)time/1000+"】秒");Thread.sleep(time);} catch (InterruptedException e) {e.printStackTrace();}}/** * 最大化窗口 */public static void maxWindow() {logger.info("window窗口最大化");getBroswer().manage().window().maximize();}/** * 根据获取的标题来切换句柄 获取的标题包含预期的标题才能切换 * @param title标题 */public void switchWindowByTitle(String title) {Set<String> handles = getBroswer().getWindowHandles();logger.info("获取【handles大小】为:【"+handles.size()+"】,【获取的handles】为:【"+handles.toString()+"】");for (String handle : handles) {if (getBroswer().getTitle().contains(title)) {logger.info("当前句柄的【标题】:【"+getBroswer().getTitle()+"】,符合预期的标题,切换成功");break;}else {logger.info("当前句柄的【标题】:【"+getBroswer().getTitle()+"】,切换句柄,寻找符合标题的句柄");getBroswer().switchTo().window(handle);}}}//====================================================================================================================//====================================================================================================================//====================================================================================================================//====================================================================================================================//====================================================================================================================//====================================================================================================================//====================================================================================================================//====================================================================================================================public static void main(String[] args) {createBrowser( "chrome");maxWindow();getBroswer().manage().deleteAllCookies();getBroswer().get("http://egis-cspi-padis-dmzstg1.pingan.com.cn/cspi-internet/palogin.jsp");input(By.cssSelector("#userName"), "01713801151301");input(By.cssSelector("#passwordInput"), "a8888888");try {Thread.sleep(25000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}//if (assertEleContains(By.cssSelector("#center"), "没有找到相应信息")) {//getBroswer().navigate().refresh();//waitTime(2000);//getBroswer().switchTo().alert().accept();//waitTime(2000);//input(By.cssSelector("#passwordInput"), "a8888888");//try {//Thread.sleep(25000);//} catch (InterruptedException e) {//// TODO Auto-generated catch block//e.printStackTrace();//}//}getBroswer().switchTo().defaultContent();try {getBroswer().switchTo().frame(getBroswer().findElement(By.cssSelector("#EM_PAGE")));}catch(Exception e) {if (assertEleContains(By.cssSelector("#center"), "没有找到相应信息")) {getBroswer().navigate().refresh();waitTime(2000);boolean s ;try {s = assertEleContains(By.cssSelector("#warmTips"), "温馨提示");} catch (Exception e2) {s = false;}if (s){getBroswer().switchTo().frame(getBroswer().findElement(By.cssSelector("#EM_PAGE")));getBroswer().switchTo().frame(getBroswer().findElement(By.cssSelector("#content")));}else {getBroswer().switchTo().alert().accept();waitTime(2000);input(By.cssSelector("#passwordInput"), "a8888888");try {Thread.sleep(25000);} catch (InterruptedException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}getBroswer().switchTo().frame(getBroswer().findElement(By.cssSelector("#EM_PAGE")));getBroswer().switchTo().frame(getBroswer().findElement(By.cssSelector("#content")));}}/** * 点击提示关闭按钮 */waitVisibilityOfElementLocated(By.cssSelector("#warmTips #closeWarmTips"));clickByJs(getBroswer().findElement(By.cssSelector("#warmTips #closeWarmTips")));/** * 通过键盘按钮点击回车键 */try {Thread.sleep(6000);Robot robot = new Robot();robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); robot.delay(500); } catch (Exception e1) {// TODO Auto-generated catch blocke1.printStackTrace();}/** * 点击alter弹框 *///getBroswer().switchTo().alert().accept();/** * 点击提示关闭按钮 */clickByJs(waitVisibilityOfElementLocated(By.cssSelector("#warmTips #closeWarmTips")));getBroswer().switchTo().defaultContent();getBroswer().switchTo().frame(getBroswer().findElement(By.cssSelector("#EM_PAGE")));/** * 点击悬浮保全处理查询 */new Actions(getBroswer()) .moveToElement(waitVisibilityOfElementLocated(By.xpath("//a[contains(text(),'保全处理查询')]"))).perform();/** * 点击余额管理 */clickByJs(waitVisibilityOfElementLocated(By.xpath("//a[contains(text(),'余额管理')]")));getBroswer().switchTo().frame(getBroswer().findElement(By.cssSelector("#content")));/** * 保单号 */String policyNo = "GP02002024242842";/** * 输入保单号 */waitVisibilityOfElementLocated(By.cssSelector("#queryPolNoT")).sendKeys(policyNo);/** * 点击查询 */waitElementToBeClickable(By.cssSelector("[onclick*='queryPolInfoByPolNo()']")).click();String beforeText = waitVisibilityOfElementLocated(By.xpath("//th[contains(text(),'保单号')]/parent::tr/following-sibling::tr")).getText();String beforeMOney = RegexUtils.regexSingleMatch("(?<=\\s)\\d+\\.\\d+(?=\\s)", beforeText);Double beforeMOney1 = Double.parseDouble(beforeMOney);System.out.println(beforeText+" "+beforeMOney1);/** * 验证查询后数据是否正确 assertPolInfoLink * 是否包含查询的保单号 */boolean assertContains = assertRowContains(By.xpath("//th[contains(text(),'保单号')]/parent::tr/following-sibling::tr"),policyNo);/** * 查询出来的保单号是否唯一 */System.out.println("保单查询后是否包含:【"+assertContains+"】");boolean assertInfoCountOne = assertInfoCountOne(By.xpath("//th[contains(text(),'保单号')]/parent::tr/following-sibling::tr") );System.out.println("保单查询后是否唯一:【"+assertInfoCountOne+"】");
/** * 点击保单链接 */waitElementToBeClickable(By.cssSelector("a[onclick*=displayDetail]")).click();/** * 验证保单链接功能 * 包含团体保单详情页 * 保单信息 * 投保人信息 * 保障信息 * 层级信息 */boolean assertEleContains2 = assertEleContains(By.cssSelector("#dialogContent.process-content "), "block", "style");System.out.println("是否包含属性值:"+assertEleContains2);boolean assertEleContains = assertEleContains(By.cssSelector("#dialogContent.process-content "),"团体保单详情页" );System.out.println("元素文本值是否包含:"+assertEleContains);assertEleContains = assertEleContains(By.cssSelector("#dialogContent.process-content "),"保单信息" );System.out.println("元素文本值是否包含:"+assertEleContains);assertEleContains = assertEleContains(By.cssSelector("#dialogContent.process-content "),"投保人信息" );System.out.println("元素文本值是否包含:"+assertEleContains);assertEleContains = assertEleContains(By.cssSelector("#dialogContent.process-content "),"保障信息" );System.out.println("元素文本值是否包含:"+assertEleContains);assertEleContains = assertEleContains(By.cssSelector("#dialogContent.process-content "),"层级信息" );System.out.println("元素文本值是否包含:"+assertEleContains);/** * 滑动到层级信息 */scrollIntoTopView(waitPresenceOfElementLocated(By.xpath("//*[contains(text(),'层级信息')]")));waitTime(5000);System.out.println(" 已经滑懂了");//scrollInTobottomView(waitPresenceOfElementLocated(By.xpath("//*[contains(text(),'层级信息')]")));//waitTime(5000);
/** * 点击职业链接 */waitElementToBeClickable(By.cssSelector("[onclick*='showProfCode']")).click();/** * 点击职业层级链接后显示的层级详情 */assertEleContains2 = assertEleContains(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]"), "", "style");System.out.println("是否包含属性值:"+assertEleContains2);assertEleContains2 = assertEleContains(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]"), "职业描述");System.out.println("是否包含文本值:"+assertEleContains2);assertEleContains2 = assertEleContains(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]"), "职业代码");System.out.println("是否包含文本值:"+assertEleContains2);assertEleContains2 = assertInfoCountBigOne(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]//td[2][not(contains(text(),'职业'))]/parent::tr"));System.out.println("元素是否大于等于1个元素:"+assertEleContains2);/** * 点击层级明细链接 * showDetailNew */waitElementToBeClickable(By.cssSelector("[onclick*='showDetailNew']")).click();assertEleContains2 = assertEleContains(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]"), "", "style");System.out.println("是否包含属性值:"+assertEleContains2);assertEleContains2 = assertEleContains(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]"), "保险金额");System.out.println("是否包含文本值:"+assertEleContains2);assertEleContains2 = assertEleContains(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]"), "赔付比例");System.out.println("是否包含文本值:"+assertEleContains2);assertEleContains2 = assertInfoCountBigOne(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]//td[2][not(contains(text(),'保险'))]/parent::tr"));System.out.println("元素是否大于等于1个元素:"+assertEleContains2);/** * 点击关闭保单详情按钮 */clickByJs(waitElementToBeClickable(By.cssSelector("[onclick*='closetxt']")));/** * 点击转出按钮 */clickByJs(waitElementToBeClickable(By.cssSelector("[onclick*='queryFundAmount']")));/** * 验证传出按钮是否跳余额转出页面 */assertEleContains2 = assertEleContains(By.cssSelector("div#returnsDiv"), "单位对公账户");System.out.println("是否包含文本值:"+assertEleContains2);assertEleContains2 = assertEleContains(By.cssSelector("div#returnsDiv"), "余额转出明细");System.out.println("是否包含文本值:"+assertEleContains2);/** * 点击余额转出明细 */click(By.cssSelector("[onclick*='historyRefundData']"));/** * 输入 余额转出明细 - 开始日期输入框 */modifyValueByJs(waitVisibilityOfElementLocated(By.cssSelector("input#beginDate")), "2021-01-01");/** * 输入 余额转出明细 - 结束日期输入框 */modifyValueByJs(waitVisibilityOfElementLocated(By.cssSelector("input#endDate")), "2021-03-13");/** * 选择 余额转出明细 - 转出状态 */new Select(waitVisibilityOfElementLocated(By.cssSelector("select#applyStatus"))).selectByValue("P");/** * 点击余额转出明细 - 点击查询 */clickByJs(waitVisibilityOfElementLocated(By.cssSelector("[onclick*='submitQuery']")));//click(By.cssSelector("[onclick*='submitQuery']"));/** * 断言查询后展示的数>=1 */waitTime(2000);assertEleContains2 = assertInfoCountBigOne(By.cssSelector(".use_money_tr"));System.out.println("元素是否大于等于1个元素:"+assertEleContains2);/** * 判断余额转出明细详情页 是否包含 div#history_data_display * 流水号 * 转移金额 */assertEleContains2 = assertEleContains(By.cssSelector("div#history_data_display"), "流水号");System.out.println("是否包含文本值:"+assertEleContains2);assertEleContains2 = assertEleContains(By.cssSelector("div#history_data_display"), "转移金额");System.out.println("是否包含文本值:"+assertEleContains2);/** * 余额转出明细详情页 返回按钮[onclick*='goBack()'] */click(By.cssSelector("[onclick*='goBack()']"));/** * 点击 单位对公账户链接 */click(By.cssSelector("[onclick*='setBankAcounts']"));/** * 单位对公账户链接 后显示的用户须知 */assertEleContains2 = assertEleContains(By.cssSelector("div#infoDiv"),"用户告知");System.out.println("是否包含文本值:"+assertEleContains2);assertEleContains2 = assertEleContains(By.cssSelector("div#infoDiv"),"注意事项");System.out.println("是否包含文本值:"+assertEleContains2);assertEleContains2 = assertEleContains(By.cssSelector("div#infoDiv"),"责任免除");/** * 单位对公账户链接 接受按钮 */click(By.cssSelector("[onclick*='goToNext']"));/** * 账户信息维护页-开户银行 */waitVisibilityOfElementLocated(By.cssSelector("#bankName"));//input(By.cssSelector("input#bankName"), "a");try {Robot r = new Robot();waitTime(1000);r.keyPress(KeyEvent.VK_TAB);r.keyRelease(KeyEvent.VK_TAB);r.delay(200);waitTime(1000);r.keyPress(KeyEvent.VK_TAB);r.keyRelease(KeyEvent.VK_TAB);r.delay(200);waitTime(1000);r.keyPress(KeyEvent.VK_B);r.keyRelease(KeyEvent.VK_B);r.delay(200);waitTime(1000);waitTime(1000);r.keyPress(KeyEvent.VK_ENTER);r.keyRelease(KeyEvent.VK_ENTER);r.delay(200);waitTime(1000);r.keyPress(KeyEvent.VK_DOWN);r.keyRelease(KeyEvent.VK_DOWN);r.delay(200);waitTime(1000);r.keyPress(KeyEvent.VK_ENTER);r.keyRelease(KeyEvent.VK_ENTER);r.delay(200);waitTime(1000);
/** * 输入账户信息维护-分行 */waitTime(1000);r.keyPress(KeyEvent.VK_TAB);r.keyRelease(KeyEvent.VK_TAB);r.delay(200);waitTime(1000);waitTime(1000);r.keyPress(KeyEvent.VK_B);r.keyRelease(KeyEvent.VK_B);r.delay(200);waitTime(1000);r.keyPress(KeyEvent.VK_ENTER);r.keyRelease(KeyEvent.VK_ENTER);r.delay(200);waitTime(1000);waitTime(1000);r.keyPress(KeyEvent.VK_DOWN);r.keyRelease(KeyEvent.VK_DOWN);r.delay(200);waitTime(1000);r.keyPress(KeyEvent.VK_ENTER);r.keyRelease(KeyEvent.VK_ENTER);r.delay(200);waitTime(1000);
/** * 账户信息维护-支行 * 输入支行 */input(By.cssSelector("#subbranchBank"), "自动化支行"+new Random().nextInt(1000));/** * 账户信息维护-银行账户 * bankAccount */input(By.cssSelector("#bankAccount"), "6217855300003734772");/** * 点击账户信息维护-确认按钮 doSaveButton */click(By.cssSelector("#doSaveButton"));/** * 点击账户信息维护-弹框确定按钮 */boolean assertAlterContains = assertAlterContains("银行账号信息保存成功");System.out.println("assertAlterContains:"+assertAlterContains);waitTime(1000);r.keyPress(KeyEvent.VK_ENTER);r.keyRelease(KeyEvent.VK_ENTER);r.delay(200);waitTime(1000);} catch (Exception e2) {// TODO Auto-generated catch blocke2.printStackTrace();}
/** * 点击转出按钮 */clickByJs(waitElementToBeClickable(By.cssSelector("[onclick*='queryFundAmount']")));/** * 点击 单位对公账户链接 */click(By.cssSelector("[onclick*='setBankAcounts']"));/** * 单位对公账户链接 接受按钮 */click(By.cssSelector("[onclick*='goToNext']"));/** * 历史维护账户信息 历史维护账户信息按钮 */click(By.cssSelector("[onclick*='queryHistoryBank']"));/** * 历史维护账号信息-历史信息勾选框 * 判断是否包含页面带出历史账户信息提示 */boolean assertEleContains3 = assertEleContains(By.cssSelector("#bankDiv2"),"页面带出历史账户信息");System.out.println(assertEleContains3);/** * 判断历史账户信息行唯一 */boolean assertInfoCountOne2 = assertInfoCountOne(By.cssSelector("#bankDiv2 [id*='items']"));System.out.println("assertInfoCountOne2:"+assertInfoCountOne2);String text = waitVisibilityOfElementLocated(By.cssSelector("#bankno1")).getText();System.out.println(text);/** * 点击历史维护账号信息-历史账户勾选框 */click(By.cssSelector("#check1"));waitTime(2000);/** * 断言历史维护账号信息-跳转到 历史维护账号信息 */boolean assertEleContains4 = assertEleContains(By.cssSelector("#bankDiv"),"账户信息维护");System.out.println(assertEleContains4);/** * 账户信息维护-确认按钮 * */click(By.cssSelector("#doSaveButton"));/** * 点击账户信息维护-弹框确定按钮 */boolean assertAlterContains = assertAlterContains("银行账号信息保存成功");System.out.println("assertAlterContains:"+assertAlterContains);waitTime(1000);Robot r;try {r = new Robot();r.keyPress(KeyEvent.VK_ENTER);r.keyRelease(KeyEvent.VK_ENTER);r.delay(200);waitTime(1000);} catch (Exception e3) {// TODO Auto-generated catch blocke3.printStackTrace();}/** * 点击转出按钮 */clickByJs(waitElementToBeClickable(By.cssSelector("[onclick*='queryFundAmount']")));/** * 余额转出-可用转出金额输入框 */String refundMoney = "1";modifyValueByJs(waitVisibilityOfElementLocated(By.cssSelector("input#amount")), refundMoney);/** * [onclick*='callRefundSubmit'] * 余额转出-确定转出按钮 */click(By.cssSelector("[onclick*='callRefundSubmit']"));/** * 判断余额转出-确定转出后-处理完成提示框是否包含转出的价格 * */boolean assertEleContains5 = assertEleContains(By.cssSelector("div#suPag"),"余额"+refundMoney+"元");System.out.println("assertEleContains5:"+assertEleContains5);/** * 余额转出-确定转出后-处理完成提示框-确定按钮 */click(By.cssSelector("[onclick*='gotoView']"));/** * 余额管理-转入按钮 */click(By.cssSelector("[onclick*='findUseableBalacePol']"));/** * 余额管理-余额转入 * detail_useable_div * 判断 余额转入是否包含 余额转入详情页 */assertEleContains(By.cssSelector("div#detail_useable_div"),"余额转入");assertEleContains(By.cssSelector("div#detail_useable_div"),policyNo);assertEleContains(By.cssSelector("div#detail_useable_div"),"转移余额");assertEleContains(By.cssSelector("div#detail_useable_div"),"可用余额");/** * 余额管理-余额转入-余额互转明细链接 */click(By.cssSelector("[onclick*='historyData']"));//====================================================/** * 输入 余额互转明细 - 开始日期输入框 */modifyValueByJs(waitVisibilityOfElementLocated(By.cssSelector("input#applyBeginDate")), "2021-01-01");/** * 输入 余额互转明细 - 结束日期输入框 */modifyValueByJs(waitVisibilityOfElementLocated(By.cssSelector("input#applyEndDate")), "2021-03-13");/** * 选择 余额互转明细 - 支付模式 */new Select(waitVisibilityOfElementLocated(By.cssSelector("select#payMode"))).selectByValue("2");/** * 点击余额互转明细 - 点击查询 */clickByJs(waitVisibilityOfElementLocated(By.cssSelector("[onclick*='histMovePage']")));//click(By.cssSelector("[onclick*='submitQuery']"));/** * 断言查询后展示的数>=1 */waitTime(2000);assertEleContains2 = assertInfoCountBigOne(By.cssSelector(".use_money_tr"));System.out.println("元素是否大于等于1个元素:"+assertEleContains2);/** * 判断余额转出明细详情页 是否包含 div#history_data_display * 流水号 * 转移金额 */assertEleContains2 = assertEleContains(By.cssSelector("div#history_data_display"), "流水号");System.out.println("是否包含文本值:"+assertEleContains2);assertEleContains2 = assertEleContains(By.cssSelector("div#history_data_display"), "转移金额");System.out.println("是否包含文本值:"+assertEleContains2);/** * 余额管理查询页 - 查询结果行boolean assertContains = assertRowContains(By.cssSelector("#user_pol_div_id .img-position tr"),policyNo); */String text2 = waitVisibilityOfElementLocated(By.xpath("//th[contains(text(),'保单号')]/parent::tr/following-sibling::tr")).getText();String regexSingleMatch = RegexUtils.regexSingleMatch("(?<=\\s)\\d+\\.\\d+(?=\\s)", text2);Double d = Double.parseDouble(regexSingleMatch);System.out.println(text2+" "+d);if (beforeMOney1==(d+Double.parseDouble(refundMoney))) {System.out.println("true");}else {System.out.println("false");
}/** * 点击余额互转明细 - 返回 ackWay(); */clickByJs(waitVisibilityOfElementLocated(By.cssSelector("[onclick*='backWay']")));/** * 暂时没有转入订单 余额转入-确定转入按钮 */clickByJs(waitVisibilityOfElementLocated(By.cssSelector("[onclick*='submitConvertMoney']")));
}
}//====================================================================================================================//====================================================================================================================//====================================================================================================================//====================================================================================================================//====================================================================================================================//====================================================================================================================//====================================================================================================================//====================================================================================================================
public void yuEGuanLiBase() {}//====================================================================================================================//====================================================================================================================//====================================================================================================================//====================================================================================================================//====================================================================================================================//====================================================================================================================//====================================================================================================================//====================================================================================================================
/** * 判断弹框文本是否包含关键字 * @param key * @return包含返回true,否则false */private static boolean assertAlterContains(String key) {waitTime(3000);System.out.println(getBroswer().switchTo().alert().getText());return getBroswer().switchTo().alert().getText().contains(key);}
/** * 判断元素的属性值是否包含预期的属性值 * @param by * @param value * @param name * @return 包含返回true,不包含返回false */public static boolean assertEleContains(By by,String value,String name) {return waitVisibilityOfElementLocated(by).getAttribute(name).contains(value);}
/** * 判断元素的文本是否包含关键字 * @param by * @param key * @return包含返回true,不包含返回false */public static boolean assertEleContains(By by,String key) {return waitVisibilityOfElementLocated(by).getText().contains(key);}
/** * 判断所有元素获取的文本是否包含指定关键字 * @param by * @param key * @return包含返回true,不包含false */private static boolean assertRowContains(By by,String key) {List<WebElement> list = waitVisibilityOfAllElementsLocatedBy(by);for (WebElement webElement : list) {if (webElement.getText().contains(key)) {return true;}}return false;}/** * 判断所有元素个数是否一个 * @param by * @param key * @return是一个true,不是唯一返回false */private static boolean assertInfoCountOne(By by) {List<WebElement> list = waitVisibilityOfAllElementsLocatedBy(by);if (list.size()==1) {return true;}return false;}/** * 判断所有元素个数是否大于等于一个 * @param by * @param key * @return是一个true,不是唯一返回false */private static boolean assertInfoCountBigOne(By by) {List<WebElement> list = waitVisibilityOfAllElementsLocatedBy(by);if (list.size()>=1) {return true;}return false;}/** * 根据获取的url来切换句柄 获取的标题包含预期的标题才能切换 * @param url标题 */public void switchWindowByCurrentUrl(String url) {Set<String> handles = getBroswer().getWindowHandles();logger.info("获取【handles大小】为:【"+handles.size()+"】,【获取的handles】为:【"+handles.toString()+"】");for (String handle : handles) {if (getBroswer().getCurrentUrl().contains(url)) {logger.info("当前句柄的【url】:【"+getBroswer().getCurrentUrl()+"】,符合预期的标题,切换成功");break;}else {logger.info("当前句柄的【url】:【"+getBroswer().getCurrentUrl()+"】,切换句柄,寻找符合标题的句柄");getBroswer().switchTo().window(handle);}}}/** * 等待单个元素存在在dom对象中,但不一定可见 * @param locatorBy 对象定位信息 * @return返回存在dom 单个WebElement中元素,否则抛出异常 */public static WebElement waitPresenceOfElementLocated(By locator) {logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,等待元素在dom中存在");logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.presenceOfElementLocated(locator)).toString()+"】");return getWait().until(ExpectedConditions.presenceOfElementLocated(locator));}/** * 等待单个元素存在在dom对象中,但不一定可见 * @param expectedPageKeyword预期页面关键在 * @param expectedUIElementKeyword预期元素关键字 * @return返回存在dom 单个WebElement中元素,否则抛出异常 */public static WebElement waitPresenceOfElementLocated(String expectedPageKeyword,String expectedUIElementKeyword) {logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,等待元素在dom中存在");By by = getXmlBy(expectedPageKeyword, expectedUIElementKeyword);logger.info("获取【元素定位信息】为:【"+by.toString()+"】");logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.presenceOfElementLocated(by)).toString()+"");return getWait().until(ExpectedConditions.presenceOfElementLocated(by));}/** * 等待多个个元素存在在dom对象中,但不一定可见 * @param expectedPageKeyword预期页面关键在 * @param expectedUIElementKeyword预期元素关键字 * @return 返回存在dom 多个WebElement中元素-List<WebElement>,否则抛出异常 */public static List<WebElement> waitPresenceOfAllElementsLocatedBy(String expectedPageKeyword,String expectedUIElementKeyword) {logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,等待元素们在dom中存在");By by = getXmlBy(expectedPageKeyword, expectedUIElementKeyword);logger.info("获取【元素定位信息】为:【"+by.toString()+"】");logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.presenceOfAllElementsLocatedBy(by)).toString()+"】,其个数为【"+getWait().until(ExpectedConditions.presenceOfAllElementsLocatedBy(by)).size()+"】");return getWait().until(ExpectedConditions.presenceOfAllElementsLocatedBy(by));}/** * 等待多个个元素存在在dom对象中,但不一定可见 * @param locatorBy 对象定位信息 * @return 返回存在dom 多个WebElement中元素-List<WebElement>,否则抛出异常 */public static List<WebElement> waitPresenceOfAllElementsLocatedBy(By locator) {logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,等待元素们在dom中存在");logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.presenceOfAllElementsLocatedBy(locator)).toString()+"】,其个数为【"+getWait().until(ExpectedConditions.presenceOfAllElementsLocatedBy(locator)).size()+"】");return getWait().until(ExpectedConditions.presenceOfAllElementsLocatedBy(locator));}//===================/** * 等待单个元素在页面可见 * @param locatorBy 对象定位信息 * @return返回页面可见的 单个WebElement中元素,否则抛出异常 */public static WebElement waitVisibilityOfElementLocated(By locator) {logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,等待元素在页面可见");logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.visibilityOfElementLocated(locator)).toString()+"】");return getWait().until(ExpectedConditions.visibilityOfElementLocated(locator));}/** * 等待单个元素在页面可见 * @param expectedPageKeyword预期页面关键在 * @param expectedUIElementKeyword预期元素关键字 * @return返回页面可见的 单个WebElement中元素,否则抛出异常 */public static WebElement waitVisibilityOfElementLocated(String expectedPageKeyword,String expectedUIElementKeyword) {logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,等待元素在在页面可见");By by = getXmlBy(expectedPageKeyword, expectedUIElementKeyword);logger.info("获取【元素定位信息】为:【"+by.toString()+"】");logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.visibilityOfElementLocated(by)).toString()+"");return getWait().until(ExpectedConditions.visibilityOfElementLocated(by));}/** * 等待单个元素在页面可见 * @param element需要等待的元素 * @return返回页面可见的 单个WebElement中元素,否则抛出异常 */public static WebElement waitVisibilityOfElementLocated(WebElement element) {logger.info("获取【元素】为:【"+element.toString()+"】,等待页面可见");return getWait().until(ExpectedConditions.visibilityOf(element));}/** * 等待多个个元素在页面可见 * @param expectedPageKeyword预期页面关键在 * @param expectedUIElementKeyword预期元素关键字 * @return 返回存在页面 多个WebElement中元素-List<WebElement>,否则抛出异常 */public static List<WebElement> waitVisibilityOfAllElementsLocatedBy(String expectedPageKeyword,String expectedUIElementKeyword) {logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,等待元们素在页面可见");By by = getXmlBy(expectedPageKeyword, expectedUIElementKeyword);logger.info("获取【元素定位信息】为:【"+by.toString()+"】");logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.visibilityOfAllElementsLocatedBy(by)).toString()+"】,其个数为【"+getWait().until(ExpectedConditions.visibilityOfAllElementsLocatedBy(by)).size()+"】");return getWait().until(ExpectedConditions.visibilityOfAllElementsLocatedBy(by));}/** * 等待多个个元素在页面可见 * @param locatorBy 对象定位信息 * @return 返回存在页面 多个WebElement中元素-List<WebElement>,否则抛出异常 */public static List<WebElement> waitVisibilityOfAllElementsLocatedBy(By locator) {logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,等待元素们在页面可见");logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator)).toString()+"】,其个数为【"+getWait().until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator)).size()+"】");return getWait().until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator));}/** * 等待多个个元素在页面可见 * @param locatorBy 对象定位信息 * @return 返回存在页面 多个WebElement中元素-List<WebElement>,否则抛出异常 */public static List<WebElement> waitVisibilityOfAllElementsLocatedBy(WebElement ... elements) {logger.info("获取【locator定位信息】为:【"+elements.toString()+"】,等待元素们在页面可见");logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.visibilityOfAllElements(elements)).toString()+"】,其个数为【"+getWait().until(ExpectedConditions.visibilityOfAllElements(elements)).size()+"】");return getWait().until(ExpectedConditions.visibilityOfAllElements(elements));}/** * 等待单个元素可被点击 * @param locatorBy 对象定位信息 * @return返回页面可点击单个WebElement中元素,否则抛出异常 */public static WebElement waitElementToBeClickable(By locator) {logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,等待元素可被点击");logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.elementToBeClickable(locator)).toString()+"】");return getWait().until(ExpectedConditions.elementToBeClickable(locator));}/** * 等待单个元素可被点击 * @param expectedPageKeyword预期页面关键在 * @param expectedUIElementKeyword预期元素关键字 * @return返回页面可点击单个WebElement中元素,否则抛出异常 */public static WebElement waitElementToBeClickable(String expectedPageKeyword,String expectedUIElementKeyword) {logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,等待元素可被点击");By by = getXmlBy(expectedPageKeyword, expectedUIElementKeyword);logger.info("获取【元素定位信息】为:【"+by.toString()+"】");logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.elementToBeClickable(by)).toString()+"");return getWait().until(ExpectedConditions.elementToBeClickable(by));}/** * 等待单个元素可被点击 * @param element需要等待的元素 * @return返回页面可点击单个WebElement中元素,否则抛出异常 */public static WebElement waitElementToBeClickable(WebElement element) {logger.info("获取【元素】为:【"+element.toString()+"】,等待元素可被点击");return getWait().until(ExpectedConditions.elementToBeClickable(element));}/** * 等待Frame可以跳转并跳转进去 * @param expectedPageKeyword预期页面关键在 * @param expectedUIElementKeyword预期元素关键字 * @return返回webDriver实例 */public static WebDriver waitFrameToBeAvailableAndSwitchToIt(String expectedPageKeyword,String expectedUIElementKeyword) {logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,等待Frame可以跳转并跳转进去");By by = getXmlBy(expectedPageKeyword, expectedUIElementKeyword);logger.info("获取【元素定位信息】为:【"+by.toString()+"】");logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(by)).toString()+"");return getWait().until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(by));}/** * 等待Frame可以跳转并跳转进去 * @param element需要等待的元素 * @return返回webDriver实例 */public static WebDriver waitFrameToBeAvailableAndSwitchToIt(WebElement element) {logger.info("获取【元素】为:【"+element.toString()+"】,等待Frame可以跳转并跳转进去");return getWait().until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(element));}/** * 等待Frame可以跳转并跳转进去 * @param frameLocator(id or name) * @return返回webDriver实例 */public static WebDriver waitFrameToBeAvailableAndSwitchToIt(String frameLocator) {logger.info("获取【frame (id or name)】为:【"+frameLocator+"】,等待Frame可以跳转并跳转进去");return getWait().until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frameLocator));}/** * 等待Frame可以跳转并跳转进去 * @param frameLocatorindex * @return返回webDriver实例 */public static WebDriver waitFrameToBeAvailableAndSwitchToIt(int frameLocator) {logger.info("获取【frame (index)】为:【"+frameLocator+"】,等待Frame可以跳转并跳转进去");return getWait().until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frameLocator));}/** * 等待Frame可以跳转并跳转进去 * @param locatorBy 对象定位信息 * @return返回webDriver实例 */public static WebDriver waitFrameToBeAvailableAndSwitchToIt(By locator) {logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,等待Frame可以跳转并跳转进去");logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(locator)).toString()+"】");return getWait().until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(locator));}/** * 点击某个元素 * @param locatorBy定位对象 */public static void click(By locator ) {try {logger.info("点击了等待可以点击的元素,其定位信息【"+locator.toString()+"】");waitElementToBeClickable(locator).click();} catch (Exception e) {try {logger.info("点击了等待可以点击的元素失败,通过js点击,其定位信息【"+locator.toString()+"】");clickByJs(waitPresenceOfElementLocated(locator));} catch (Exception e2) {e.printStackTrace();}}}
/** * 点击某个元素 * @param element元素对象 */public static void click(WebElement element ) {try {logger.info("点击了等待可以点击的元素,其元素信息【"+element.toString()+"】");waitElementToBeClickable(element).click();} catch (Exception e) {try {logger.info("点击了等待可以点击的元素失败,通过js点击,其元素信息【"+element.toString()+"】");clickByJs(element);} catch (Exception e2) {e.printStackTrace();}}}/** * 点击某个元素 * @param expectedPageKeyword预期页面 * @param expectedUIElementKeyword预期关键字 */public static void click(String expectedPageKeyword,String expectedUIElementKeyword ) {try {logger.info("点击了【关键页面】:【"+expectedPageKeyword+"】【关键元素】:【"+expectedUIElementKeyword+"】");waitElementToBeClickable(expectedPageKeyword, expectedUIElementKeyword).click();} catch (Exception e) {try {logger.info("点击了【关键页面】:【"+expectedPageKeyword+"】【关键元素】:【"+expectedUIElementKeyword+"】失败,现通过js点击");clickByJs(waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword));} catch (Exception e2) {e.printStackTrace();}}}/** * 输入信息 * @param locatorBy定位对象 * @param keysToSend输入的值 */public static void input(By locator,String keysToSend) {try {logger.info("对dom存在的元素进行输入,其定位信息【"+locator.toString()+"】,【输入的内容】为:【"+keysToSend+"】");waitPresenceOfElementLocated(locator).sendKeys(keysToSend);if (!checkTextToBePresentInElementValue(locator, keysToSend)) {logger.info("对dom存在的元素进行输入,输入的信息和value值不一致,通过js直接改变value值,其定位信息【"+locator.toString()+"】,【输入的内容】为:【"+keysToSend+"】");modifyValueByJs(waitVisibilityOfElementLocated(locator), keysToSend);}} catch (Exception e) {e.printStackTrace();}}
/** * 输入信息 * @param element元素对象 * @param keysToSend输入的值 */public static void input(WebElement element,String keysToSend ) {try {logger.info("对dom存在的元素进行输入,其定位信息【"+element.toString()+"】,【输入的内容】为:【"+keysToSend+"】");element.sendKeys(keysToSend);if (!checkTextToBePresentInElementValue(element, keysToSend)) {logger.info("对dom存在的元素进行输入,输入的信息和value值不一致,通过js直接改变value值,其定位信息【"+element.toString()+"】,【输入的内容】为:【"+keysToSend+"】");modifyValueByJs(element, keysToSend);}} catch (Exception e) {e.printStackTrace();}}/** * 点击某个元素 * @param expectedPageKeyword预期页面 * @param expectedUIElementKeyword预期关键字 * @param keysToSend输入的值 */public static void input(String expectedPageKeyword,String expectedUIElementKeyword,String keysToSend ) {try {logger.info("向【关键页面】:【"+expectedPageKeyword+"】【关键元素】:【"+expectedUIElementKeyword+"】输入【 "+keysToSend+"】");waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword).sendKeys(keysToSend);if (!checkTextToBePresentInElementValue(waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword), keysToSend)) {logger.info("对dom存在的元素进行输入,输入的信息和value值不一致,通过js直接改变value值,其关键页面【"+expectedPageKeyword+"】,【关键元素】:【"+ expectedUIElementKeyword+"】,【输入的内容】为:【"+keysToSend+"】");modifyValueByJs(waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword), keysToSend);}}catch (Exception e) {e.printStackTrace();}}/** * 取消选择所有select * @param locatorBy 对象定位信息 */public static void deselectAll(By locator) {logger.info("取消所有的select选项,select定位是:【"+locator.toString()+"】");Select select = new Select(waitPresenceOfElementLocated(locator));select.deselectAll();}
/** * 取消选择所有select * @param elementWebElement对象 */public static void deselectAll(WebElement element) {logger.info("取消所有的select选项,select定位是:【"+element.toString()+"】");Select select = new Select(element);select.deselectAll();}/** * 取消选择所有select * @param expectedPageKeyword预期页面 * @param expectedUIElementKeyword预期关键字 */public static void deselectAll(String expectedPageKeyword,String expectedUIElementKeyword) {logger.info("取消所有的select选项");Select select = new Select(waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword));select.deselectAll();}
/** * 通过索引来选择select * @param locatorBy 对象定位信息 * @param index索引 */public static void selectByIndex(By locator,int index) {logger.info("通过索引来选择select,select定位是:【"+locator.toString()+"】,【索引值】:【"+index+"】");Select select = new Select(waitPresenceOfElementLocated(locator));select.selectByIndex(index);}
/** * 通过索引来选择select * @param elementWebElement对象 * @param index索引 */public static void selectByIndex(WebElement element,int index) {logger.info("通过索引来选择select,select定位是:【"+element.toString()+"】,【索引值】:【"+index+"】");Select select = new Select(element);select.selectByIndex(index);}/** * 通过索引值来选择select * @param expectedPageKeyword预期页面 * @param expectedUIElementKeyword预期关键字 * @param index索引值 */public static void selectByIndex(String expectedPageKeyword,String expectedUIElementKeyword,int index) {logger.info("通过索引值来选择select,select所在关键页面是:【"+expectedPageKeyword+"】,所在关键元素是【"+expectedUIElementKeyword+"】,【索引值】:【"+index+"】");Select select = new Select(waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword));select.selectByIndex(index);}/** * 通过value值来选择select * @param locatorBy 对象定位信息 * @param valuevalue值 */public static void selectByValue(By locator,String value) {logger.info("通过value值来选择select,select定位是:【"+locator.toString()+"】,【value值值】:【"+value+"】");Select select = new Select(waitPresenceOfElementLocated(locator));select.selectByValue(value);}
/** * 通过value值来选择select * @param elementWebElement对象 * @param valuevalue值 */public static void selectByValue(WebElement element,String value) {logger.info("通过value值来选择select,select定位是:【"+element.toString()+"】,【value值值】:【"+value+"】");Select select = new Select(element);select.selectByValue(value);}/** * 通过value值来选择select * @param expectedPageKeyword预期页面 * @param expectedUIElementKeyword预期关键字 * @param valuevalue值 */public static void selectByValue(String expectedPageKeyword,String expectedUIElementKeyword,String value) {logger.info("通过value值来选择select,select所在关键页面是:【"+expectedPageKeyword+"】,所在关键元素是【"+expectedUIElementKeyword+"】,【value值值】:【"+value+"】");Select select = new Select(waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword));select.selectByValue(value);}///** * 通过可见文本值来选择select * @param locatorBy 对象定位信息 * @param text可见文本值 */public static void selectByVisibleText(By locator,String text) {logger.info("通过可见文本值来选择select,select定位是:【"+locator.toString()+"】,【可见文本值】:【"+text+"】");Select select = new Select(waitPresenceOfElementLocated(locator));select.selectByVisibleText(text);}
/** * 通过可见文本值来选择select * @param elementWebElement对象 * @param text可见文本值 */public static void selectByVisibleText(WebElement element,String text) {logger.info("通过可见文本值来选择select,select定位是:【"+element.toString()+"】,【可见文本值】:【"+text+"】");Select select = new Select(element);select.selectByVisibleText(text);}/** * 通过可见文本值来选择select * @param expectedPageKeyword预期页面 * @param expectedUIElementKeyword预期关键字 * @param text可见文本值 */public static void selectByVisibleText(String expectedPageKeyword,String expectedUIElementKeyword,String text) {logger.info("通过可见文本值来选择select,select所在关键页面是:【"+expectedPageKeyword+"】,所在关键元素是【"+expectedUIElementKeyword+"】,【可见文本值】:【"+text+"】");Select select = new Select(waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword));select.selectByVisibleText(text);}/** * 确认alter弹框 */public static void acceptAlert() {logger.info("确认alter弹框");Alert alert = getBroswer().switchTo().alert();alert.accept();}/** * 关闭浏览器 */public static void closeBrowser() {logger.info("关闭浏览器");getBroswer().quit();}/** * 取消alter弹框 */public static void dismissAlert() {logger.info("取消alter弹框");Alert alert = getBroswer().switchTo().alert();alert.dismiss();}
/** * 检查给定元素是否被选中的期望值。 * @param locatorBy 对象定位信息 * @param selected预期的选中状态 * @return返回true,实际选中状态和预期选中状态一致,反之返回false */public static Boolean checkElementSelectionStateToBe(By locator,boolean selected) {logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,检查select是否被选中,【预期选中状态】:【"+selected+"】");logger.info("返回的实际选中状态【"+ getWait().until(ExpectedConditions.elementSelectionStateToBe(locator, selected))+"】");return getWait().until(ExpectedConditions.elementSelectionStateToBe(locator, selected));}/** * 检查给定元素是否被选中的期望值。 * @param expectedPageKeyword预期页面关键在 * @param expectedUIElementKeyword预期元素关键字 * @return返回true,实际选中状态和预期选中状态一致,反之返回false */public static Boolean checkElementSelectionStateToBe(String expectedPageKeyword,String expectedUIElementKeyword,boolean selected ) {logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,检查select是否被选中");By locator = getXmlBy(expectedPageKeyword, expectedUIElementKeyword);logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,检查select是否被选中");logger.info("返回的实际选中状态【"+ getWait().until(ExpectedConditions.elementSelectionStateToBe(locator, selected))+"】");return getWait().until(ExpectedConditions.elementSelectionStateToBe(locator, selected));}/** * 检查给定元素是否被选中的期望值。 * @param element需要判断的元素 * @param selected预期的选中状态 * @return返回true,实际选中状态和预期选中状态一致,反之返回false */public static Boolean checkElementSelectionStateToBe(WebElement element,boolean selected) {logger.info("获取【element元素信息】为:【"+element.toString()+"】,检查select是否被选中,【预期选中状态】:【"+selected+"】");logger.info("返回的实际选中状态【"+ getWait().until(ExpectedConditions.elementSelectionStateToBe(element, selected))+"】");return getWait().until(ExpectedConditions.elementSelectionStateToBe(element, selected));}/** * 检查给定元素是否被选中。 * @param locatorBy 对象定位信息 * @return已选择返回true,反之返回false */public static Boolean checkElementToBeSelected(By locator) {logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,检查select被选中");logger.info("返回的实际选中状态【"+ getWait().until(ExpectedConditions.elementToBeSelected(locator))+"】");return getWait().until(ExpectedConditions.elementToBeSelected(locator));}/** * 检查给定元素是否被选中。 * @param element需要判断的元素 * @param selected预期的选中状态 * @return已选择返回true,反之返回false */public static Boolean checkElementToBeSelected(WebElement element) {logger.info("获取【element元素信息】为:【"+element.toString()+"】,检查select被选中");logger.info("返回的实际选中状态【"+ getWait().until(ExpectedConditions.elementToBeSelected(element))+"】");return getWait().until(ExpectedConditions.elementToBeSelected(element));}/** * 检查给定元素是否被选中。 * @param expectedPageKeyword预期页面关键在 * @param expectedUIElementKeyword预期元素关键字 * @return已选择返回true,反之返回false */public static Boolean checkElementToBeSelected(String expectedPageKeyword,String expectedUIElementKeyword ) {logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,检查select被选中");By locator = getXmlBy(expectedPageKeyword, expectedUIElementKeyword);logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,检查select被选中");logger.info("返回的实际选中状态【"+ getWait().until(ExpectedConditions.elementToBeSelected(locator))+"】");return getWait().until(ExpectedConditions.elementToBeSelected(locator));}/** * 获取指定元素的实际文本,是否【等于】预期的文本值 * @param locatorBy 对象定位信息 * @param value预期的文本值 * @return等于返回true,反之返回false */public static Boolean checkTextToBe(By locator,String value) {logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,预期的文本值【"+value+"】,获取指定元素的实际文本,是否【等于】预期的文本值");logger.info("获取【实际文本值】为:【"+waitPresenceOfElementLocated(locator).getText()+"】");logger.info("返回的文本与预期的文本是否相等状态:【"+ getWait().until(ExpectedConditions.textToBe(locator,value ))+"】");return getWait().until(ExpectedConditions.textToBe(locator, value));}/** * 获取指定元素的实际文本,是否【等于】预期的文本值 * @param expectedPageKeyword预期页面关键在 * @param expectedUIElementKeyword预期元素关键字 * @param value预期的文本值 * @return等于返回true,反之返回false */public static Boolean checkTextToBe(String expectedPageKeyword,String expectedUIElementKeyword,String value) {logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,获取指定元素的实际文本,是否【等于】预期的文本值");By locator = getXmlBy(expectedPageKeyword, expectedUIElementKeyword);logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,获取指定元素的实际文本,是否等于预期的文本值");logger.info("获取【实际文本值】为:【"+waitPresenceOfElementLocated(locator).getText()+"】");logger.info("返回的文本与预期的文本是否相等状态:【"+ getWait().until(ExpectedConditions.textToBe(locator,value ))+"】");return getWait().until(ExpectedConditions.textToBe(locator, value));}/** * 获取指定元素的实际文本,是否【包含】预期的文本值 * @param locatorBy 对象定位信息 * @param value预期的文本值 * @return等于返回true,反之返回false */public static Boolean checkTextToBePresentInElementLocated(By locator,String value) {logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,预期的文本值【"+value+"】,,获取指定元素的实际文本,是否【包含】预期的文本值");logger.info("获取【实际文本值】为:【"+waitPresenceOfElementLocated(locator).getText()+"】");logger.info("返回的文本与预期的文本是否【相等】状态:【"+ getWait().until(ExpectedConditions.textToBePresentInElementLocated(locator, value))+"】");return getWait().until(ExpectedConditions.textToBePresentInElementLocated(locator, value));}/** * 获取指定元素的实际文本,是否【包含】预期的文本值 * @param expectedPageKeyword预期页面关键在 * @param expectedUIElementKeyword预期元素关键字 * @param value预期的文本值 * @return等于返回true,反之返回false */public static Boolean checkTextToBePresentInElementLocated(String expectedPageKeyword,String expectedUIElementKeyword,String value) {logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,获取指定元素的实际文本,是否【包含】等于预期的文本值");By locator = getXmlBy(expectedPageKeyword, expectedUIElementKeyword);logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,获取指定元素的实际文本,是否【包含】等于预期的文本值");logger.info("获取【实际文本值】为:【"+waitPresenceOfElementLocated(locator).getText()+"】");logger.info("返回的文本与预期的文本是否【包含】状态:【"+ getWait().until(ExpectedConditions.textToBePresentInElementLocated(locator,value ))+"】");return getWait().until(ExpectedConditions.textToBePresentInElementLocated(locator, value));}/** * 获取指定元素的实际文本,是否【包含】预期的文本值 * @param element指定元素 * @param value预期的文本值 * @return等于返回true,反之返回false */public static Boolean checkTextToBePresentInElementLocated(WebElement element,String value) {logger.info("获取【element元素信息】为:【"+element.toString()+"】,预期的文本值【"+value+"】,获取指定元素的实际文本值获取指定元素的实际文本,是否【包含】等于预期的文本值");logger.info("获取【实际文本值】为:【"+element.getText()+"】");logger.info("返回的文本与预期的文本是否【包含】状态:【"+ getWait().until(ExpectedConditions.textToBePresentInElement(element, value))+"】");return getWait().until(ExpectedConditions.textToBePresentInElement(element, value));}/** * 获取指定元素的实际value属性值,是否【包含】预期的value属性值 * @param element指定元素 * @param value预期value * @return等于返回true,反之返回false */public static Boolean checkTextToBePresentInElementValue(WebElement element,String value) {logger.info("获取【element元素信息】为:【"+element.toString()+"】,预期的value值【"+value+"】,获取指定元素的实际value值获取指定元素的实际文本,是否【包含】等于预期的value值");logger.info("获取【实际value值】为:【"+element.getAttribute("value")+"】");logger.info("返回的文本与预期的文本是否【包含】状态:【"+ getWait().until(ExpectedConditions.textToBePresentInElementValue(element, value))+"】");return getWait().until(ExpectedConditions.textToBePresentInElementValue(element, value));}/** * 获取指定元素的实际value属性值,是否【包含】预期的value属性值 * @param locatorBy 对象定位信息 * @param value预期value值 * @return等于返回true,反之返回false */public static Boolean checkTextToBePresentInElementValue(By locator,String value) {logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,预期的文本值【"+value+"】,获取指定元素的实际value值,获取指定元素的实际value属性值,是否【包含】等于预期的value属性值");logger.info("获取【实际value值】为:【"+waitPresenceOfElementLocated(locator).getAttribute("value")+"】");logger.info("返回的实际value值与预期的value值是否【包含】状态:【"+ getWait().until(ExpectedConditions.textToBePresentInElementValue(locator, value))+"】");return getWait().until(ExpectedConditions.textToBePresentInElementValue(locator, value));}/** * 获取指定元素的实际value属性值,是否【包含】预期的value属性值 * @param expectedPageKeyword预期页面关键在 * @param expectedUIElementKeyword预期元素关键字 * @param value预期的文本值 * @return等于返回true,反之返回false */public static Boolean checkTextToBePresentInElementValue(String expectedPageKeyword,String expectedUIElementKeyword,String value) {logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,【预期的属性值】:【"+value+"】获取指定元素的实际value属性值,是否【包含】等于预期的value属性值");By locator = getXmlBy(expectedPageKeyword, expectedUIElementKeyword);logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,获取指定元素的实际value属性值,是否【包含】等于预期的value属性值");logger.info("获取【实际value值】为:【"+waitPresenceOfElementLocated(locator).getAttribute("value")+"】");logger.info("返回的实际value值与预期的value值是否【包含】状态:【"+ getWait().until(ExpectedConditions.textToBePresentInElementValue(locator,value ))+"】");return getWait().until(ExpectedConditions.textToBePresentInElementValue(locator, value));}
/** * 获取指定元素的实际属性,是否【包含】预期的属性值 * @param element指定元素 * @param attribute指定属性名 * @param value预期value * @return等于返回true,反之返回false */public static Boolean checkAttributeContains(WebElement element,String attribute,String value) {logger.info("获取【element元素信息】为:【"+element.toString()+"】,指定的【属性名】:【"+attribute+"】,【预期的属性值】:"+value+",获取指定元素的实际属性是否【包含】等于预期的属性值");logger.info("获取【实际属性值】为:【"+element.getAttribute(attribute)+"】");logger.info("返回的文本与预期的文本是否【包含】状态:【"+ getWait().until(ExpectedConditions.attributeContains(element, attribute, value))+"】");return getWait().until(ExpectedConditions.attributeContains(element, attribute, value));}/** * 检查alter弹框是否存在 * @return true存在,false不存在 */public boolean checkAlertIsPresent() {try {logger.info("开始检查alter是否存在");getWait().until(ExpectedConditions.alertIsPresent());logger.info("开始检查alter存在");return true;}catch (Exception e) {logger.info("开始检查alter不存在");return false;}}/** * 检查页面元素是否可见 * @param locator * @return 如果返回为true,页面不可见或者元素不再dom中,返回false,页面可见 */public static boolean checkInvisibilityOfElementLocated(By locator) {logger.info("检查元素是否不可见或页面不存在,【元素信息】:【"+locator.toString()+"】");logger.info("检查元素是否不可见或页面的状态:【"+getWait().until(ExpectedConditions.invisibilityOfElementLocated(locator))+"】");return getWait().until(ExpectedConditions.invisibilityOfElementLocated(locator));}/** * 获取指定元素的实际属性,是否【包含】预期的属性值 * @param expectedPageKeyword预期页面关键在 * @param expectedUIElementKeyword预期元素关键字 * @param attribute指定属性名 * @param value预期的文本值 * @return等于返回true,反之返回false */public static Boolean checkAttributeContains(String expectedPageKeyword,String expectedUIElementKeyword,String attribute,String value) {logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,指定的【属性名】:【"+attribute+"】,【预期的属性值】:"+value+",获取指定元素的实际属性是否【包含】等于预期的属性值");By locator = getXmlBy(expectedPageKeyword, expectedUIElementKeyword);logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,获取指定元素的实际属性值,是否【包含】等于预期的属性值");logger.info("获取【实际属性值】为:【"+waitPresenceOfElementLocated(locator).getAttribute(attribute)+"】");logger.info("返回的实际属性值与预期的属性值是否【包含】状态:【"+ getWait().until(ExpectedConditions.attributeContains(locator, attribute, value))+"】");return getWait().until(ExpectedConditions.attributeContains(locator, attribute, value));}/** * 获取指定元素的实际属性,是否【包含】预期的属性值 * @param expectedPageKeyword预期页面关键在 * @param expectedUIElementKeyword预期元素关键字 * @param attribute指定属性名 * @param value预期的文本值 * @return等于返回true,反之返回false */public static Boolean checkAttributeContains(By locator,String attribute,String value) {logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,【指定的属性名】:【"+attribute+"】,【指定的预期属性值】:【"+value+"】,获取指定元素的实际属性值,是否【包含】等于预期的属性值");logger.info("获取【实际属性值】为:【"+waitPresenceOfElementLocated(locator).getAttribute(attribute)+"】");logger.info("返回的实际属性值与预期的属性值是否【包含】状态:【"+ getWait().until(ExpectedConditions.attributeContains(locator, attribute, value))+"】");return getWait().until(ExpectedConditions.attributeContains(locator, attribute, value));}/** * 获取指定元素的实际属性,是否【等于】预期的属性值 * @param element指定元素 * @param attribute指定属性名 * @param value预期value * @return等于返回true,反之返回false */public static Boolean checkAttributeToBe(WebElement element,String attribute,String value) {logger.info("获取【element元素信息】为:【"+element.toString()+"】,指定的【属性名】:【"+attribute+"】,【预期的属性值】:"+value+",获取指定元素的实际属性是否【等于】等于预期的属性值");logger.info("获取【实际属性值】为:【"+element.getAttribute(attribute)+"】");logger.info("返回的文本与预期的文本是否【等于】状态:【"+ getWait().until(ExpectedConditions.attributeToBe(element, attribute, value))+"】");return getWait().until(ExpectedConditions.attributeToBe(element, attribute, value));}/** * 获取指定元素的实际属性,是否【等于】预期的属性值 * @param expectedPageKeyword预期页面关键在 * @param expectedUIElementKeyword预期元素关键字 * @param attribute指定属性名 * @param value预期的文本值 * @return等于返回true,反之返回false */public static Boolean checkAttributeToBe(String expectedPageKeyword,String expectedUIElementKeyword,String attribute,String value) {logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,指定的【属性名】:【"+attribute+"】,【预期的属性值】:"+value+",获取指定元素的实际属性是否【等于】等于预期的属性值");By locator = getXmlBy(expectedPageKeyword, expectedUIElementKeyword);logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,获取指定元素的实际属性值,是否【等于】等于预期的属性值");logger.info("获取【实际属性值】为:【"+waitPresenceOfElementLocated(locator).getAttribute(attribute)+"】");logger.info("返回的实际属性值与预期的属性值是否【等于】状态:【"+ getWait().until(ExpectedConditions.attributeToBe(locator, attribute, value))+"】");return getWait().until(ExpectedConditions.attributeToBe(locator, attribute, value));}/** * 获取指定元素的实际属性,是否【等于】预期的属性值 * @param expectedPageKeyword预期页面关键在 * @param expectedUIElementKeyword预期元素关键字 * @param attribute指定属性名 * @param value预期的文本值 * @return等于返回true,反之返回false */public static Boolean checkAttributeToBe(By locator,String attribute,String value) {logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,【指定的属性名】:【"+attribute+"】,【指定的预期属性值】:【"+value+"】,获取指定元素的实际属性值,是否【等于】等于预期的属性值");logger.info("获取【实际属性值】为:【"+waitPresenceOfElementLocated(locator).getAttribute(attribute)+"】");logger.info("返回的实际属性值与预期的属性值是否【等于】状态:【"+ getWait().until(ExpectedConditions.attributeToBe(locator, attribute, value))+"】");return getWait().until(ExpectedConditions.attributeToBe(locator, attribute, value));}//public void test() throws InterruptedException {////把“随意拖拽1-1”元素移动到“随意拖拽1-2”节点下去//WebElement webElement1 = BrowserUtil.driver.findElement(By.id("treeDemo_2_span"));//WebElement webElement2= BrowserUtil.driver.findElement(By.id("treeDemo_3_span"));////构建鼠标的一连串动作//Actions actions = new Actions(BrowserUtil.driver);//actions.clickAndHold(webElement1).moveToElement(webElement2).release().build().perform();//}
public static By getXmlBy(String path ,String expectedPageKeyword,String expectedUIElementKeyword) {try {String uiElementBy = "";String uiElementValue = "";for(Page page:XmlUtils.loadXml(path)) {String actualPageKeyword = page.getKeyword();if (expectedPageKeyword.equals(actualPageKeyword)) {for (UIElement uiElement : page.getUiElementList()) {String actualUiElement = uiElement.getKeyword();if (expectedUIElementKeyword.equals(actualUiElement)) {uiElementBy = uiElement.getBy();uiElementValue = uiElement.getValue();}}}}Class<By> clazz = By.class;Method method = clazz.getMethod(uiElementBy, String.class);By by =(By) method.invoke(null, uiElementValue);return by;}catch (Exception e) {e.printStackTrace();}return null;}public static By getXmlBy(String expectedPageKeyword,String expectedUIElementKeyword) {try {String uiElementBy = "";String uiElementValue = "";for(Page page:XmlUtils.loadXml("")) {String actualPageKeyword = page.getKeyword();if (expectedPageKeyword.equals(actualPageKeyword)) {for (UIElement uiElement : page.getUiElementList()) {String actualUiElement = uiElement.getKeyword();if (expectedUIElementKeyword.equals(actualUiElement)) {uiElementBy = uiElement.getBy();uiElementValue = uiElement.getValue();}}}}Class<By> clazz = By.class;Method method = clazz.getMethod(uiElementBy, String.class);By by =(By) method.invoke(null, uiElementValue);return by;}catch (Exception e) {e.printStackTrace();}return null;}//=======================================Js部分=========================================================
/** * Selenium定位的元素被图层遮挡,click无法操作,可以用此方法点击 * @param webElement需要操作的元素 */public static void clickByJs(WebElement webElement) {logger.info(webElement.toString());getJs().executeScript("arguments[0].click();", webElement);}/** * 去除元素只读属性 * @param webElement需要操作的元素 */public static void removeAttributeReadonly(WebElement webElement) {getJs().executeScript("arguments[0].removeAttribute('readonly')",webElement);}/** * 文本输入框赋值 * @param webElement需要操作的元素 * @param input期望输入的值 * 注意: JS企图去控制输入框的值失效,是因为没有触发input或change事件(有些前端会设置) */public static void modifyValueByJs(WebElement webElement,String input) {getJs().executeScript("arguments[0].value=arguments[1]",webElement,input);}/** * 设置富文本框的内容 * @param webElement需要操作的元素 * @param input期望输入的值 */public static void modifyInnerHTMLByJs(WebElement webElement,String input) {getJs().executeScript("arguments[0].innerHTML = arguments[1]" ,webElement,input);}/** * 利用js代码取出关键字 * @param webElement需要操作的元素 * @return返回获取的value值 */public static String getValueByJs(WebElement webElement) {return (String) getJs().executeScript("var result=arguments[0].value;return result",webElement);
}
/** * 用js判断页面加载完毕,返回complete * @return返回complete */public static String judgeLoadComplete() {return getJs().executeScript("return document.readyState").toString();}/** * 获取富文本框内容 * @param webElement需要操作的元素 * @return 返回获取的innerHTML */public static String getInnerHTMLByJs(WebElement webElement) {return (String) getJs().executeScript("var result=arguments[0].innerHTML;return result" ,webElement);}/** * 滑动页面到指定元素的顶部对其 * @param webElement需要操作的元素 */public static void scrollIntoTopView(WebElement webElement) {getJs().executeScript("arguments[0].scrollIntoView(true);",webElement);}/** * 滑动页面到指定元素的顶部底部 * @param webElement需要操作的元素 */public static void scrollInTobottomView(WebElement webElement) {getJs().executeScript("arguments[0].scrollIntoView(false);",webElement);}/** * 元素滚动至可见位置_如果当前可见,则不会发生滚动 * @param webElement需要操作的元素 */public static void scrollIntoViewIfNeeded(WebElement webElement) {getJs().executeScript("arguments[0].scrollIntoViewIfNeeded(true);",webElement);}/** * 封装设置或者新增页面对象的属性值的方法 调用JS代码修改页面元素的属性值,arguments[0]~arguments[1]分别 * 会用后面的element,attributeName和value参数进行替换 * @param webElement需要操作的元素 * @param attributeName属性名 * @param attributeValue属性值 */public static void setAttribute(WebElement webElement,String attributeName,String attributeValue) {getJs().executeScript("arguments[0].setAttribute(\""+attributeName+"\",\""+attributeValue+"\")",webElement,attributeName,attributeValue);
}/** * 删除元素指定元素属性 * @param webElement需要操作的元素 * @param attributeName属性名 */public static void removeAttribute(WebElement webElement,String attributeName) {getJs().executeScript("arguments[0].removeAttribute(arguments[1])",webElement,attributeName);}/** * 获取指定元素对应的属性值 * @param webElement需要操作的元素 * @param attributeName属性名 * @return 返回指定元素对应的属性值 */public static String getAttribute(WebElement webElement,String attributeName) {logger.info("浏览器信息和获取的元素信息:【"+webElement.toString()+"】,获取该元素属性名为【"+attributeName+"】,属性值值为【"+(String)getJs().executeScript("return arguments[0].getAttribute(arguments[1])",webElement,attributeName)+"】");return (String)getJs().executeScript("return arguments[0].getAttribute(arguments[1])",webElement,attributeName);}/** * 元素高亮展示#边框border:2px; blue红色 * @param webElement需要操作的元素 */public static void setHighLight(WebElement webElement) {logger.info("浏览器信息和获取的元素信息:【"+webElement.toString()+"】,设置了【style】属性,其属性值为【border: 10px solid blue;】,其目的是高亮显示元素");setAttribute(webElement, "style", "border: 10px solid blue;");}//=======================================正则表达式替换修改xml部分===========================================/** * 替换${格式的信息} * @param expectedPageKeyword * @param expectedUIElementKeyword * @param modifyValue */public static void replaceValue(String expectedPageKeyword,String expectedUIElementKeyword,String modifyValue) {//XmlUtils.(expectedPageKeyword, expectedUIElementKeyword, modifyValue, "\\$\\{.+\\}");}/** * 替换中文字符为S{XXX} * @param expectedPageKeyword * @param expectedUIElementKeyword * @param modifyValue */public static void replaceChinese(String expectedPageKeyword,String expectedUIElementKeyword,String modifyValue) {//XmlUtil.modifyXmlInfo(expectedPageKeyword, expectedUIElementKeyword, modifyValue, "[\\x{4e00}-\\x{9fa5}]+");}//机动车综合险2020版/** * 将中文字符替换为 S{XXX} * @param expectedPageKeyword * @param expectedUIElementKeyword * @param modifyValue */public static void replaceChineseNum(String expectedPageKeyword,String expectedUIElementKeyword,String modifyValue) {//XmlUtil.modifyXmlInfo(expectedPageKeyword, expectedUIElementKeyword, modifyValue, "[0-9\\x{4e00}-\\x{9fa5}]+");}/** * 匹配中文和带-的 * @param expectedPageKeyword * @param expectedUIElementKeyword * @param modifyValue */public static void replaceChineseNum1(String expectedPageKeyword,String expectedUIElementKeyword,String modifyValue) {//XmlUtil.modifyXmlInfo(expectedPageKeyword, expectedUIElementKeyword, modifyValue, "[\\\\-\\x{4e00}-\\x{9fa5}]+");}
}
package common; import java.awt.AWTException; import java.awt.Robot; import java.awt.event.KeyEvent; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Random; import java.util.Set; import java.util.regex.Pattern; import org.apache.log4j.Logger; import org.apache.poi.poifs.crypt.dsig.KeyInfoKeySelector; import org.apache.tika.metadata.WordPerfect; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.Cookie; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.interactions.SendKeysAction; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.Select; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import pojo.Page; import pojo.UIElement; import utils.BrowserUtils; import utils.RegexUtils; import utils.XmlUtils; public class BasePage { private final static Logger logger = Logger.getLogger(BasePage.class); /** * 获取浏览器 * @return 浏览器 */ public static WebDriver getBroswer() { return BrowserUtils.getDriver(); } /** * 获取JavascriptExecutor * @return JavascriptExecutor */ public static JavascriptExecutor getJs() { return BrowserUtils.getJavascriptExecutor(); } /** * 获取WebDriverWait * @return WebDriverWait */ public static WebDriverWait getWait() { return BrowserUtils.getWebDriverWait(); } /** * 创建本地浏览器或者远程浏览器 * @param type type→本地/远程 * @param browserType 浏览器类型 */ public static void createBroswer(String type,String browserType) { logger.info("================================打开"+type+"浏览器("+browserType+") ========================================"); if (type.contains("本地")) { BrowserUtils.openBrowser(browserType);; }else if (type.contains("远程")) { BrowserUtils.openRemoteBrowser(browserType); } } /** * 创建本地浏览器 * @param browserType 浏览器类型 */ public static void createBrowser(String browserType) { logger.info("================================打开远程端浏览器("+browserType+") ========================================"); BrowserUtils.openBrowser(browserType); } /** * 创建远程浏览器 * @param browserType 浏览器类型 */ public static void createRemoteBrowser(String browserType) { logger.info("================================打开本地浏览器("+browserType+") ========================================"); BrowserUtils.openRemoteBrowser(browserType); } /** * 清楚缓存 */ public static void ClearCache () { logger.info("================================开始清除缓存 ========================================"); Set<Cookie> coo = getBroswer().manage().getCookies(); //打印Cookie logger.info("cookies信息:【"+coo.toString()+"】"); //清除所有的缓存 getBroswer().manage().deleteAllCookies(); } /** * 跳转到指定的url * @param url 需跳转的url地址 */ public static void getUrl(String url) { logger.info("浏览器跳转到:【"+url+"】"); getBroswer().get(url); } /** * 硬性等待 * @param time 毫秒 */ public static void waitTime(int time) { try { logger.info("线程等待:【"+(double)time/1000+"】秒"); Thread.sleep(time); } catch (InterruptedException e) { e.printStackTrace(); } } /** * 最大化窗口 */ public static void maxWindow() { logger.info("window窗口最大化"); getBroswer().manage().window().maximize(); } /** * 根据获取的标题来切换句柄 获取的标题包含预期的标题才能切换 * @param title 标题 */ public void switchWindowByTitle(String title) { Set<String> handles = getBroswer().getWindowHandles(); logger.info("获取【handles大小】为:【"+handles.size()+"】,【获取的handles】为:【"+handles.toString()+"】"); for (String handle : handles) { if (getBroswer().getTitle().contains(title)) { logger.info("当前句柄的【标题】:【"+getBroswer().getTitle()+"】,符合预期的标题,切换成功"); break; }else { logger.info("当前句柄的【标题】:【"+getBroswer().getTitle()+"】,切换句柄,寻找符合标题的句柄"); getBroswer().switchTo().window(handle); } } } //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== public static void main(String[] args) { createBrowser( "chrome"); maxWindow(); getBroswer().manage().deleteAllCookies(); getBroswer().get("http://egis-cspi-padis-dmzstg1.pingan.com.cn/cspi-internet/palogin.jsp"); input(By.cssSelector("#userName"), "01713801151301"); input(By.cssSelector("#passwordInput"), "a8888888"); try { Thread.sleep(25000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // if (assertEleContains(By.cssSelector("#center"), "没有找到相应信息")) { // getBroswer().navigate().refresh(); // waitTime(2000); // getBroswer().switchTo().alert().accept(); // waitTime(2000); // input(By.cssSelector("#passwordInput"), "a8888888"); // try { // Thread.sleep(25000); // } catch (InterruptedException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } getBroswer().switchTo().defaultContent(); try { getBroswer().switchTo().frame(getBroswer().findElement(By.cssSelector("#EM_PAGE"))); }catch(Exception e) { if (assertEleContains(By.cssSelector("#center"), "没有找到相应信息")) { getBroswer().navigate().refresh(); waitTime(2000); boolean s ; try { s = assertEleContains(By.cssSelector("#warmTips"), "温馨提示"); } catch (Exception e2) { s = false; } if (s){ getBroswer().switchTo().frame(getBroswer().findElement(By.cssSelector("#EM_PAGE"))); getBroswer().switchTo().frame(getBroswer().findElement(By.cssSelector("#content"))); }else { getBroswer().switchTo().alert().accept(); waitTime(2000); input(By.cssSelector("#passwordInput"), "a8888888"); try { Thread.sleep(25000); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } getBroswer().switchTo().frame(getBroswer().findElement(By.cssSelector("#EM_PAGE"))); getBroswer().switchTo().frame(getBroswer().findElement(By.cssSelector("#content"))); } } /** * 点击提示关闭按钮 */ waitVisibilityOfElementLocated(By.cssSelector("#warmTips #closeWarmTips")); clickByJs(getBroswer().findElement(By.cssSelector("#warmTips #closeWarmTips"))); /** * 通过键盘按钮点击回车键 */ try { Thread.sleep(6000); Robot robot = new Robot(); robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); robot.delay(500); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } /** * 点击alter弹框 */ // getBroswer().switchTo().alert().accept(); /** * 点击提示关闭按钮 */ clickByJs( waitVisibilityOfElementLocated(By.cssSelector("#warmTips #closeWarmTips"))); getBroswer().switchTo().defaultContent(); getBroswer().switchTo().frame(getBroswer().findElement(By.cssSelector("#EM_PAGE"))); /** * 点击悬浮保全处理查询 */ new Actions(getBroswer()) .moveToElement( waitVisibilityOfElementLocated(By.xpath("//a[contains(text(),'保全处理查询')]"))).perform(); /** * 点击余额管理 */ clickByJs(waitVisibilityOfElementLocated(By.xpath("//a[contains(text(),'余额管理')]"))); getBroswer().switchTo().frame(getBroswer().findElement(By.cssSelector("#content"))); /** * 保单号 */ String policyNo = "GP02002024242842"; /** * 输入保单号 */ waitVisibilityOfElementLocated(By.cssSelector("#queryPolNoT")).sendKeys(policyNo); /** * 点击查询 */ waitElementToBeClickable(By.cssSelector("[onclick*='queryPolInfoByPolNo()']")).click(); String beforeText = waitVisibilityOfElementLocated(By.xpath("//th[contains(text(),'保单号')]/parent::tr/following-sibling::tr")).getText(); String beforeMOney = RegexUtils.regexSingleMatch("(?<=\\s)\\d+\\.\\d+(?=\\s)", beforeText); Double beforeMOney1 = Double.parseDouble(beforeMOney); System.out.println(beforeText+" "+beforeMOney1); /** * 验证查询后数据是否正确 assertPolInfoLink * 是否包含查询的保单号 */ boolean assertContains = assertRowContains(By.xpath("//th[contains(text(),'保单号')]/parent::tr/following-sibling::tr"),policyNo); /** * 查询出来的保单号是否唯一 */ System.out.println("保单查询后是否包含:【"+assertContains+"】"); boolean assertInfoCountOne = assertInfoCountOne(By.xpath("//th[contains(text(),'保单号')]/parent::tr/following-sibling::tr") ); System.out.println("保单查询后是否唯一:【"+assertInfoCountOne+"】"); /** * 点击保单链接 */ waitElementToBeClickable(By.cssSelector("a[onclick*=displayDetail]")).click(); /** * 验证保单链接功能 * 包含 团体保单详情页 * 保单信息 * 投保人信息 * 保障信息 * 层级信息 */ boolean assertEleContains2 = assertEleContains(By.cssSelector("#dialogContent.process-content "), "block", "style"); System.out.println("是否包含属性值:"+assertEleContains2); boolean assertEleContains = assertEleContains(By.cssSelector("#dialogContent.process-content "),"团体保单详情页" ); System.out.println("元素文本值是否包含:"+assertEleContains); assertEleContains = assertEleContains(By.cssSelector("#dialogContent.process-content "),"保单信息" ); System.out.println("元素文本值是否包含:"+assertEleContains); assertEleContains = assertEleContains(By.cssSelector("#dialogContent.process-content "),"投保人信息" ); System.out.println("元素文本值是否包含:"+assertEleContains); assertEleContains = assertEleContains(By.cssSelector("#dialogContent.process-content "),"保障信息" ); System.out.println("元素文本值是否包含:"+assertEleContains); assertEleContains = assertEleContains(By.cssSelector("#dialogContent.process-content "),"层级信息" ); System.out.println("元素文本值是否包含:"+assertEleContains); /** * 滑动到层级信息 */ scrollIntoTopView(waitPresenceOfElementLocated(By.xpath("//*[contains(text(),'层级信息')]"))); waitTime(5000); System.out.println(" 已经滑懂了"); // scrollInTobottomView(waitPresenceOfElementLocated(By.xpath("//*[contains(text(),'层级信息')]"))); // waitTime(5000); /** * 点击职业链接 */ waitElementToBeClickable(By.cssSelector("[onclick*='showProfCode']")).click(); /** * 点击职业层级链接后显示的层级详情 */ assertEleContains2 = assertEleContains(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]"), "", "style"); System.out.println("是否包含属性值:"+assertEleContains2); assertEleContains2 = assertEleContains(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]"), "职业描述"); System.out.println("是否包含文本值:"+assertEleContains2); assertEleContains2 = assertEleContains(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]"), "职业代码"); System.out.println("是否包含文本值:"+assertEleContains2); assertEleContains2 = assertInfoCountBigOne(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]//td[2][not(contains(text(),'职业'))]/parent::tr")); System.out.println("元素是否大于等于1个元素:"+assertEleContains2); /** * 点击层级明细链接 * showDetailNew */ waitElementToBeClickable(By.cssSelector("[onclick*='showDetailNew']")).click(); assertEleContains2 = assertEleContains(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]"), "", "style"); System.out.println("是否包含属性值:"+assertEleContains2); assertEleContains2 = assertEleContains(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]"), "保险金额"); System.out.println("是否包含文本值:"+assertEleContains2); assertEleContains2 = assertEleContains(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]"), "赔付比例"); System.out.println("是否包含文本值:"+assertEleContains2); assertEleContains2 = assertInfoCountBigOne(By.xpath("//tr[@class='show-li' and not(contains(@style,'none'))]//td[2][not(contains(text(),'保险'))]/parent::tr")); System.out.println("元素是否大于等于1个元素:"+assertEleContains2); /** * 点击关闭保单详情按钮 */ clickByJs(waitElementToBeClickable(By.cssSelector("[onclick*='closetxt']"))); /** * 点击转出按钮 */ clickByJs(waitElementToBeClickable(By.cssSelector("[onclick*='queryFundAmount']"))); /** * 验证传出按钮是否跳余额转出页面 */ assertEleContains2 = assertEleContains(By.cssSelector("div#returnsDiv"), "单位对公账户"); System.out.println("是否包含文本值:"+assertEleContains2); assertEleContains2 = assertEleContains(By.cssSelector("div#returnsDiv"), "余额转出明细"); System.out.println("是否包含文本值:"+assertEleContains2); /** * 点击余额转出明细 */ click(By.cssSelector("[onclick*='historyRefundData']")); /** * 输入 余额转出明细 - 开始日期输入框 */ modifyValueByJs(waitVisibilityOfElementLocated(By.cssSelector("input#beginDate")), "2021-01-01"); /** * 输入 余额转出明细 - 结束日期输入框 */ modifyValueByJs(waitVisibilityOfElementLocated(By.cssSelector("input#endDate")), "2021-03-13"); /** * 选择 余额转出明细 - 转出状态 */ new Select(waitVisibilityOfElementLocated(By.cssSelector("select#applyStatus"))).selectByValue("P"); /** * 点击余额转出明细 - 点击查询 */ clickByJs(waitVisibilityOfElementLocated(By.cssSelector("[onclick*='submitQuery']"))); // click(By.cssSelector("[onclick*='submitQuery']")); /** * 断言查询后展示的数>=1 */ waitTime(2000); assertEleContains2 = assertInfoCountBigOne(By.cssSelector(".use_money_tr")); System.out.println("元素是否大于等于1个元素:"+assertEleContains2); /** * 判断余额转出明细详情页 是否包含 div#history_data_display * 流水号 * 转移金额 */ assertEleContains2 = assertEleContains(By.cssSelector("div#history_data_display"), "流水号"); System.out.println("是否包含文本值:"+assertEleContains2); assertEleContains2 = assertEleContains(By.cssSelector("div#history_data_display"), "转移金额"); System.out.println("是否包含文本值:"+assertEleContains2); /** * 余额转出明细详情页 返回按钮[onclick*='goBack()'] */ click(By.cssSelector("[onclick*='goBack()']")); /** * 点击 单位对公账户链接 */ click(By.cssSelector("[onclick*='setBankAcounts']")); /** * 单位对公账户链接 后显示的用户须知 */ assertEleContains2 = assertEleContains(By.cssSelector("div#infoDiv"),"用户告知"); System.out.println("是否包含文本值:"+assertEleContains2); assertEleContains2 = assertEleContains(By.cssSelector("div#infoDiv"),"注意事项"); System.out.println("是否包含文本值:"+assertEleContains2); assertEleContains2 = assertEleContains(By.cssSelector("div#infoDiv"),"责任免除"); /** * 单位对公账户链接 接受按钮 */ click(By.cssSelector("[onclick*='goToNext']")); /** * 账户信息维护页-开户银行 */ waitVisibilityOfElementLocated(By.cssSelector("#bankName")); // input(By.cssSelector("input#bankName"), "a"); try { Robot r = new Robot(); waitTime(1000); r.keyPress(KeyEvent.VK_TAB); r.keyRelease(KeyEvent.VK_TAB); r.delay(200); waitTime(1000); r.keyPress(KeyEvent.VK_TAB); r.keyRelease(KeyEvent.VK_TAB); r.delay(200); waitTime(1000); r.keyPress(KeyEvent.VK_B); r.keyRelease(KeyEvent.VK_B); r.delay(200); waitTime(1000); waitTime(1000); r.keyPress(KeyEvent.VK_ENTER); r.keyRelease(KeyEvent.VK_ENTER); r.delay(200); waitTime(1000); r.keyPress(KeyEvent.VK_DOWN); r.keyRelease(KeyEvent.VK_DOWN); r.delay(200); waitTime(1000); r.keyPress(KeyEvent.VK_ENTER); r.keyRelease(KeyEvent.VK_ENTER); r.delay(200); waitTime(1000); /** * 输入账户信息维护-分行 */ waitTime(1000); r.keyPress(KeyEvent.VK_TAB); r.keyRelease(KeyEvent.VK_TAB); r.delay(200); waitTime(1000);waitTime(1000); r.keyPress(KeyEvent.VK_B); r.keyRelease(KeyEvent.VK_B); r.delay(200); waitTime(1000); r.keyPress(KeyEvent.VK_ENTER); r.keyRelease(KeyEvent.VK_ENTER); r.delay(200); waitTime(1000); waitTime(1000); r.keyPress(KeyEvent.VK_DOWN); r.keyRelease(KeyEvent.VK_DOWN); r.delay(200); waitTime(1000); r.keyPress(KeyEvent.VK_ENTER); r.keyRelease(KeyEvent.VK_ENTER); r.delay(200); waitTime(1000); /** * 账户信息维护-支行 * 输入支行 */ input(By.cssSelector("#subbranchBank"), "自动化支行"+new Random().nextInt(1000)); /** * 账户信息维护-银行账户 * bankAccount */ input(By.cssSelector("#bankAccount"), "6217855300003734772"); /** * 点击账户信息维护-确认按钮 doSaveButton */ click(By.cssSelector("#doSaveButton")); /** * 点击账户信息维护-弹框确定按钮 */ boolean assertAlterContains = assertAlterContains("银行账号信息保存成功"); System.out.println("assertAlterContains:"+assertAlterContains); waitTime(1000); r.keyPress(KeyEvent.VK_ENTER); r.keyRelease(KeyEvent.VK_ENTER); r.delay(200); waitTime(1000); } catch (Exception e2) { // TODO Auto-generated catch block e2.printStackTrace(); } /** * 点击转出按钮 */ clickByJs(waitElementToBeClickable(By.cssSelector("[onclick*='queryFundAmount']"))); /** * 点击 单位对公账户链接 */ click(By.cssSelector("[onclick*='setBankAcounts']"));/** * 单位对公账户链接 接受按钮 */ click(By.cssSelector("[onclick*='goToNext']")); /** * 历史维护账户信息 历史维护账户信息按钮 */ click(By.cssSelector("[onclick*='queryHistoryBank']")); /** * 历史维护账号信息-历史信息勾选框 * 判断是否包含页面带出历史账户信息提示 */ boolean assertEleContains3 = assertEleContains(By.cssSelector("#bankDiv2"),"页面带出历史账户信息"); System.out.println(assertEleContains3); /** * 判断历史账户信息行唯一 */ boolean assertInfoCountOne2 = assertInfoCountOne(By.cssSelector("#bankDiv2 [id*='items']")); System.out.println("assertInfoCountOne2:"+assertInfoCountOne2); String text = waitVisibilityOfElementLocated(By.cssSelector("#bankno1")).getText(); System.out.println(text); /** * 点击历史维护账号信息-历史账户勾选框 */ click(By.cssSelector("#check1")); waitTime(2000); /** * 断言历史维护账号信息-跳转到 历史维护账号信息 */ boolean assertEleContains4 = assertEleContains(By.cssSelector("#bankDiv"),"账户信息维护"); System.out.println(assertEleContains4); /** * 账户信息维护-确认按钮 * */ click(By.cssSelector("#doSaveButton")); /** * 点击账户信息维护-弹框确定按钮 */ boolean assertAlterContains = assertAlterContains("银行账号信息保存成功"); System.out.println("assertAlterContains:"+assertAlterContains); waitTime(1000); Robot r; try { r = new Robot(); r.keyPress(KeyEvent.VK_ENTER); r.keyRelease(KeyEvent.VK_ENTER); r.delay(200); waitTime(1000); } catch (Exception e3) { // TODO Auto-generated catch block e3.printStackTrace(); } /** * 点击转出按钮 */ clickByJs(waitElementToBeClickable(By.cssSelector("[onclick*='queryFundAmount']"))); /** * 余额转出-可用转出金额输入框 */ String refundMoney = "1"; modifyValueByJs(waitVisibilityOfElementLocated(By.cssSelector("input#amount")), refundMoney); /** * [onclick*='callRefundSubmit'] * 余额转出-确定转出按钮 */ click(By.cssSelector("[onclick*='callRefundSubmit']")); /** * 判断余额转出-确定转出后-处理完成提示框是否包含转出的价格 * */ boolean assertEleContains5 = assertEleContains(By.cssSelector("div#suPag"),"余额"+refundMoney+"元"); System.out.println("assertEleContains5:"+assertEleContains5); /** * 余额转出-确定转出后-处理完成提示框-确定按钮 */ click(By.cssSelector("[onclick*='gotoView']")); /** * 余额管理-转入按钮 */ click(By.cssSelector("[onclick*='findUseableBalacePol']")); /** * 余额管理-余额转入 * detail_useable_div * 判断 余额转入是否包含 余额转入详情页 */ assertEleContains(By.cssSelector("div#detail_useable_div"),"余额转入"); assertEleContains(By.cssSelector("div#detail_useable_div"),policyNo); assertEleContains(By.cssSelector("div#detail_useable_div"),"转移余额"); assertEleContains(By.cssSelector("div#detail_useable_div"),"可用余额"); /** * 余额管理-余额转入-余额互转明细链接 */ click(By.cssSelector("[onclick*='historyData']")); //==================================================== /** * 输入 余额互转明细 - 开始日期输入框 */ modifyValueByJs(waitVisibilityOfElementLocated(By.cssSelector("input#applyBeginDate")), "2021-01-01"); /** * 输入 余额互转明细 - 结束日期输入框 */ modifyValueByJs(waitVisibilityOfElementLocated(By.cssSelector("input#applyEndDate")), "2021-03-13"); /** * 选择 余额互转明细 - 支付模式 */ new Select(waitVisibilityOfElementLocated(By.cssSelector("select#payMode"))).selectByValue("2"); /** * 点击余额互转明细 - 点击查询 */ clickByJs(waitVisibilityOfElementLocated(By.cssSelector("[onclick*='histMovePage']"))); // click(By.cssSelector("[onclick*='submitQuery']")); /** * 断言查询后展示的数>=1 */ waitTime(2000); assertEleContains2 = assertInfoCountBigOne(By.cssSelector(".use_money_tr")); System.out.println("元素是否大于等于1个元素:"+assertEleContains2); /** * 判断余额转出明细详情页 是否包含 div#history_data_display * 流水号 * 转移金额 */ assertEleContains2 = assertEleContains(By.cssSelector("div#history_data_display"), "流水号"); System.out.println("是否包含文本值:"+assertEleContains2); assertEleContains2 = assertEleContains(By.cssSelector("div#history_data_display"), "转移金额"); System.out.println("是否包含文本值:"+assertEleContains2); /** * 余额管理查询页 - 查询结果行 boolean assertContains = assertRowContains(By.cssSelector("#user_pol_div_id .img-position tr"),policyNo); */ String text2 = waitVisibilityOfElementLocated(By.xpath("//th[contains(text(),'保单号')]/parent::tr/following-sibling::tr")).getText(); String regexSingleMatch = RegexUtils.regexSingleMatch("(?<=\\s)\\d+\\.\\d+(?=\\s)", text2); Double d = Double.parseDouble(regexSingleMatch); System.out.println(text2+" "+d); if (beforeMOney1==(d+Double.parseDouble(refundMoney))) { System.out.println("true"); }else { System.out.println("false"); } /** * 点击余额互转明细 - 返回 ackWay(); */ clickByJs(waitVisibilityOfElementLocated(By.cssSelector("[onclick*='backWay']"))); /** * 暂时没有转入订单 余额转入-确定转入按钮 */ clickByJs(waitVisibilityOfElementLocated(By.cssSelector("[onclick*='submitConvertMoney']"))); } } //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== public void yuEGuanLiBase() { } //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== //==================================================================================================================== /** * 判断弹框文本是否包含关键字 * @param key * @return 包含返回true,否则false */ private static boolean assertAlterContains(String key) { waitTime(3000); System.out.println(getBroswer().switchTo().alert().getText()); return getBroswer().switchTo().alert().getText().contains(key); } /** * 判断元素的属性值是否包含预期的属性值 * @param by * @param value * @param name * @return 包含返回true,不包含返回false */ public static boolean assertEleContains(By by,String value,String name) { return waitVisibilityOfElementLocated(by).getAttribute(name).contains(value); } /** * 判断元素的文本是否包含关键字 * @param by * @param key * @return 包含返回true,不包含返回false */ public static boolean assertEleContains(By by,String key) { return waitVisibilityOfElementLocated(by).getText().contains(key); } /** * 判断所有元素获取的文本是否包含指定关键字 * @param by * @param key * @return 包含返回true,不包含false */ private static boolean assertRowContains(By by,String key) { List<WebElement> list = waitVisibilityOfAllElementsLocatedBy(by); for (WebElement webElement : list) { if (webElement.getText().contains(key)) { return true; } } return false; } /** * 判断所有元素个数是否一个 * @param by * @param key * @return 是一个true,不是唯一返回false */ private static boolean assertInfoCountOne(By by) { List<WebElement> list = waitVisibilityOfAllElementsLocatedBy(by); if (list.size()==1) { return true; } return false; } /** * 判断所有元素个数是否大于等于一个 * @param by * @param key * @return 是一个true,不是唯一返回false */ private static boolean assertInfoCountBigOne(By by) { List<WebElement> list = waitVisibilityOfAllElementsLocatedBy(by); if (list.size()>=1) { return true; } return false; } /** * 根据获取的url来切换句柄 获取的标题包含预期的标题才能切换 * @param url 标题 */ public void switchWindowByCurrentUrl(String url) { Set<String> handles = getBroswer().getWindowHandles(); logger.info("获取【handles大小】为:【"+handles.size()+"】,【获取的handles】为:【"+handles.toString()+"】"); for (String handle : handles) { if (getBroswer().getCurrentUrl().contains(url)) { logger.info("当前句柄的【url】:【"+getBroswer().getCurrentUrl()+"】,符合预期的标题,切换成功"); break; }else { logger.info("当前句柄的【url】:【"+getBroswer().getCurrentUrl()+"】,切换句柄,寻找符合标题的句柄"); getBroswer().switchTo().window(handle); } } } /** * 等待单个元素存在在dom对象中,但不一定可见 * @param locator By 对象定位信息 * @return 返回存在dom 单个WebElement中元素,否则抛出异常 */ public static WebElement waitPresenceOfElementLocated(By locator) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,等待元素在dom中存在"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.presenceOfElementLocated(locator)).toString()+"】"); return getWait().until(ExpectedConditions.presenceOfElementLocated(locator)); } /** * 等待单个元素存在在dom对象中,但不一定可见 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @return 返回存在dom 单个WebElement中元素,否则抛出异常 */ public static WebElement waitPresenceOfElementLocated(String expectedPageKeyword,String expectedUIElementKeyword) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,等待元素在dom中存在"); By by = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【元素定位信息】为:【"+by.toString()+"】"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.presenceOfElementLocated(by)).toString()+""); return getWait().until(ExpectedConditions.presenceOfElementLocated(by)); } /** * 等待多个个元素存在在dom对象中,但不一定可见 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @return 返回存在dom 多个WebElement中元素-List<WebElement>,否则抛出异常 */ public static List<WebElement> waitPresenceOfAllElementsLocatedBy(String expectedPageKeyword,String expectedUIElementKeyword) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,等待元素们在dom中存在"); By by = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【元素定位信息】为:【"+by.toString()+"】"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.presenceOfAllElementsLocatedBy(by)).toString()+"】,其个数为【"+getWait().until(ExpectedConditions.presenceOfAllElementsLocatedBy(by)).size()+"】"); return getWait().until(ExpectedConditions.presenceOfAllElementsLocatedBy(by)); } /** * 等待多个个元素存在在dom对象中,但不一定可见 * @param locator By 对象定位信息 * @return 返回存在dom 多个WebElement中元素-List<WebElement>,否则抛出异常 */ public static List<WebElement> waitPresenceOfAllElementsLocatedBy(By locator) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,等待元素们在dom中存在"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.presenceOfAllElementsLocatedBy(locator)).toString()+"】,其个数为【"+getWait().until(ExpectedConditions.presenceOfAllElementsLocatedBy(locator)).size()+"】"); return getWait().until(ExpectedConditions.presenceOfAllElementsLocatedBy(locator)); } //=================== /** * 等待单个元素在页面可见 * @param locator By 对象定位信息 * @return 返回页面可见的 单个WebElement中元素,否则抛出异常 */ public static WebElement waitVisibilityOfElementLocated(By locator) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,等待元素在页面可见"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.visibilityOfElementLocated(locator)).toString()+"】"); return getWait().until(ExpectedConditions.visibilityOfElementLocated(locator)); } /** * 等待单个元素在页面可见 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @return 返回页面可见的 单个WebElement中元素,否则抛出异常 */ public static WebElement waitVisibilityOfElementLocated(String expectedPageKeyword,String expectedUIElementKeyword) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,等待元素在在页面可见"); By by = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【元素定位信息】为:【"+by.toString()+"】"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.visibilityOfElementLocated(by)).toString()+""); return getWait().until(ExpectedConditions.visibilityOfElementLocated(by)); } /** * 等待单个元素在页面可见 * @param element 需要等待的元素 * @return 返回页面可见的 单个WebElement中元素,否则抛出异常 */ public static WebElement waitVisibilityOfElementLocated(WebElement element) { logger.info("获取【元素】为:【"+element.toString()+"】,等待页面可见"); return getWait().until(ExpectedConditions.visibilityOf(element)); } /** * 等待多个个元素在页面可见 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @return 返回存在页面 多个WebElement中元素-List<WebElement>,否则抛出异常 */ public static List<WebElement> waitVisibilityOfAllElementsLocatedBy(String expectedPageKeyword,String expectedUIElementKeyword) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,等待元们素在页面可见"); By by = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【元素定位信息】为:【"+by.toString()+"】"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.visibilityOfAllElementsLocatedBy(by)).toString()+"】,其个数为【"+getWait().until(ExpectedConditions.visibilityOfAllElementsLocatedBy(by)).size()+"】"); return getWait().until(ExpectedConditions.visibilityOfAllElementsLocatedBy(by)); } /** * 等待多个个元素在页面可见 * @param locator By 对象定位信息 * @return 返回存在页面 多个WebElement中元素-List<WebElement>,否则抛出异常 */ public static List<WebElement> waitVisibilityOfAllElementsLocatedBy(By locator) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,等待元素们在页面可见"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator)).toString()+"】,其个数为【"+getWait().until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator)).size()+"】"); return getWait().until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator)); } /** * 等待多个个元素在页面可见 * @param locator By 对象定位信息 * @return 返回存在页面 多个WebElement中元素-List<WebElement>,否则抛出异常 */ public static List<WebElement> waitVisibilityOfAllElementsLocatedBy(WebElement ... elements) { logger.info("获取【locator定位信息】为:【"+elements.toString()+"】,等待元素们在页面可见"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.visibilityOfAllElements(elements)).toString()+"】,其个数为【"+getWait().until(ExpectedConditions.visibilityOfAllElements(elements)).size()+"】"); return getWait().until(ExpectedConditions.visibilityOfAllElements(elements)); } /** * 等待单个元素可被点击 * @param locator By 对象定位信息 * @return 返回页面可点击单个WebElement中元素,否则抛出异常 */ public static WebElement waitElementToBeClickable(By locator) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,等待元素可被点击"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.elementToBeClickable(locator)).toString()+"】"); return getWait().until(ExpectedConditions.elementToBeClickable(locator)); } /** * 等待单个元素可被点击 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @return 返回页面可点击单个WebElement中元素,否则抛出异常 */ public static WebElement waitElementToBeClickable(String expectedPageKeyword,String expectedUIElementKeyword) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,等待元素可被点击"); By by = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【元素定位信息】为:【"+by.toString()+"】"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.elementToBeClickable(by)).toString()+""); return getWait().until(ExpectedConditions.elementToBeClickable(by)); } /** * 等待单个元素可被点击 * @param element 需要等待的元素 * @return 返回页面可点击单个WebElement中元素,否则抛出异常 */ public static WebElement waitElementToBeClickable(WebElement element) { logger.info("获取【元素】为:【"+element.toString()+"】,等待元素可被点击"); return getWait().until(ExpectedConditions.elementToBeClickable(element)); } /** * 等待Frame可以跳转并跳转进去 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @return 返回webDriver实例 */ public static WebDriver waitFrameToBeAvailableAndSwitchToIt(String expectedPageKeyword,String expectedUIElementKeyword) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,等待Frame可以跳转并跳转进去"); By by = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【元素定位信息】为:【"+by.toString()+"】"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(by)).toString()+""); return getWait().until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(by)); } /** * 等待Frame可以跳转并跳转进去 * @param element 需要等待的元素 * @return 返回webDriver实例 */ public static WebDriver waitFrameToBeAvailableAndSwitchToIt(WebElement element) { logger.info("获取【元素】为:【"+element.toString()+"】,等待Frame可以跳转并跳转进去"); return getWait().until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(element)); } /** * 等待Frame可以跳转并跳转进去 * @param frameLocator (id or name) * @return 返回webDriver实例 */ public static WebDriver waitFrameToBeAvailableAndSwitchToIt(String frameLocator) { logger.info("获取【frame (id or name)】为:【"+frameLocator+"】,等待Frame可以跳转并跳转进去"); return getWait().until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frameLocator)); } /** * 等待Frame可以跳转并跳转进去 * @param frameLocator index * @return 返回webDriver实例 */ public static WebDriver waitFrameToBeAvailableAndSwitchToIt(int frameLocator) { logger.info("获取【frame (index)】为:【"+frameLocator+"】,等待Frame可以跳转并跳转进去"); return getWait().until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frameLocator)); } /** * 等待Frame可以跳转并跳转进去 * @param locator By 对象定位信息 * @return 返回webDriver实例 */ public static WebDriver waitFrameToBeAvailableAndSwitchToIt(By locator) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,等待Frame可以跳转并跳转进去"); logger.info("返回的元素有【"+ getWait().until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(locator)).toString()+"】"); return getWait().until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(locator)); } /** * 点击某个元素 * @param locator By定位对象 */ public static void click(By locator ) { try { logger.info("点击了等待可以点击的元素,其定位信息【"+locator.toString()+"】"); waitElementToBeClickable(locator).click(); } catch (Exception e) { try { logger.info("点击了等待可以点击的元素失败,通过js点击,其定位信息【"+locator.toString()+"】"); clickByJs(waitPresenceOfElementLocated(locator)); } catch (Exception e2) { e.printStackTrace(); } } } /** * 点击某个元素 * @param element 元素对象 */ public static void click(WebElement element ) { try { logger.info("点击了等待可以点击的元素,其元素信息【"+element.toString()+"】"); waitElementToBeClickable(element).click(); } catch (Exception e) { try { logger.info("点击了等待可以点击的元素失败,通过js点击,其元素信息【"+element.toString()+"】"); clickByJs(element); } catch (Exception e2) { e.printStackTrace(); } } } /** * 点击某个元素 * @param expectedPageKeyword 预期页面 * @param expectedUIElementKeyword 预期关键字 */ public static void click(String expectedPageKeyword,String expectedUIElementKeyword ) { try { logger.info("点击了【关键页面】:【"+expectedPageKeyword+"】【关键元素】:【"+expectedUIElementKeyword+"】"); waitElementToBeClickable(expectedPageKeyword, expectedUIElementKeyword).click(); } catch (Exception e) { try { logger.info("点击了【关键页面】:【"+expectedPageKeyword+"】【关键元素】:【"+expectedUIElementKeyword+"】失败,现通过js点击"); clickByJs(waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword)); } catch (Exception e2) { e.printStackTrace(); } } } /** * 输入信息 * @param locator By定位对象 * @param keysToSend 输入的值 */ public static void input(By locator,String keysToSend) { try { logger.info("对dom存在的元素进行输入,其定位信息【"+locator.toString()+"】,【输入的内容】为:【"+keysToSend+"】"); waitPresenceOfElementLocated(locator).sendKeys(keysToSend); if (!checkTextToBePresentInElementValue(locator, keysToSend)) { logger.info("对dom存在的元素进行输入,输入的信息和value值不一致,通过js直接改变value值,其定位信息【"+locator.toString()+"】,【输入的内容】为:【"+keysToSend+"】"); modifyValueByJs(waitVisibilityOfElementLocated(locator), keysToSend); } } catch (Exception e) { e.printStackTrace(); } } /** * 输入信息 * @param element 元素对象 * @param keysToSend 输入的值 */ public static void input(WebElement element,String keysToSend ) { try { logger.info("对dom存在的元素进行输入,其定位信息【"+element.toString()+"】,【输入的内容】为:【"+keysToSend+"】"); element.sendKeys(keysToSend); if (!checkTextToBePresentInElementValue(element, keysToSend)) { logger.info("对dom存在的元素进行输入,输入的信息和value值不一致,通过js直接改变value值,其定位信息【"+element.toString()+"】,【输入的内容】为:【"+keysToSend+"】"); modifyValueByJs(element, keysToSend); } } catch (Exception e) { e.printStackTrace(); } } /** * 点击某个元素 * @param expectedPageKeyword 预期页面 * @param expectedUIElementKeyword 预期关键字 * @param keysToSend 输入的值 */ public static void input(String expectedPageKeyword,String expectedUIElementKeyword,String keysToSend ) { try { logger.info("向【关键页面】:【"+expectedPageKeyword+"】【关键元素】:【"+expectedUIElementKeyword+"】输入【 "+keysToSend+"】"); waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword).sendKeys(keysToSend); if (!checkTextToBePresentInElementValue(waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword), keysToSend)) { logger.info("对dom存在的元素进行输入,输入的信息和value值不一致,通过js直接改变value值,其关键页面【"+expectedPageKeyword+"】,【关键元素】:【"+ expectedUIElementKeyword+"】,【输入的内容】为:【"+keysToSend+"】"); modifyValueByJs(waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword), keysToSend); } }catch (Exception e) { e.printStackTrace(); } } /** * 取消选择所有select * @param locator By 对象定位信息 */ public static void deselectAll(By locator) { logger.info("取消所有的select选项,select定位是:【"+locator.toString()+"】"); Select select = new Select(waitPresenceOfElementLocated(locator)); select.deselectAll(); } /** * 取消选择所有select * @param element WebElement对象 */ public static void deselectAll(WebElement element) { logger.info("取消所有的select选项,select定位是:【"+element.toString()+"】"); Select select = new Select(element); select.deselectAll(); } /** * 取消选择所有select * @param expectedPageKeyword 预期页面 * @param expectedUIElementKeyword 预期关键字 */ public static void deselectAll(String expectedPageKeyword,String expectedUIElementKeyword) { logger.info("取消所有的select选项"); Select select = new Select(waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword)); select.deselectAll(); } /** * 通过索引来选择select * @param locator By 对象定位信息 * @param index 索引 */ public static void selectByIndex(By locator,int index) { logger.info("通过索引来选择select,select定位是:【"+locator.toString()+"】,【索引值】:【"+index+"】"); Select select = new Select(waitPresenceOfElementLocated(locator)); select.selectByIndex(index); } /** * 通过索引来选择select * @param element WebElement对象 * @param index 索引 */ public static void selectByIndex(WebElement element,int index) { logger.info("通过索引来选择select,select定位是:【"+element.toString()+"】,【索引值】:【"+index+"】"); Select select = new Select(element); select.selectByIndex(index); } /** * 通过索引值来选择select * @param expectedPageKeyword 预期页面 * @param expectedUIElementKeyword 预期关键字 * @param index 索引值 */ public static void selectByIndex(String expectedPageKeyword,String expectedUIElementKeyword,int index) { logger.info("通过索引值来选择select,select所在关键页面是:【"+expectedPageKeyword+"】,所在关键元素是【"+expectedUIElementKeyword+"】,【索引值】:【"+index+"】"); Select select = new Select(waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword)); select.selectByIndex(index); } /** * 通过value值来选择select * @param locator By 对象定位信息 * @param value value值 */ public static void selectByValue(By locator,String value) { logger.info("通过value值来选择select,select定位是:【"+locator.toString()+"】,【value值值】:【"+value+"】"); Select select = new Select(waitPresenceOfElementLocated(locator)); select.selectByValue(value); } /** * 通过value值来选择select * @param element WebElement对象 * @param value value值 */ public static void selectByValue(WebElement element,String value) { logger.info("通过value值来选择select,select定位是:【"+element.toString()+"】,【value值值】:【"+value+"】"); Select select = new Select(element); select.selectByValue(value); } /** * 通过value值来选择select * @param expectedPageKeyword 预期页面 * @param expectedUIElementKeyword 预期关键字 * @param value value值 */ public static void selectByValue(String expectedPageKeyword,String expectedUIElementKeyword,String value) { logger.info("通过value值来选择select,select所在关键页面是:【"+expectedPageKeyword+"】,所在关键元素是【"+expectedUIElementKeyword+"】,【value值值】:【"+value+"】"); Select select = new Select(waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword)); select.selectByValue(value); } // /** * 通过可见文本值来选择select * @param locator By 对象定位信息 * @param text 可见文本值 */ public static void selectByVisibleText(By locator,String text) { logger.info("通过可见文本值来选择select,select定位是:【"+locator.toString()+"】,【可见文本值】:【"+text+"】"); Select select = new Select(waitPresenceOfElementLocated(locator)); select.selectByVisibleText(text); } /** * 通过可见文本值来选择select * @param element WebElement对象 * @param text 可见文本值 */ public static void selectByVisibleText(WebElement element,String text) { logger.info("通过可见文本值来选择select,select定位是:【"+element.toString()+"】,【可见文本值】:【"+text+"】"); Select select = new Select(element); select.selectByVisibleText(text); } /** * 通过可见文本值来选择select * @param expectedPageKeyword 预期页面 * @param expectedUIElementKeyword 预期关键字 * @param text 可见文本值 */ public static void selectByVisibleText(String expectedPageKeyword,String expectedUIElementKeyword,String text) { logger.info("通过可见文本值来选择select,select所在关键页面是:【"+expectedPageKeyword+"】,所在关键元素是【"+expectedUIElementKeyword+"】,【可见文本值】:【"+text+"】"); Select select = new Select(waitPresenceOfElementLocated(expectedPageKeyword, expectedUIElementKeyword)); select.selectByVisibleText(text); } /** * 确认alter弹框 */ public static void acceptAlert() { logger.info("确认alter弹框"); Alert alert = getBroswer().switchTo().alert(); alert.accept(); } /** * 关闭浏览器 */ public static void closeBrowser() { logger.info("关闭浏览器"); getBroswer().quit(); } /** * 取消alter弹框 */ public static void dismissAlert() { logger.info("取消alter弹框"); Alert alert = getBroswer().switchTo().alert(); alert.dismiss(); } /** * 检查给定元素是否被选中的期望值。 * @param locator By 对象定位信息 * @param selected 预期的选中状态 * @return 返回true,实际选中状态和预期选中状态一致,反之返回false */ public static Boolean checkElementSelectionStateToBe(By locator,boolean selected) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,检查select是否被选中,【预期选中状态】:【"+selected+"】"); logger.info("返回的实际选中状态【"+ getWait().until(ExpectedConditions.elementSelectionStateToBe(locator, selected))+"】"); return getWait().until(ExpectedConditions.elementSelectionStateToBe(locator, selected)); } /** * 检查给定元素是否被选中的期望值。 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @return 返回true,实际选中状态和预期选中状态一致,反之返回false */ public static Boolean checkElementSelectionStateToBe(String expectedPageKeyword,String expectedUIElementKeyword,boolean selected ) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,检查select是否被选中"); By locator = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,检查select是否被选中"); logger.info("返回的实际选中状态【"+ getWait().until(ExpectedConditions.elementSelectionStateToBe(locator, selected))+"】"); return getWait().until(ExpectedConditions.elementSelectionStateToBe(locator, selected)); } /** * 检查给定元素是否被选中的期望值。 * @param element 需要判断的元素 * @param selected 预期的选中状态 * @return 返回true,实际选中状态和预期选中状态一致,反之返回false */ public static Boolean checkElementSelectionStateToBe(WebElement element,boolean selected) { logger.info("获取【element元素信息】为:【"+element.toString()+"】,检查select是否被选中,【预期选中状态】:【"+selected+"】"); logger.info("返回的实际选中状态【"+ getWait().until(ExpectedConditions.elementSelectionStateToBe(element, selected))+"】"); return getWait().until(ExpectedConditions.elementSelectionStateToBe(element, selected)); } /** * 检查给定元素是否被选中。 * @param locator By 对象定位信息 * @return 已选择返回true,反之返回false */ public static Boolean checkElementToBeSelected(By locator) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,检查select被选中"); logger.info("返回的实际选中状态【"+ getWait().until(ExpectedConditions.elementToBeSelected(locator))+"】"); return getWait().until(ExpectedConditions.elementToBeSelected(locator)); } /** * 检查给定元素是否被选中。 * @param element 需要判断的元素 * @param selected 预期的选中状态 * @return 已选择返回true,反之返回false */ public static Boolean checkElementToBeSelected(WebElement element) { logger.info("获取【element元素信息】为:【"+element.toString()+"】,检查select被选中"); logger.info("返回的实际选中状态【"+ getWait().until(ExpectedConditions.elementToBeSelected(element))+"】"); return getWait().until(ExpectedConditions.elementToBeSelected(element)); } /** * 检查给定元素是否被选中。 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @return 已选择返回true,反之返回false */ public static Boolean checkElementToBeSelected(String expectedPageKeyword,String expectedUIElementKeyword ) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,检查select被选中"); By locator = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,检查select被选中"); logger.info("返回的实际选中状态【"+ getWait().until(ExpectedConditions.elementToBeSelected(locator))+"】"); return getWait().until(ExpectedConditions.elementToBeSelected(locator)); } /** * 获取指定元素的实际文本,是否【等于】预期的文本值 * @param locator By 对象定位信息 * @param value 预期的文本值 * @return 等于返回true,反之返回false */ public static Boolean checkTextToBe(By locator,String value) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,预期的文本值【"+value+"】,获取指定元素的实际文本,是否【等于】预期的文本值"); logger.info("获取【实际文本值】为:【"+waitPresenceOfElementLocated(locator).getText()+"】"); logger.info("返回的文本与预期的文本是否相等状态:【"+ getWait().until(ExpectedConditions.textToBe(locator,value ))+"】"); return getWait().until(ExpectedConditions.textToBe(locator, value)); } /** * 获取指定元素的实际文本,是否【等于】预期的文本值 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @param value 预期的文本值 * @return 等于返回true,反之返回false */ public static Boolean checkTextToBe(String expectedPageKeyword,String expectedUIElementKeyword,String value) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,获取指定元素的实际文本,是否【等于】预期的文本值"); By locator = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,获取指定元素的实际文本,是否等于预期的文本值"); logger.info("获取【实际文本值】为:【"+waitPresenceOfElementLocated(locator).getText()+"】"); logger.info("返回的文本与预期的文本是否相等状态:【"+ getWait().until(ExpectedConditions.textToBe(locator,value ))+"】"); return getWait().until(ExpectedConditions.textToBe(locator, value)); } /** * 获取指定元素的实际文本,是否【包含】预期的文本值 * @param locator By 对象定位信息 * @param value 预期的文本值 * @return 等于返回true,反之返回false */ public static Boolean checkTextToBePresentInElementLocated(By locator,String value) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,预期的文本值【"+value+"】,,获取指定元素的实际文本,是否【包含】预期的文本值"); logger.info("获取【实际文本值】为:【"+waitPresenceOfElementLocated(locator).getText()+"】"); logger.info("返回的文本与预期的文本是否【相等】状态:【"+ getWait().until(ExpectedConditions.textToBePresentInElementLocated(locator, value))+"】"); return getWait().until(ExpectedConditions.textToBePresentInElementLocated(locator, value)); } /** * 获取指定元素的实际文本,是否【包含】预期的文本值 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @param value 预期的文本值 * @return 等于返回true,反之返回false */ public static Boolean checkTextToBePresentInElementLocated(String expectedPageKeyword,String expectedUIElementKeyword,String value) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,获取指定元素的实际文本,是否【包含】等于预期的文本值"); By locator = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,获取指定元素的实际文本,是否【包含】等于预期的文本值"); logger.info("获取【实际文本值】为:【"+waitPresenceOfElementLocated(locator).getText()+"】"); logger.info("返回的文本与预期的文本是否【包含】状态:【"+ getWait().until(ExpectedConditions.textToBePresentInElementLocated(locator,value ))+"】"); return getWait().until(ExpectedConditions.textToBePresentInElementLocated(locator, value)); } /** * 获取指定元素的实际文本,是否【包含】预期的文本值 * @param element 指定元素 * @param value 预期的文本值 * @return 等于返回true,反之返回false */ public static Boolean checkTextToBePresentInElementLocated(WebElement element,String value) { logger.info("获取【element元素信息】为:【"+element.toString()+"】,预期的文本值【"+value+"】,获取指定元素的实际文本值获取指定元素的实际文本,是否【包含】等于预期的文本值"); logger.info("获取【实际文本值】为:【"+element.getText()+"】"); logger.info("返回的文本与预期的文本是否【包含】状态:【"+ getWait().until(ExpectedConditions.textToBePresentInElement(element, value))+"】"); return getWait().until(ExpectedConditions.textToBePresentInElement(element, value)); } /** * 获取指定元素的实际value属性值,是否【包含】预期的value属性值 * @param element 指定元素 * @param value 预期value * @return 等于返回true,反之返回false */ public static Boolean checkTextToBePresentInElementValue(WebElement element,String value) { logger.info("获取【element元素信息】为:【"+element.toString()+"】,预期的value值【"+value+"】,获取指定元素的实际value值获取指定元素的实际文本,是否【包含】等于预期的value值"); logger.info("获取【实际value值】为:【"+element.getAttribute("value")+"】"); logger.info("返回的文本与预期的文本是否【包含】状态:【"+ getWait().until(ExpectedConditions.textToBePresentInElementValue(element, value))+"】"); return getWait().until(ExpectedConditions.textToBePresentInElementValue(element, value)); } /** * 获取指定元素的实际value属性值,是否【包含】预期的value属性值 * @param locator By 对象定位信息 * @param value 预期value值 * @return 等于返回true,反之返回false */ public static Boolean checkTextToBePresentInElementValue(By locator,String value) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,预期的文本值【"+value+"】,获取指定元素的实际value值,获取指定元素的实际value属性值,是否【包含】等于预期的value属性值"); logger.info("获取【实际value值】为:【"+waitPresenceOfElementLocated(locator).getAttribute("value")+"】"); logger.info("返回的实际value值与预期的value值是否【包含】状态:【"+ getWait().until(ExpectedConditions.textToBePresentInElementValue(locator, value))+"】"); return getWait().until(ExpectedConditions.textToBePresentInElementValue(locator, value)); } /** * 获取指定元素的实际value属性值,是否【包含】预期的value属性值 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @param value 预期的文本值 * @return 等于返回true,反之返回false */ public static Boolean checkTextToBePresentInElementValue(String expectedPageKeyword,String expectedUIElementKeyword,String value) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,【预期的属性值】:【"+value+"】获取指定元素的实际value属性值,是否【包含】等于预期的value属性值"); By locator = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,获取指定元素的实际value属性值,是否【包含】等于预期的value属性值"); logger.info("获取【实际value值】为:【"+waitPresenceOfElementLocated(locator).getAttribute("value")+"】"); logger.info("返回的实际value值与预期的value值是否【包含】状态:【"+ getWait().until(ExpectedConditions.textToBePresentInElementValue(locator,value ))+"】"); return getWait().until(ExpectedConditions.textToBePresentInElementValue(locator, value)); } /** * 获取指定元素的实际属性,是否【包含】预期的属性值 * @param element 指定元素 * @param attribute 指定属性名 * @param value 预期value * @return 等于返回true,反之返回false */ public static Boolean checkAttributeContains(WebElement element,String attribute,String value) { logger.info("获取【element元素信息】为:【"+element.toString()+"】,指定的【属性名】:【"+attribute+"】,【预期的属性值】:"+value+",获取指定元素的实际属性是否【包含】等于预期的属性值"); logger.info("获取【实际属性值】为:【"+element.getAttribute(attribute)+"】"); logger.info("返回的文本与预期的文本是否【包含】状态:【"+ getWait().until(ExpectedConditions.attributeContains(element, attribute, value))+"】"); return getWait().until(ExpectedConditions.attributeContains(element, attribute, value)); } /** * 检查alter弹框是否存在 * @return true存在,false不存在 */ public boolean checkAlertIsPresent() { try { logger.info("开始检查alter是否存在"); getWait().until(ExpectedConditions.alertIsPresent()); logger.info("开始检查alter存在"); return true; }catch (Exception e) { logger.info("开始检查alter不存在"); return false; } } /** * 检查页面元素是否可见 * @param locator * @return 如果返回为true,页面不可见或者元素不再dom中,返回false,页面可见 */ public static boolean checkInvisibilityOfElementLocated(By locator) { logger.info("检查元素是否不可见或页面不存在,【元素信息】:【"+locator.toString()+"】"); logger.info("检查元素是否不可见或页面的状态:【"+getWait().until(ExpectedConditions.invisibilityOfElementLocated(locator))+"】"); return getWait().until(ExpectedConditions.invisibilityOfElementLocated(locator)); } /** * 获取指定元素的实际属性,是否【包含】预期的属性值 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @param attribute 指定属性名 * @param value 预期的文本值 * @return 等于返回true,反之返回false */ public static Boolean checkAttributeContains(String expectedPageKeyword,String expectedUIElementKeyword,String attribute,String value) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,指定的【属性名】:【"+attribute+"】,【预期的属性值】:"+value+",获取指定元素的实际属性是否【包含】等于预期的属性值"); By locator = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,获取指定元素的实际属性值,是否【包含】等于预期的属性值"); logger.info("获取【实际属性值】为:【"+waitPresenceOfElementLocated(locator).getAttribute(attribute)+"】"); logger.info("返回的实际属性值与预期的属性值是否【包含】状态:【"+ getWait().until(ExpectedConditions.attributeContains(locator, attribute, value))+"】"); return getWait().until(ExpectedConditions.attributeContains(locator, attribute, value)); } /** * 获取指定元素的实际属性,是否【包含】预期的属性值 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @param attribute 指定属性名 * @param value 预期的文本值 * @return 等于返回true,反之返回false */ public static Boolean checkAttributeContains(By locator,String attribute,String value) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,【指定的属性名】:【"+attribute+"】,【指定的预期属性值】:【"+value+"】,获取指定元素的实际属性值,是否【包含】等于预期的属性值"); logger.info("获取【实际属性值】为:【"+waitPresenceOfElementLocated(locator).getAttribute(attribute)+"】"); logger.info("返回的实际属性值与预期的属性值是否【包含】状态:【"+ getWait().until(ExpectedConditions.attributeContains(locator, attribute, value))+"】"); return getWait().until(ExpectedConditions.attributeContains(locator, attribute, value)); } /** * 获取指定元素的实际属性,是否【等于】预期的属性值 * @param element 指定元素 * @param attribute 指定属性名 * @param value 预期value * @return 等于返回true,反之返回false */ public static Boolean checkAttributeToBe(WebElement element,String attribute,String value) { logger.info("获取【element元素信息】为:【"+element.toString()+"】,指定的【属性名】:【"+attribute+"】,【预期的属性值】:"+value+",获取指定元素的实际属性是否【等于】等于预期的属性值"); logger.info("获取【实际属性值】为:【"+element.getAttribute(attribute)+"】"); logger.info("返回的文本与预期的文本是否【等于】状态:【"+ getWait().until(ExpectedConditions.attributeToBe(element, attribute, value))+"】"); return getWait().until(ExpectedConditions.attributeToBe(element, attribute, value)); } /** * 获取指定元素的实际属性,是否【等于】预期的属性值 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @param attribute 指定属性名 * @param value 预期的文本值 * @return 等于返回true,反之返回false */ public static Boolean checkAttributeToBe(String expectedPageKeyword,String expectedUIElementKeyword,String attribute,String value) { logger.info("获取【页面关键字】为:【"+expectedPageKeyword+"】,获取【元素关键字】为:【"+expectedUIElementKeyword+"】,指定的【属性名】:【"+attribute+"】,【预期的属性值】:"+value+",获取指定元素的实际属性是否【等于】等于预期的属性值"); By locator = getXmlBy(expectedPageKeyword, expectedUIElementKeyword); logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,获取指定元素的实际属性值,是否【等于】等于预期的属性值"); logger.info("获取【实际属性值】为:【"+waitPresenceOfElementLocated(locator).getAttribute(attribute)+"】"); logger.info("返回的实际属性值与预期的属性值是否【等于】状态:【"+ getWait().until(ExpectedConditions.attributeToBe(locator, attribute, value))+"】"); return getWait().until(ExpectedConditions.attributeToBe(locator, attribute, value)); } /** * 获取指定元素的实际属性,是否【等于】预期的属性值 * @param expectedPageKeyword 预期页面关键在 * @param expectedUIElementKeyword 预期元素关键字 * @param attribute 指定属性名 * @param value 预期的文本值 * @return 等于返回true,反之返回false */ public static Boolean checkAttributeToBe(By locator,String attribute,String value) { logger.info("获取【locator定位信息】为:【"+locator.toString()+"】,【指定的属性名】:【"+attribute+"】,【指定的预期属性值】:【"+value+"】,获取指定元素的实际属性值,是否【等于】等于预期的属性值"); logger.info("获取【实际属性值】为:【"+waitPresenceOfElementLocated(locator).getAttribute(attribute)+"】"); logger.info("返回的实际属性值与预期的属性值是否【等于】状态:【"+ getWait().until(ExpectedConditions.attributeToBe(locator, attribute, value))+"】"); return getWait().until(ExpectedConditions.attributeToBe(locator, attribute, value)); } // public void test() throws InterruptedException { // //把“随意拖拽1-1”元素移动到“随意拖拽1-2”节点下去 // WebElement webElement1 = BrowserUtil.driver.findElement(By.id("treeDemo_2_span")); // WebElement webElement2= BrowserUtil.driver.findElement(By.id("treeDemo_3_span")); // //构建鼠标的一连串动作 // Actions actions = new Actions(BrowserUtil.driver); // actions.clickAndHold(webElement1).moveToElement(webElement2).release().build().perform(); // } public static By getXmlBy(String path ,String expectedPageKeyword,String expectedUIElementKeyword) { try { String uiElementBy = ""; String uiElementValue = ""; for(Page page:XmlUtils.loadXml(path)) { String actualPageKeyword = page.getKeyword(); if (expectedPageKeyword.equals(actualPageKeyword)) { for (UIElement uiElement : page.getUiElementList()) { String actualUiElement = uiElement.getKeyword(); if (expectedUIElementKeyword.equals(actualUiElement)) { uiElementBy = uiElement.getBy(); uiElementValue = uiElement.getValue(); } } } } Class<By> clazz = By.class; Method method = clazz.getMethod(uiElementBy, String.class); By by =(By) method.invoke(null, uiElementValue); return by;}catch (Exception e) { e.printStackTrace(); } return null; } public static By getXmlBy(String expectedPageKeyword,String expectedUIElementKeyword) { try { String uiElementBy = ""; String uiElementValue = ""; for(Page page:XmlUtils.loadXml("")) { String actualPageKeyword = page.getKeyword(); if (expectedPageKeyword.equals(actualPageKeyword)) { for (UIElement uiElement : page.getUiElementList()) { String actualUiElement = uiElement.getKeyword(); if (expectedUIElementKeyword.equals(actualUiElement)) { uiElementBy = uiElement.getBy(); uiElementValue = uiElement.getValue(); } } } } Class<By> clazz = By.class; Method method = clazz.getMethod(uiElementBy, String.class); By by =(By) method.invoke(null, uiElementValue); return by; }catch (Exception e) { e.printStackTrace(); } return null; } //=======================================Js部分========================================================= /** * Selenium定位的元素被图层遮挡,click无法操作,可以用此方法点击 * @param webElement 需要操作的元素 */ public static void clickByJs(WebElement webElement) { logger.info(webElement.toString()); getJs().executeScript("arguments[0].click();", webElement); } /** * 去除元素只读属性 * @param webElement 需要操作的元素 */ public static void removeAttributeReadonly(WebElement webElement) { getJs().executeScript("arguments[0].removeAttribute('readonly')",webElement); } /** * 文本输入框赋值 * @param webElement 需要操作的元素 * @param input 期望输入的值 * 注意: JS企图去控制输入框的值失效,是因为没有触发input或change事件(有些前端会设置) */ public static void modifyValueByJs(WebElement webElement,String input) { getJs().executeScript("arguments[0].value=arguments[1]",webElement,input); } /** * 设置富文本框的内容 * @param webElement 需要操作的元素 * @param input 期望输入的值 */ public static void modifyInnerHTMLByJs(WebElement webElement,String input) { getJs().executeScript("arguments[0].innerHTML = arguments[1]" ,webElement,input); } /** * 利用js代码取出关键字 * @param webElement 需要操作的元素 * @return 返回获取的value值 */ public static String getValueByJs(WebElement webElement) { return (String) getJs().executeScript("var result=arguments[0].value;return result",webElement); } /** * 用js判断页面加载完毕,返回complete * @return 返回complete */ public static String judgeLoadComplete() { return getJs().executeScript("return document.readyState").toString(); } /** * 获取富文本框内容 * @param webElement 需要操作的元素 * @return 返回获取的innerHTML */ public static String getInnerHTMLByJs(WebElement webElement) { return (String) getJs().executeScript("var result=arguments[0].innerHTML;return result" ,webElement); } /** * 滑动页面到指定元素的顶部对其 * @param webElement 需要操作的元素 */ public static void scrollIntoTopView(WebElement webElement) { getJs().executeScript("arguments[0].scrollIntoView(true);",webElement); } /** * 滑动页面到指定元素的顶部底部 * @param webElement 需要操作的元素 */ public static void scrollInTobottomView(WebElement webElement) { getJs().executeScript("arguments[0].scrollIntoView(false);",webElement); } /** * 元素滚动至可见位置_如果当前可见,则不会发生滚动 * @param webElement 需要操作的元素 */ public static void scrollIntoViewIfNeeded(WebElement webElement) { getJs().executeScript("arguments[0].scrollIntoViewIfNeeded(true);",webElement); } /** * 封装设置或者新增页面对象的属性值的方法 调用JS代码修改页面元素的属性值,arguments[0]~arguments[1]分别 * 会用后面的element,attributeName和value参数进行替换 * @param webElement 需要操作的元素 * @param attributeName 属性名 * @param attributeValue 属性值 */ public static void setAttribute(WebElement webElement,String attributeName,String attributeValue) { getJs().executeScript("arguments[0].setAttribute(\""+attributeName+"\",\""+attributeValue+"\")",webElement,attributeName,attributeValue); } /** * 删除元素指定元素属性 * @param webElement 需要操作的元素 * @param attributeName 属性名 */ public static void removeAttribute(WebElement webElement,String attributeName) { getJs().executeScript("arguments[0].removeAttribute(arguments[1])",webElement,attributeName); } /** * 获取指定元素对应的属性值 * @param webElement 需要操作的元素 * @param attributeName 属性名 * @return 返回指定元素对应的属性值 */ public static String getAttribute(WebElement webElement,String attributeName) { logger.info("浏览器信息和获取的元素信息:【"+webElement.toString()+"】,获取该元素属性名为【"+attributeName+"】,属性值值为【"+(String)getJs().executeScript("return arguments[0].getAttribute(arguments[1])",webElement,attributeName)+"】"); return (String)getJs().executeScript("return arguments[0].getAttribute(arguments[1])",webElement,attributeName); } /** * 元素高亮展示 #边框border:2px; blue红色 * @param webElement 需要操作的元素 */ public static void setHighLight(WebElement webElement) { logger.info("浏览器信息和获取的元素信息:【"+webElement.toString()+"】,设置了【style】属性,其属性值为【border: 10px solid blue;】,其目的是高亮显示元素"); setAttribute(webElement, "style", "border: 10px solid blue;"); } //=======================================正则表达式替换修改xml部分=========================================== /** * 替换${格式的信息} * @param expectedPageKeyword * @param expectedUIElementKeyword * @param modifyValue */ public static void replaceValue(String expectedPageKeyword,String expectedUIElementKeyword,String modifyValue) { // XmlUtils.(expectedPageKeyword, expectedUIElementKeyword, modifyValue, "\\$\\{.+\\}"); } /** * 替换中文字符为S{XXX} * @param expectedPageKeyword * @param expectedUIElementKeyword * @param modifyValue */ public static void replaceChinese(String expectedPageKeyword,String expectedUIElementKeyword,String modifyValue) { // XmlUtil.modifyXmlInfo(expectedPageKeyword, expectedUIElementKeyword, modifyValue, "[\\x{4e00}-\\x{9fa5}]+"); } //机动车综合险2020版 /** * 将中文字符替换为 S{XXX} * @param expectedPageKeyword * @param expectedUIElementKeyword * @param modifyValue */ public static void replaceChineseNum(String expectedPageKeyword,String expectedUIElementKeyword,String modifyValue) { // XmlUtil.modifyXmlInfo(expectedPageKeyword, expectedUIElementKeyword, modifyValue, "[0-9\\x{4e00}-\\x{9fa5}]+"); } /** * 匹配中文和带-的 * @param expectedPageKeyword * @param expectedUIElementKeyword * @param modifyValue */ public static void replaceChineseNum1(String expectedPageKeyword,String expectedUIElementKeyword,String modifyValue) { // XmlUtil.modifyXmlInfo(expectedPageKeyword, expectedUIElementKeyword, modifyValue, "[\\\\-\\x{4e00}-\\x{9fa5}]+"); } }