多测师是一家拥有先进的教学理念,强大的师资团队,业内好评甚多的接口自动化测试培训机构!

17727591462

联系电话

您现在所在位置:接口自动化测试培训 > 新闻资讯

Selenium之常用方法的封装-自动化测试

更新时间:2022-04-25 09:21:01 作者:多测师 浏览:161

  一.概述

  selenium在编写页面中函数的时候,需要调用的某些方法是很多PageObject都需要调用到的,而且这些原始的方法需要进行封装,以便使脚本更好的实现功能,那么有哪些方法是需要经常用到的呢?

Selenium之常用方法的封装-自动化测试

  二.selenium封装的常用方法

  1 /**

  2 * 判断是否找到对象

  3 * @param by

  4 * @return

  5 */

  6 private boolean waitToDisplayed(final By by){

  7 boolean waitDisplayed=false;

  8 try {

  9 waitDisplayed=new WebDriverWait(driver,Config.waitTime).until(new ExpectedCondition(){

  10 @Override

  11 public Boolean apply(WebDriver d) {

  12 // TODO Auto-generated method stub

  13 return d.findElement(by).isDisplayed();

  14 }

  15 });

  16

  17 } catch (Exception e) {

  18 // TODO: handle exception

  19 throw new DefinedException(by.toString() + " is not exist until " +Config.waitTime+" sec in file: "+yamlFile);

  20 }

  21 return waitDisplayed;

  22 }


  1 /**

  2 * 判断对象是否没出现

  3 * @param key

  4 * @param replace

  5 * @return

  6 */

  7 private boolean waitToNonDisplayed(String key,String[] replace){

  8 boolean waitNonDisplayed=false;

  9 final By by=this.getBy(key, replace);

  10 try {

  11 waitNonDisplayed=new WebDriverWait(driver,Config.waitTime).until(new ExpectedCondition(){

  12 @Override

  13 public Boolean apply(WebDriver d) {

  14 // TODO Auto-generated method stub

  15 if(d.findElements(by).isEmpty()||!d.findElement(by).isDisplayed()){

  16 return true;

  17 }else{

  18 return false;

  19 }

  20 }

  21 });

  22 } catch (Exception e) {

  23 // TODO: handle exception

  24 throw new DefinedException(by.toString() + " is also exist until " +Config.waitTime+" sec in file: "+yamlFile);

  25 }

  26 return waitNonDisplayed;

  27 }


  1 /**

  2 * 判断对象是否存在

  3 * @param key

  4 * @param replace

  5 * @return

  6 */

  7 public boolean isExist(String key,String[] replace){

  8 By by=this.getBy(key, replace);

  9 return driver.findElements(by).size()>0?true:false;

  10 }

  1 public void sleep(int sec){

  2 try {

  3 Thread.sleep(sec*1000);

  4 } catch (InterruptedException e) {

  5 e.printStackTrace();

  6 }

  7 }

  1 public void click(){

  2 log.info("click the element:"+key);

  3 element.click();

  4 }

  5

  6 public void sendKeys(String value){

  7 log.info("type the value:"+value+" to element:"+key);

  8 element.sendKeys(value);

  9 }

  10

  11 public void selectKeys(String text){

  12 log.info("select the text:"+text+" to element:"+key);

  13 Select select=new Select(element);

  14 select.selectByVisibleText(text);

  15 }

  1 public void moveToElement(){

  2 log.info("move to element: "+key);

  3 Actions action = new Actions(driver);

  4 action.moveToElement(element).perform();

  5 }

  6

  7 public void executeJS(String js){

  8 log.info("execute the js: "+js+" at element: "+key);

  9 ((JavascriptExecutor)driver).executeScript(js, element);

  10 }

  11

  12 public void waitClickToLoad(){

  13 log.info("click the element:"+key);

  14 WebDriverWait waitLoad = new WebDriverWait(driver,Config.waitTime);

  15 waitLoad.until(new ExpectedCondition(){

  16 @Override

  17 public WebElement apply(WebDriver d) {

  18 return element;

  19 }}).click();

  20 }

  21

  22 public void switchToFrame(int index){

  23 if(index==-1){

  24 log.info("返回默认窗口");

  25 driver.switchTo().defaultContent();

  26 log.info("返回默认窗口成功");

  27 }else{

  28 log.info("切换到第一个frame");

  29 driver.switchTo().frame(index);

  30 }

  31 }

  以上内容为大家介绍了自动化测试中的Selenium之常用方法的封装,本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/

联系电话

17727591462

返回顶部