首页 > 代码库 > WebDriver自动化测试常用处理方法(持续更新)
WebDriver自动化测试常用处理方法(持续更新)
- 获取当前时间
public static String getTime(){ Date date=new Date(); DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return "<b>"+format.format(date)+"</b>"; }
- 等待页面刷新(10秒内),获取页面刷新所需时间
public static boolean waitPageRefresh(WebElement trigger) { int refreshTime = 0; boolean isRefresh = false; try { for (int i = 1; i < 10; i++) { refreshTime = i; trigger.getTagName(); Thread.sleep(1000); } } catch (StaleElementReferenceException e) { isRefresh = true; System.out.println("Page refresh time is:" + refreshTime + " seconds!"); Reporter.log("<pre><font color=‘#000000‘>【系统日志】\t\tPage refresh time is " + refreshTime + " seconds!</font></pre>"); return isRefresh; } catch (WebDriverException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Page didnt refresh in 10 seconds!"); Reporter.log("<pre><font color=‘#000000‘>【系统日志】\t\tPage didnt refresh in 10 seconds!</font></pre>"); return isRefresh; }
- 让程序睡眠
public static void pauseDriver(int time){ if(time<0 || time>10){ time = 10; } for (int i = 1; i < time; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }
- 判断是否会有弹窗
public static boolean isAlert(WebDriver driver){ try{ driver.switchTo().alert(); sysLog("当前页面出现弹窗"); System.out.println("有弹窗"); return true; } catch(NoAlertPresentException e){ System.out.println("没有弹窗"); return false; } }
- 判断元素是否存在
public static boolean isWebElementExist(WebDriver driver, By selector){ try { driver.findElement(selector); System.out.println("元素存在"); return true; } catch (NoSuchElementException e) { System.out.println("元素不存在"); return false; } }
- 使页面滚动到最底端
private static void getPageEnd(WebDriver driver){ String js = "window.scrollTo(0,document.body.scrollHeight);"; JavascriptExecutor jsDriver = (JavascriptExecutor) driver; jsDriver.executeScript(js); System.out.println("执行JS语句,控制页面滚动条滚动到页面底端"); }
- 让鼠标在元素上方悬停
private static void moveToElement(WebDriver driver, By locator) { Actions builder = new Actions(driver); builder.moveToElement(driver.findElement(locator)).perform(); }
- 判断下拉框内容是否为空
public static Boolean isSelectEmpty(WebDriver driver, String selectXpath){ Select select = new Select(driver.findElement(By.xpath(selectXpath))); return select.getOptions().isEmpty(); }
WebDriver自动化测试常用处理方法(持续更新)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。