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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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));
						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;
	}

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

	/**
	 * name属性获取多个元素
	 * 
	 * @param name
	 * @return
	 */
	public List<WebElement> getElementsByName(String name) {
		LOG.info("getElementsByName method called");
		Reporter.log("getElementsByName method called");
		System.out.println("getElementsByName method called");
		try {
			elementList = driver.findElements(By.name(name));
			if (elementList == null) {
				for (int i = 0; i < DELAY_NUM; i++) {
					TimeUtils.waitfor(DELAY_STEP_LENGTH);
					elementList = driver.findElements(By.name(name));
					if (elementList != null) {
						break;
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			LOG.info("Can not find elements:" + name);
			Reporter.log("Can not find element:" + name);
			System.out.println("Can not find element:" + name);
			elementList = null;
			// Assert.fail("Can not find element:"+name);
			return elementList;
		}
		return elementList;
	}
	/**
	 * name属性获取多个元素-从父级元素获取
	 * 
	 * @param name
	 * @return
	 */
	public List<WebElement> getElementsByName(WebElement fatherElement, String name) {
		if (fatherElement!=null) {
			LOG.info("getElementsByName method called");
			Reporter.log("getElementsByName method called");
			System.out.println("getElementsByName method called");
			try {
				elementList = fatherElement.findElements(By.name(name));
				if (elementList == null) {
					for (int i = 0; i < DELAY_NUM; i++) {
						TimeUtils.waitfor(DELAY_STEP_LENGTH);
						elementList = fatherElement.findElements(By.name(name));
						if (elementList != null) {
							break;
						}
					}
				}
			} catch (Exception e) {
				// TODO: handle exception
				LOG.info("Can not find elements:" + name);
				Reporter.log("Can not find element:" + name);
				System.out.println("Can not find element:" + name);
				elementList = null;
				// Assert.fail("Can not find element:"+name);
				return elementList;
			}
		}
		return elementList;
	}

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

	/**
	 * 标签选择器获取多个元素
	 * 
	 * @param tagname
	 * @return
	 */
	public List<WebElement> getElementsByTagName(String tagname) {
		LOG.info("getElementsByTagName method called");
		Reporter.log("getElementsByTagName method called");
		System.out.println("getElementsByTagName method called");
		try {
			elementList = driver.findElements(By.tagName(tagname));
			if (elementList == null) {
				for (int i = 0; i < DELAY_NUM; i++) {
					TimeUtils.waitfor(DELAY_STEP_LENGTH);
					elementList = driver.findElements(By.tagName(tagname));
					if (elementList != null) {
						break;
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			LOG.info("Can not find elements:" + tagname);
			Reporter.log("Can not find element:" + tagname);
			System.out.println("Can not find element:" + tagname);
			elementList = null;
			// Assert.fail("Can not find element:"+tagname);
			return elementList;
		}
		return elementList;
	}
	/**
	 * 标签选择器获取多个元素-从父级元素获取
	 * 
	 * @param tagname
	 * @return
	 */
	public List<WebElement> getElementsByTagName(WebElement fatherElement, String tagname) {
		if (fatherElement !=null) {
			LOG.info("getElementsByTagName method called");
			Reporter.log("getElementsByTagName method called");
			System.out.println("getElementsByTagName method called");
			try {
				elementList = fatherElement.findElements(By.tagName(tagname));
				if (elementList == null) {
					for (int i = 0; i < DELAY_NUM; i++) {
						TimeUtils.waitfor(DELAY_STEP_LENGTH);
						elementList = fatherElement.findElements(By.tagName(tagname));
						if (elementList != null) {
							break;
						}
					}
				}
			} catch (Exception e) {
				// TODO: handle exception
				LOG.info("Can not find elements:" + tagname);
				Reporter.log("Can not find element:" + tagname);
				System.out.println("Can not find element:" + tagname);
				elementList = null;
				// Assert.fail("Can not find element:"+tagname);
				return elementList;
			}
		}
		return elementList;
	}

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

	/**
	 * linkText获取多个元素,匹配查询
	 * 
	 * @param text
	 * @return
	 */
	public List<WebElement> getElementsByLinkText(String text) {
		LOG.info("getElementsByLinkText method called");
		Reporter.log("getElementsByLinkText method called");
		System.out.println("getElementsByLinkText method called");
		try {
			elementList = driver.findElements(By.linkText(text));
			if (elementList == null) {
				for (int i = 0; i < DELAY_NUM; i++) {
					TimeUtils.waitfor(DELAY_STEP_LENGTH);
					elementList = driver.findElements(By.linkText(text));
					if (elementList != null) {
						break;
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			LOG.info("Can not find elements:" + text);
			Reporter.log("Can not find element:" + text);
			System.out.println("Can not find element:" + text);
			System.out.println(e.toString());
			elementList = null;
			// Assert.fail("Can not find element:"+text);
			return elementList;
		}
		return elementList;
	}
	/**
	 * linkText获取多个元素,匹配查询-从父级元素获取
	 * 
	 * @param text
	 * @return
	 */
	public List<WebElement> getElementsByLinkText(WebElement fatherElement, String text) {
		if (fatherElement!=null) {
			LOG.info("getElementsByLinkText method called");
			Reporter.log("getElementsByLinkText method called");
			System.out.println("getElementsByLinkText method called");
			try {
				elementList = fatherElement.findElements(By.linkText(text));
				if (elementList == null) {
					for (int i = 0; i < DELAY_NUM; i++) {
						TimeUtils.waitfor(DELAY_STEP_LENGTH);
						elementList = fatherElement.findElements(By.linkText(text));
						if (elementList != null) {
							break;
						}
					}
				}
			} catch (Exception e) {
				// TODO: handle exception
				LOG.info("Can not find elements:" + text);
				Reporter.log("Can not find element:" + text);
				System.out.println("Can not find element:" + text);
				System.out.println(e.toString());
				elementList = null;
				// Assert.fail("Can not find element:"+text);
				return elementList;
			}
		}
		return elementList;
	}

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

	/**
	 * PatialLinkText获取多个元素,模糊匹配
	 * 
	 * @param pttext
	 * @return
	 */
	public List<WebElement> getElementsByPtLinkText(String pttext) {
		LOG.info("getElementsByPtLinkText method called");
		Reporter.log("getElementsByPtLinkText method called");
		System.out.println("getElementsByPtLinkText method called");
		try {
			elementList = driver.findElements(By.partialLinkText(pttext));
			if (elementList == null) {
				for (int i = 0; i < DELAY_NUM; i++) {
					TimeUtils.waitfor(DELAY_STEP_LENGTH);
					elementList = driver.findElements(By.partialLinkText(pttext));
					if (elementList != null) {
						break;
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			LOG.info("Can not find elements:" + pttext);
			Reporter.log("Can not find element:" + pttext);
			System.out.println("Can not find element:" + pttext);
			elementList = null;
			// Assert.fail("Can not find element:"+pttext);
			return elementList;
		}
		return elementList;
	}
	/**
	 * PatialLinkText获取多个元素,模糊匹配-从父级元素获取
	 * 
	 * @param pttext
	 * @return
	 */
	public List<WebElement> getElementsByPtLinkText(WebElement fatherElement, String pttext) {
		if (fatherElement !=null) {
			LOG.info("getElementsByPtLinkText method called");
			Reporter.log("getElementsByPtLinkText method called");
			System.out.println("getElementsByPtLinkText method called");
			try {
				elementList = fatherElement.findElements(By.partialLinkText(pttext));
				if (elementList == null) {
					for (int i = 0; i < DELAY_NUM; i++) {
						TimeUtils.waitfor(DELAY_STEP_LENGTH);
						elementList = fatherElement.findElements(By.partialLinkText(pttext));
						if (elementList != null) {
							break;
						}
					}
				}
			} catch (Exception e) {
				// TODO: handle exception
				LOG.info("Can not find elements:" + pttext);
				Reporter.log("Can not find element:" + pttext);
				System.out.println("Can not find element:" + pttext);
				elementList = null;
				// Assert.fail("Can not find element:"+pttext);
				return elementList;
			}
		}
		return elementList;
	}

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

	/**
	 * css-selector获取多个元素
	 * 
	 * @param css
	 * @return
	 */
	public List<WebElement> getElementsByCSSSelector(String css) {
		LOG.info("getElementsByCSSSelector method called");
		Reporter.log("getElementsByCSSSelector method called");
		System.out.println("getElementsByCSSSelector method called");
		try {
			elementList = driver.findElements(By.cssSelector(css));
			if (elementList == null) {
				for (int i = 0; i < DELAY_NUM; i++) {
					TimeUtils.waitfor(DELAY_STEP_LENGTH);
					elementList = driver.findElements(By.cssSelector(css));
					if (elementList != null) {
						break;
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			LOG.info("Can not find elements:" + css);
			Reporter.log("Can not find element:" + css);
			System.out.println("Can not find element:" + css);
			elementList = null;
			// Assert.fail("Can not find element:"+css);
			return elementList;
		}
		return elementList;
	}
	/**
	 * css-selector获取多个元素-从父级元素获取
	 * 
	 * @param css
	 * @return
	 */
	public List<WebElement> getElementsByCSSSelector(WebElement fatherElement, String css) {
		if (fatherElement!=null) {
			LOG.info("getElementsByCSSSelector method called");
			Reporter.log("getElementsByCSSSelector method called");
			System.out.println("getElementsByCSSSelector method called");
			try {
				elementList = fatherElement.findElements(By.cssSelector(css));
				if (elementList == null) {
					for (int i = 0; i < DELAY_NUM; i++) {
						TimeUtils.waitfor(DELAY_STEP_LENGTH);
						elementList = fatherElement.findElements(By.cssSelector(css));
						if (elementList != null) {
							break;
						}
					}
				}
			} catch (Exception e) {
				// TODO: handle exception
				LOG.info("Can not find elements:" + css);
				Reporter.log("Can not find element:" + css);
				System.out.println("Can not find element:" + css);
				elementList = null;
				// Assert.fail("Can not find element:"+css);
				return elementList;
			}
		}
		return elementList;
	}

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

	/**
	 * 通过页面元素存在不存在来断言,不存在元素,断言失败,用例停止执行 页面存在元素,不做任何操作
	 * 
	 * @param element
	 *            元素
	 * @param str
	 *            依据什么找到元素
	 */
	public void assertFailByElementNotExist(WebElement element, String str) {
		if (element == null) {
			Assert.fail("Can not find element:" + str);
		}
	}

	/**
	 * 通过页面元素存不存在来断言 不存在元素,查看是否是跳入了非安全页面 在containsLabel为true的情况下,断言成功
	 * 在containsLabel为false的情况下,断言失败
	 * 
	 * @param element
	 *            元素
	 * @param str
	 *            依据什么找到的元素
	 * @param curUrl
	 *            当前页面的url
	 */
	public void assertByElementNotExist(WebElement element, String str, String curUrl) {
		if (element == null) {
			if (curUrl != null && CloudTestBase.containsLabel) {
				Assert.assertTrue(enterUnsafePage(curUrl));
			} else {
				Assert.fail("Can not find element:" + str);
			}
		}
	}

	/**
	 * 是否进入了非安全页面
	 * 
	 * @param curUrl
	 * @return
	 */
	public boolean enterUnsafePage(String curUrl) {
		if (curUrl != null && curUrl.equals(CommonProperties.UNSAFE_CLOUD_URL)) {
			return true;
		}
		return false;
	}

	/**
	 * 鼠标移动到元素位置
	 * 
	 * @param element
	 */
	public void scrollTo(WebElement element) {
		JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
		javascriptExecutor.executeScript("arguments[0].scrollIntoView(true)", element);
	}

	/**
	 * driver跳转到到当前window
	 * 
	 * @param curDriver
	 */
	public WebDriver switchToCurrentWindow(WebDriver curDriver) {
		String currentWindow = curDriver.getWindowHandle();
		Set<String> winHandels = curDriver.getWindowHandles();
		List<String> it = new ArrayList<String>(winHandels);
		// 跳转到新打开的窗口
		for (String winHandle : curDriver.getWindowHandles()) {
			if (!winHandle.equals(currentWindow)) {
				curDriver = curDriver.switchTo().window(winHandle);
				break;
			}
		}
		return curDriver;
	}

	/**
	 * 判断是否出现了alert框或者确认对话框
	 * 
	 * @param curDriver
	 * @return
	 */
	public boolean isDispalyedAlert(WebDriver curDriver) {
		if (curDriver != null) {
			Alert confirm = curDriver.switchTo().alert();
			if (confirm != null) {
				return true;
			}
		}
		return false;
	}
	/**
	 * 获取Input标签的值
	 * @param driver
	 * @param element
	 * @return
	 */
	public String getInputValue(WebDriver driver, WebElement element){
		JavascriptExecutor javaScriptExecutor = (JavascriptExecutor) driver;
		try{
            String value = javaScriptExecutor.executeScript("return arguments[0].value", element).toString();
			return value;
		}catch (Exception e){
		    e.printStackTrace();
			return null;
		}

	}

	/**
	 * 鼠标移动到元素
	 * 
	 * @param element
	 */
	public void actionToElement(WebElement element) {
		// 创建鼠标属性方法
		Actions action = new Actions(driver);
		// 获取 moveToElement 方法 ,元素定位到想要移上去的元素上
		action.moveToElement(element).perform();
	}

	/**
	 * 窗口最大化
	 */
	public void maxWindow() {
		driver.manage().window().maximize();
	}
/**
	 * 滚动到目标元素的Y坐标位置
	 * 
	 * @param driver
	 * @param element
	 */
	public void scrollYLoaction(WebDriver driver, WebElement element) {
		int YLoc = element.getLocation().getY();
		String js = String.format("window.scroll(0,%s)", YLoc);
		((JavascriptExecutor) driver).executeScript(js, element);
	}
	/**
	 * 滚动到目标元素位置
	 * @param driver
	 * @param element
	 */
	public void scrollTargetLoaction(WebDriver driver, WebElement element) {
			((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element);
	}

 

有些其他封装的方法也一并粘出来了,

里边的Log貌似设的有点多,可再修改一下,有时间再改吧,目的是脚本一旦执行出错,能快速定位到问题所在。

如果有谁有更好的脚本强壮的建议,可直接提额,互相精进一下嘛~~

暂时先这些。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值