WebDriver自动化测试常用处理方法
Posted 程序员二黑.
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();
最后:下方这份完整的【软件测试】视频学习教程已经整理上传完成,朋友们如果需要可以自行免费领取 【保证100%免费】
以上是关于WebDriver自动化测试常用处理方法的主要内容,如果未能解决你的问题,请参考以下文章
从零开始到设计UI自动化测试框架---webdriver常用方法
自动化测试Selenium Webdriver (JAVA)学习小结