【selenium】常用操作的封装

简介:总结整理selenium常用操作的封装,包括对元素操作的封装和页面基础操作封装,方便在写自动化用例时直接调用。

元素操作的封装

  • OperateElement.java
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;
import java.util.List;

/*
 *作者:灵枢
 *时间:2018-11-22  下午4:45:15
 *描述:元素的基本操作
 **/

public class OperateElement {
	private static int timeOutInSeconds = 20;

	/**
	 * 通过元素的Xpath,等待元素的出现,返回此元素
	 * @param driver
	 * @return 返回等待的元素
	 */
	public static WebElement waitByXpath(String xpath,WebDriver driver){
		WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
		//presenceOfElementLocated:显示等待元素在页面中存在,不用等页面全部加载完
		WebElement targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(xpath)));
		return targetElement;
	}
	
	/**
	 * 通过元素的name,等待元素的出现,返回此元素
	 * @param driver
	 * @param name 元素的name
	 * @return 返回等待的元素
	 */
	public static WebElement waitByName(String name,WebDriver driver){
		WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
		WebElement	targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.name(name)));
		return targetElement;
	}
	
	/**
	 * 通过元素的id,等待元素的出现,返回此元素
	 * @param driver
	 * @param id 元素的id
	 * @return 返回等待的元素
	 */
	public static WebElement waitById(String id,WebDriver driver){
		WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
		WebElement	targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.id(id)));
		return targetElement;
	}
	
	/**
	 * 通过元素的linkText,等待元素的出现,返回此元素
	 * @param driver
	 * @param linkText 元素的linkText
	 * @return 返回等待的元素
	 */
	public static WebElement waitByLinkText(String linkText,WebDriver driver){
		WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
		WebElement targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText(linkText)));
		return targetElement;
	}
	
	/**
	 * 通过元素的className,等待元素的出现,返回此元素
	 * @param driver
	 * @param className 元素的className
	 * @return 返回等待的元素
	 */
	public static WebElement waitByClassName(String className,WebDriver driver){
		WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
		WebElement targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.className(className)));
		return targetElement;
	}
	
	/**
	 * 通过元素的Css,等待元素的出现,返回此元素
	 * @param driver
	 * @return 返回等待的元素
	 */
	public static WebElement waitByCss(String css,WebDriver driver){
		WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
		WebElement targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(css)));
		return targetElement;
	}

	/**
	 * 通过元素的className,等待元素列表的出现,返回此元素列表
	 *
  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值