WebDriver自动化测试常用处理方法(持续更新)
Posted unique1319
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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自动化测试常用处理方法(持续更新)的主要内容,如果未能解决你的问题,请参考以下文章
从零开始到设计UI自动化测试框架---webdriver常用方法
nightwatchjs 基于nodejs&& webdriver 协议的自动化测试&&持续集成框架