20181128_selenium_java问题集锦(3)_element定位方法的封装

本文分享了在使用Selenium Java进行自动化测试时,对元素定位方法的封装,旨在简化非空判断操作,提高脚本的可读性和易维护性。同时,讨论了如何通过日志增强脚本的错误定位能力,欢迎大家提出优化建议,共同进步。
摘要由CSDN通过智能技术生成

1、对于selenium_java还是封装一下element的定位方法吧,否则,每次查找元素后都非空判断一下好麻烦。

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.log4testng.Logger;

import cn.com.cnpc.richfit.cloud.resourceMan.testcase.CloudTestBase;
import cn.com.cnpc.richfit.cloud.resourceMan.utils.CommonProperties;
import cn.com.cnpc.richfit.cloud.resourceMan.utils.TimeUtils;

/**
 * 查找元素的工具类
 * @author lenovo
 *
 */
public class CloudElementMethods {
	public WebElement element = null;
	public List<WebElement> elementList = new ArrayList<WebElement>();
	private WebDriver driver = null;
	private static final Logger LOG = Logger.getLogger(CloudElementMethods.class);
	private static final int DELAY_NUM = 2;// 默认延迟次数
	private static final long DELAY_STEP_LENGTH = 500;// 默认延迟步长,500毫秒

	public void setDriver(WebDriver driver) {
		this.driver = driver;
	}

	/**
	 * id获取单个元素
	 * 
	 * @param id
	 * @return
	 */
	public WebElement getElementById(String id) {
		LOG.info("getElementById method called");
		Reporter.log("getElementById method called");
		System.out.println("getElementById method called");
		try {
			element = driver.findElement(By.id(id));
			if (element == null) {
				for (int i = 0; i < DELAY_NUM; i++) {
					TimeUtils.waitfor(DELAY_STEP_LENGTH);
					element = driver.findElement(By.id(id));
					if (element != null) {
						break;
					}
				}
			}
		} catch (Exception e) {
			LOG.info("Can not find element:" + id);
			Reporter.log("Can not find element:" + id);
			System.out.println("Can not find element:" + id);
			element = null;
			// Assert.fail("Can not find element:"+id);
			return element;
		}
		return element;
	}
	/**
	 * id获取单个元素-从父级元素获取
	 * 
	 * @param id
	 * @return
	 */
	public WebElement getElementById(WebElement fatherElement, String id) {
		LOG.info("getElementById method called");
		Reporter.log("getElementById method called");
		System.out.println("getElementById method called");
		try {
			element = fatherElement.findElement(By.id(id));
			if (element == null) {
				for (int i = 0; i < DELAY_NUM; i++) {
					TimeUtils.waitfor(DELAY_STEP_LENGTH);
					element = fatherElement.findElement(By.id(id));
					if (element != null) {
						break;
					}
				}
			}
		} catch (Exception e) {
			LOG.info("Can not find element:" + id);
			Reporter.log("Can not find element:" + id);
			System.out.println("Can not find element:" + id);
			element = null;
			// Assert.fail("Can not find element:"+id);
			return element;
		}
		return element;
	}

	/**
	 * classname获取单个元素
	 * 
	 * @param classname
	 * @return
	 */
	public WebElement getElementByClassName(String classname) {
		LOG.info("getElementByClassName method called");
		Reporter.log("getElementByClassName method called");
		System.out.println("getElementByClassName method called");
		try {
			element = driver.findElement(By.className(classname));
			if (element == null) {
				for (int i = 0; i < DELAY_NUM; i++) {
					TimeUtils.waitfor(DELAY_STEP_LENGTH);
					element = driver.findElement(By.className(classname));
					if (element != null) {
						break;
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			LOG.info("Can not find element:" + classname);
			Reporter.log("Can not find element:" + classname);
			System.out.println("Can not find element:" + classname);
			element = null;
			// Assert.fail("Can not find element:"+classname);
			return element;
		}
		return element;
	}
	/**
	 * classname获取单个元素-从父级元素获取
	 * 
	 * @param classname
	 * @return
	 */
	public WebElement getElementByClassName(WebElement fatherELement, String classname) {
		if (fatherELement!=null) {
			LOG.info("getElementByClassName method called");
			Reporter.log("getElementByClassName method called");
			System.out.println("getElementByClassName method called");
			try {
				element = fatherELement.findElement(By.className(classname));
				if (element == null) {
					for (int i = 0; i < DELAY_NUM; i++) {
						TimeUtils.waitfor(DELAY_STEP_LENGTH);
						element = fatherELement.findElement(By.className(classname));
						if (element != null) {
							break;
						}
					}
				}
			} catch (Exception e) {
				// TODO: handle exception
				LOG.info("Can not find element:" + classname);
				Reporter.log("Can not find element:" + classname);
				System.out.println("Can not find element:" + classname);
				element = null;
				// Assert.fail("Can not find element:"+classname);
				return element;
			}
		}
		return element;
	}

	/**
	 * classname获取多个元素
	 * 
	 * @param classname
	 * @return
	 */
	public List<WebElement> getElementsByClassName(String classname) {
		LOG.info("getElementsByClassName method called");
		Reporter.log("getElementsByClassName method called");
		System.out.println("getElementsByClassName method called");
		try {
			elementList = driver.findElements(By.className(classname));
			if (elementList == null) {
				for (int i = 0; i < DELAY_NUM; i++) {
					TimeUtils.waitfor(DELAY_STEP_LENGTH);
					elementList = driver.findElements(By.className(classname));
					if (elementList != null) {
						break;
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			LOG.info("Can not find elements:" + classname);
			Reporter.log("Can not find element:" + classname);
			System.out.println("Can not find element:" + classname);
			elementList = null;
			// Assert.fail("Can not find element:"+classname);
			return elementList;
		}
		return elementList;
	}
	/**
	 * classname获取多个元素-从父级元素获取
	 * 
	 * @param classname
	 * @return
	 */
	public List<WebElement> getElementsByClassName(WebElement fatherElement, String classname) {
		if (fatherElement!=null) {
			LOG.info("getElementsByClassName method called");
			Reporter.log("getElementsByClassName method called");
			System.out.println("getElementsByClassName method called");
			try {
				elementList = fatherElement.findElements(By.className(classname));
				if (elementList == null) {
					for (int i = 0; i < DELAY_NUM; i++) {
						TimeUtils.waitfor(DELAY_STEP_LENGTH);
						elementList = fatherElement.findElements(By.className(classname));
	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值