java【selenium】封装显示等待通用方法

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

/*

  • 封装类
  • 查找单个元素,或者查找元素集合
    */

public class EncapsulationElement {
WebDriver driver;
//初始化构造函数时,带着WebDriver driver
public EncapsulationElement(WebDriver driver) {
this.driver=driver;

}

//封装单个元素
public WebElement gElement(String locator,String type) {
	//将传入字符串全部转换为小写
	type = type.toLowerCase();
	if (type.equals("id")) {
		System.out.println("用id查找元素" + locator);
		return this.driver.findElement(By.id(locator));
	} else if (type.equals("xpath")) {
		System.out.println("用xpath查找元素" + locator);
		return this.driver.findElement(By.xpath(locator));
	} else if (type.equals("name")) {
		System.out.println("用name查找元素: " + locator);
		return this.driver.findElement(By.name(locator));
	} else if (type.equals("css")) {
		System.out.println("用css查找元素: " + locator);
		return this.driver.findElement(By.cssSelector(locator));
	} else if (type.equals("classname")) {
		System.out.println("用classname查找元素: " + locator);
		return this.driver.findElement(By.className(locator));
	} else if (type.equals("tagname")) {
		System.out.println("用tagname查找元素: " + locator);
		return this.driver.findElement(By.tagName(locator));
	} else if (type.equals("linktext")) {
		System.out.println("用linktext查找元素: " + locator);
		return this.driver.findElement(By.linkText(locator));
	} else if (type.equals("partiallinktext")) {
		System.out.println("用partiallinktext查找元素: " + locator);
		return this.driver.findElement(By.partialLinkText(locator));
	} else {
		System.out.println("你输入查找元素方式不支持,或属性已更改,请校验");
		return null;
	}
	
	
	
}

//封装集合元素

public List<WebElement> gElementsList(String locator,String type){
	//之前是返回查找元素的集合return this.driver.findElements(By.id(locator)); 直接返回一个集合

// type=type.toLowerCase();
// if (type.equals(“id”)) {
// System.out.println(“用id查找元素” + locator);
// return this.driver.findElements(By.id(locator));
// } else if (type.equals(“xpath”)) {
// System.out.println(“用xpath查找元素” + locator);
// return this.driver.findElements(By.xpath(locator));
// } else if (type.equals(“name”)) {
// System.out.println("用name查找元素: " + locator);
// return this.driver.findElements(By.name(locator));
// } else if (type.equals(“css”)) {
// System.out.println("用css查找元素: " + locator);
// return this.driver.findElements(By.cssSelector(locator));
// } else if (type.equals(“classname”)) {
// System.out.println("用classname查找元素: " + locator);
// return this.driver.findElements(By.className(locator));
// } else if (type.equals(“tagname”)) {
// System.out.println("用tagname查找元素: " + locator);
// return this.driver.findElements(By.tagName(locator));
// } else if (type.equals(“linktext”)) {
// System.out.println(“用linktext查找元素: " + locator);
// return this.driver.findElements(By.linkText(locator));
// } else if (type.equals(“partiallinktext”)) {
// System.out.println(“用partiallinktext查找元素: " + locator);
// return this.driver.findElements(By.partialLinkText(locator));
// } else {
// System.out.println(“定位的路径不支持”);
// return null;
// }
//代码重构
type = type.toLowerCase();
List elementList = new ArrayList();
if (type.equals(“id”)) {
elementList = this.driver.findElements(By.id(locator));
}
else if (type.equals(“name”)) {
elementList = this.driver.findElements(By.name(locator));
}
else if (type.equals(“xpath”)) {
elementList = this.driver.findElements(By.xpath(locator));
}
else if (type.equals(“css”)) {
elementList = this.driver.findElements(By.cssSelector(locator));
}
else if (type.equals(“classname”)) {
elementList = this.driver.findElements(By.className(locator));
}
else if (type.equals(“tagname”)) {
elementList = this.driver.findElements(By.tagName(locator));
}
else if (type.equals(“linktext”)) {
elementList = this.driver.findElements(By.linkText(locator));
}
else if (type.equals(“partiallinktext”)) {
elementList = this.driver.findElements(By.partialLinkText(locator));
}
else {
System.out.println(“定位的类型不支持”);
}
if (elementList.isEmpty()) {
System.out.println(“用 " + type +”: " + locator+” 没有找到元素”);

	} else {
		System.out.println("元素用  " + type +": " + locator+" 找到了");
	}
	return elementList;

	
}
//判断元素是否存在
public boolean IselementIn(String locator ,String type) {
	//判断元素是否存在在页面上
	List<WebElement> elemntList=gElementsList(locator, type);
	int size=elemntList.size();
	if (size>0) {
		System.out.println("查找元素正确");
		return true;
		
		
	}else {
		System.out.println("查找元素失败,请校验数据正确性");

		return false;
		
	}
	
	
	
	
	
}
//封装显示等待方法

	public WebElement waitforelement(By locaor,int timeout ) {
		WebElement element =null;
		try {
			System.out.println("最长等待了"+timeout+"元素可用");
			//设置显示等待
			WebDriverWait wait = new WebDriverWait(driver, 3);
			WebElement emailField=wait.until(ExpectedConditions.visibilityOfElementLocated(locaor));
			
		} catch (Exception e) {
			System.out.println("元素没有在页面出现");
		}
		
		return element;
		
	}
	//元素点击后才会出现
	public WebElement clickheReady(By locaor,int timeout ) {
		
		WebElement element =null;
		try {
			System.out.println("元素等待了"+timeout+"可以点击");
			WebDriverWait wait=new WebDriverWait(driver, 3);
			element=wait.until(ExpectedConditions.elementToBeClickable(locaor));
			element.click();
			System.out.println("元素在页面上点击了");
			//捕获异常
		} catch (Exception e) {
			System.out.println("元素在页面没有出现");
		}
		
		return element;
	}

}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值