添加Elemenet中间类,便于事件类对元素重新进行获取
This commit is contained in:
parent
2aa8e8b310
commit
2987267baa
|
@ -581,8 +581,6 @@ public abstract class AbstractElement {
|
|||
default:
|
||||
throw new UnrecognizableLocationModeException("无法识别的定位类型:" + byType);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -7,6 +7,7 @@ import org.openqa.selenium.support.ui.WebDriverWait;
|
|||
|
||||
import pres.auxiliary.selenium.xml.ByType;
|
||||
import pres.auxiliary.work.selenium.brower.AbstractBrower;
|
||||
import pres.auxiliary.work.selenium.element.Element.ElementType;
|
||||
|
||||
/**
|
||||
* <p><b>文件名:</b>CommonElement.java</p>
|
||||
|
@ -17,7 +18,7 @@ import pres.auxiliary.work.selenium.brower.AbstractBrower;
|
|||
* </p>
|
||||
* <p><b>编码时间:</b>2020年4月26日下午10:34:55</p>
|
||||
* <p><b>修改时间:</b>2020年4月26日下午10:34:55</p>
|
||||
* @author
|
||||
* @author 彭宇琦
|
||||
* @version Ver1.0
|
||||
* @since JDK 12
|
||||
*
|
||||
|
@ -41,22 +42,22 @@ public class CommonElement extends AbstractElement {
|
|||
}
|
||||
|
||||
/**
|
||||
* 用于根据xml文件中元素的名称,返回对应的WebElement对象。该方法亦可传入元素
|
||||
* 用于根据xml文件中元素的名称,返回对应的{@link Element}对象。该方法亦可传入元素
|
||||
* 定位内容,通过遍历所有的定位方式,在页面上查找元素,来获取元素的WebElement对象。
|
||||
* 建议传入的定位内容为xpath路径或绝对的css路径,若非这两路径,则在识别元素时会很慢,降低
|
||||
* 程序运行速度。若非xml文件中的元素,且不是xpath路径或绝对的css路径,建议使用{@link #getWebElement(String, ByType)}方法
|
||||
* @param name 元素的名称或元素定位内容
|
||||
* @return WebElement对象
|
||||
* @return {@link Element}对象
|
||||
*/
|
||||
public Element getWebElement(String name) {
|
||||
public Element getElement(String name) {
|
||||
return getElement(name, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于根据xml文件中元素的名称,与定位方式,返回对应的WebElement对象。该方法亦可传入元素
|
||||
* 用于根据xml文件中元素的名称,与定位方式,返回对应的{@link Element}对象。该方法亦可传入元素
|
||||
* 定位内容,并根据定位方式,对页面数据进行查找
|
||||
* @param name 元素的名称或元素定位内容
|
||||
* @return WebElement对象
|
||||
* @return {@link Element}对象
|
||||
*/
|
||||
public Element getElement(String name, ByType byType) {
|
||||
return getElement(new ElementInformation(name, byType));
|
||||
|
@ -83,16 +84,14 @@ public class CommonElement extends AbstractElement {
|
|||
* @param elementInformation 元素信息类对象
|
||||
* @return WebElement对象
|
||||
*/
|
||||
/*
|
||||
private Element getElement(ElementInformation elementInformation) {
|
||||
//判断传入的元素是否在xml文件中,若存在再判断是否自动切换窗体,若需要,则获取元素的所有父窗体并进行切换
|
||||
if (xml != null && xml.isElement(elementInformation.name) && isAutoSwitchIframe) {
|
||||
switchFrame(getParentFrameName(elementInformation.name));
|
||||
}
|
||||
|
||||
return new Element(recognitionElement(elementInformation));
|
||||
return new Element(driver, ElementType.COMMON_ELEMENT, recognitionElement(elementInformation));
|
||||
}
|
||||
*/
|
||||
|
||||
@Override
|
||||
boolean isExistElement(By by, long waitTime) {
|
||||
|
|
|
@ -15,6 +15,7 @@ import org.openqa.selenium.support.ui.WebDriverWait;
|
|||
|
||||
import pres.auxiliary.selenium.xml.ByType;
|
||||
import pres.auxiliary.work.selenium.brower.AbstractBrower;
|
||||
import pres.auxiliary.work.selenium.element.Element.ElementType;
|
||||
|
||||
/**
|
||||
* <p><b>文件名:</b>DataListElement.java</p>
|
||||
|
@ -33,7 +34,7 @@ public class DataListElement extends AbstractElement {
|
|||
/**
|
||||
* 用于存储获取到的列表一列元素,key为列表名称,value为列表元素
|
||||
*/
|
||||
private LinkedHashMap<ElementInformation, List<WebElement>> elementMap = new LinkedHashMap<>(16);
|
||||
private LinkedHashMap<ElementInformation, List<Element>> elementMap = new LinkedHashMap<>(16);
|
||||
|
||||
/**
|
||||
* 用于存储元素列累计的个数
|
||||
|
@ -76,9 +77,9 @@ public class DataListElement extends AbstractElement {
|
|||
}
|
||||
|
||||
/**
|
||||
* 构造对象并存储浏览器的WebDriver对象
|
||||
* 构造对象并存储浏览器的{@link WebDriver}对象
|
||||
*
|
||||
* @param driver 浏览器的WebDriver对象
|
||||
* @param driver 浏览器的{@link WebDriver}对象
|
||||
*/
|
||||
public DataListElement(WebDriver driver) {
|
||||
super(driver);
|
||||
|
@ -192,9 +193,15 @@ public class DataListElement extends AbstractElement {
|
|||
* @param isAddSize 是否需要统计
|
||||
*/
|
||||
private void add(ElementInformation elementInformation) {
|
||||
List<WebElement> elementList = new ArrayList<WebElement>();
|
||||
List<Element> elementList = new ArrayList<Element>();
|
||||
//获取元素
|
||||
elementList = driver.findElements(recognitionElement(elementInformation));
|
||||
By by = recognitionElement(elementInformation);
|
||||
int size = driver.findElements(by).size();
|
||||
//构造Element对象
|
||||
for (int i = 0; i < size; i++) {
|
||||
elementList.add(new Element(driver, ElementType.DATA_LIST_ELEMENT, by, i));
|
||||
}
|
||||
//elementList = driver.findElements(recognitionElement(elementInformation));
|
||||
//添加元素
|
||||
elementMap.put(elementInformation, elementList);
|
||||
|
||||
|
@ -226,7 +233,7 @@ public class DataListElement extends AbstractElement {
|
|||
* @param isRemove 是否需要将该列移除
|
||||
* @return 被移除列中存储的所有元素
|
||||
*/
|
||||
public List<WebElement> clearColumn(String name, boolean isRemove) {
|
||||
public List<Element> clearColumn(String name, boolean isRemove) {
|
||||
ElementInformation element = nameToElementInformation(name);
|
||||
//若元素不存在,则直接返回null
|
||||
if (element == null) {
|
||||
|
@ -234,7 +241,7 @@ public class DataListElement extends AbstractElement {
|
|||
}
|
||||
|
||||
//用于存储被移除的元素
|
||||
List<WebElement> elementList = elementMap.get(element);
|
||||
List<Element> elementList = elementMap.get(element);
|
||||
//判断元素是否需要被完全移除
|
||||
if (isRemove) {
|
||||
//若元素需要被完全移除,则直接移除元素
|
||||
|
@ -277,7 +284,7 @@ public class DataListElement extends AbstractElement {
|
|||
* @return 对应列指定的元素
|
||||
* @throws NoSuchElementException 当未对name列进行获取数据或index的绝对值大于列表最大值时抛出的异常
|
||||
*/
|
||||
public WebElement getWebElement(String name, int index) {
|
||||
public Element getWebElement(String name, int index) {
|
||||
//获取元素信息,并判断元素是否存在,不存在则抛出异常
|
||||
ElementInformation element = nameToElementInformation(name);
|
||||
if (element == null) {
|
||||
|
@ -298,7 +305,7 @@ public class DataListElement extends AbstractElement {
|
|||
* @return 对应列元素
|
||||
* @throws NoSuchElementException 当未对name列进行获取数据时抛出的异常
|
||||
*/
|
||||
public List<WebElement> getAllWebElement(String name) {
|
||||
public List<Element> getAllWebElement(String name) {
|
||||
//获取元素信息,并判断元素是否存在,不存在则抛出异常
|
||||
ElementInformation element = nameToElementInformation(name);
|
||||
if (element == null) {
|
||||
|
@ -317,9 +324,9 @@ public class DataListElement extends AbstractElement {
|
|||
* @throws NoSuchElementException 当未对name列进行获取数据或index的绝对值大于列表最大值时抛出的异常
|
||||
* @see #getWebElement(String, int)
|
||||
*/
|
||||
public List<WebElement> getWebElements(String name, int... indexs) {
|
||||
public List<Element> getWebElements(String name, int... indexs) {
|
||||
// 存储所有获取到的事件
|
||||
ArrayList<WebElement> events = new ArrayList<>();
|
||||
ArrayList<Element> events = new ArrayList<>();
|
||||
|
||||
// 循环,解析所有的下标,并调用getEvent()方法,存储至events
|
||||
for (int index : indexs) {
|
||||
|
@ -336,7 +343,7 @@ public class DataListElement extends AbstractElement {
|
|||
* @param length 需要返回列表事件的个数
|
||||
* @return 列表事件组
|
||||
*/
|
||||
public List<WebElement> getRandomWebElements(String name, int length) {
|
||||
public List<Element> getRandomWebElements(String name, int length) {
|
||||
//获取元素信息,并判断元素是否存在,不存在则抛出异常
|
||||
ElementInformation element = nameToElementInformation(name);
|
||||
if (element == null) {
|
||||
|
|
|
@ -1,36 +1,125 @@
|
|||
package pres.auxiliary.work.selenium.element;
|
||||
|
||||
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.Select;
|
||||
|
||||
/**
|
||||
* <p><b>文件名:</b>Element.java</p>
|
||||
* <p><b>用途:</b>
|
||||
* 用于返回和查找页面元素,以方便在元素过期时能进行重新获取
|
||||
* </p>
|
||||
* <p><b>编码时间:</b>2020年5月18日上午8:39:05</p>
|
||||
* <p><b>修改时间:</b>2020年5月18日上午8:39:05</p>
|
||||
* @author 彭宇琦
|
||||
* @version Ver1.0
|
||||
* @since JDK 12
|
||||
*
|
||||
*/
|
||||
public class Element {
|
||||
/**
|
||||
* 存储元素的名称或定位内容
|
||||
* 存储元素
|
||||
*/
|
||||
private WebElement element = null;
|
||||
/**
|
||||
* 存储查找元素的By对象
|
||||
*/
|
||||
private List<WebElement> elementList = null;
|
||||
|
||||
private By by;
|
||||
/**
|
||||
* 存储获取需要获取的元素下标
|
||||
*/
|
||||
private int index;
|
||||
|
||||
/**
|
||||
* 初始化信息
|
||||
* 存储WebDriver对象,以查找相应元素
|
||||
*/
|
||||
private WebDriver driver;
|
||||
|
||||
/**
|
||||
* 标记元素的类型,以用于重新获取时
|
||||
*/
|
||||
private ElementType elementType;
|
||||
|
||||
/**
|
||||
* 初始化信息,并添加需要获取元素的下标,用于需要获取列表时使用
|
||||
* @param name 元素名称或定位内容
|
||||
* @param byType 元素定位
|
||||
*/
|
||||
public Element(By by) {
|
||||
public Element(WebDriver driver, ElementType elementType, By by, int index) {
|
||||
super();
|
||||
this.by = by;
|
||||
this.index = index;
|
||||
this.driver = driver;
|
||||
this.elementType = elementType;
|
||||
|
||||
//对元素进行一次查找
|
||||
findElement();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化信息,指定获取第一个元素,用于只获取单个元素时使用
|
||||
* @param name 元素名称或定位内容
|
||||
* @param byType 元素定位
|
||||
*/
|
||||
public Element(WebDriver driver, ElementType elementType, By by) {
|
||||
this(driver, elementType, by, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于返回元素对应的WebElement对象
|
||||
* @return 返回元素对应的WebElement对象
|
||||
*/
|
||||
public WebElement getWebElement(WebDriver driver) {
|
||||
return driver.findElement(by);
|
||||
public WebElement getWebElement() {
|
||||
return element;
|
||||
}
|
||||
|
||||
public void
|
||||
/**
|
||||
* 根据存储的WebDriver对象对元素进行更新
|
||||
*/
|
||||
public void findElement() {
|
||||
switch (elementType) {
|
||||
case COMMON_ELEMENT:
|
||||
case DATA_LIST_ELEMENT:
|
||||
case SELECT_DATAS_ELEMENT:
|
||||
element = driver.findElements(by).get(index);
|
||||
break;
|
||||
case SELECT_OPTION_ELEMENT:
|
||||
new Select(element).getOptions().get(index);
|
||||
default:
|
||||
throw new IllegalArgumentException("Unexpected value: " + elementType);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <p><b>文件名:</b>Element.java</p>
|
||||
* <p><b>用途:</b>
|
||||
* 用于标记当前传入的元素是以何种方式进行获取
|
||||
* </p>
|
||||
* <p><b>编码时间:</b>2020年5月20日上午7:43:38</p>
|
||||
* <p><b>修改时间:</b>2020年5月20日上午7:43:38</p>
|
||||
* @author
|
||||
* @version Ver1.0
|
||||
* @since JDK 12
|
||||
*
|
||||
*/
|
||||
public enum ElementType {
|
||||
/**
|
||||
* 指向普通类型元素
|
||||
*/
|
||||
COMMON_ELEMENT,
|
||||
/**
|
||||
* 指向数据列表类型元素
|
||||
*/
|
||||
DATA_LIST_ELEMENT,
|
||||
/**
|
||||
* 指向标准下拉框选择类型元素
|
||||
*/
|
||||
SELECT_OPTION_ELEMENT,
|
||||
/**
|
||||
* 指向列表型下拉框选择类型元素
|
||||
*/
|
||||
SELECT_DATAS_ELEMENT;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
package pres.auxiliary.work.selenium.event;
|
||||
|
||||
import org.openqa.selenium.ElementClickInterceptedException;
|
||||
import org.openqa.selenium.StaleElementReferenceException;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.interactions.Actions;
|
||||
import org.openqa.selenium.support.ui.ExpectedConditions;
|
||||
|
||||
import pres.auxiliary.work.selenium.element.Element;
|
||||
|
||||
/**
|
||||
* <p><b>文件名:</b>ClickEvent.java</p>
|
||||
* <p><b>用途:</b>
|
||||
|
@ -34,19 +36,20 @@ public class ClickEvent extends AbstractEvent {
|
|||
*
|
||||
* @param element 通过查找页面得到的控件元素对象
|
||||
*/
|
||||
public void click(WebElement element) {
|
||||
//元素高亮
|
||||
elementHight(element);
|
||||
public void click(Element element) {
|
||||
//在等待时间内判断元素是否可以点击
|
||||
wait.until(ExpectedConditions.elementToBeClickable(element));
|
||||
wait.until(ExpectedConditions.elementToBeClickable(element.getWebElement()));
|
||||
//进行操作,若仍抛出ElementClickInterceptedException异常,则再等待,直到不再抛出异常
|
||||
wait.until((driver) -> {
|
||||
try {
|
||||
element.click();
|
||||
element.getWebElement().click();
|
||||
return true;
|
||||
} catch (ElementClickInterceptedException e) {
|
||||
return false;
|
||||
} catch (StaleElementReferenceException e) {
|
||||
element.findElement();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
step = "鼠标左键点击“" + ELEMENT_NAME + "”元素";
|
||||
|
@ -57,18 +60,19 @@ public class ClickEvent extends AbstractEvent {
|
|||
*
|
||||
* @param element 通过查找页面得到的控件元素对象
|
||||
*/
|
||||
public void doubleClick(WebElement element) {
|
||||
//元素高亮
|
||||
elementHight(element);
|
||||
public void doubleClick(Element element) {
|
||||
//在等待时间内判断元素是否可以点击,若可以点击元素,则进行双击事件
|
||||
wait.until(ExpectedConditions.elementToBeClickable(element));
|
||||
wait.until(ExpectedConditions.elementToBeClickable(element.getWebElement()));
|
||||
//进行操作,若仍抛出ElementClickInterceptedException异常,则再等待,直到不再抛出异常
|
||||
wait.until((driver) -> {
|
||||
try {
|
||||
new Actions(driver).doubleClick(element).perform();
|
||||
new Actions(driver).doubleClick(element.getWebElement()).perform();
|
||||
return true;
|
||||
} catch (ElementClickInterceptedException e) {
|
||||
return false;
|
||||
} catch (StaleElementReferenceException e) {
|
||||
element.findElement();
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -79,19 +83,20 @@ public class ClickEvent extends AbstractEvent {
|
|||
* 鼠标右键点击事件
|
||||
* @param element 通过查找页面得到的控件元素对象
|
||||
*/
|
||||
public void rightClick(WebElement element) {
|
||||
//元素高亮
|
||||
elementHight(element);
|
||||
public void rightClick(Element element) {
|
||||
//在等待时间内判断元素是否可以点击,若可以点击元素,则进行右击事件
|
||||
wait.until(ExpectedConditions.elementToBeClickable(element));
|
||||
wait.until(ExpectedConditions.elementToBeClickable(element.getWebElement()));
|
||||
//进行操作,若仍抛出ElementClickInterceptedException异常,则再等待,直到不再抛出异常
|
||||
wait.until((driver) -> {
|
||||
try {
|
||||
new Actions(driver).contextClick(element).perform();
|
||||
new Actions(driver).contextClick(element.getWebElement()).perform();
|
||||
return true;
|
||||
} catch (ElementClickInterceptedException e) {
|
||||
return false;
|
||||
} catch (StaleElementReferenceException e) {
|
||||
element.findElement();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
step = "鼠标右键点击“" + ELEMENT_NAME + "”元素";
|
||||
|
@ -103,7 +108,7 @@ public class ClickEvent extends AbstractEvent {
|
|||
* @param clickCount 点击次数
|
||||
* @param sleepInMillis 操作时间间隔,单位为毫秒
|
||||
*/
|
||||
public void continuousClick(WebElement element, int clickCount, long sleepInMillis) {
|
||||
public void continuousClick(Element element, int clickCount, long sleepInMillis) {
|
||||
for(int i = 0; i < clickCount; i++) {
|
||||
click(element);
|
||||
|
||||
|
|
|
@ -6,17 +6,30 @@ import java.util.UUID;
|
|||
|
||||
import org.openqa.selenium.By;
|
||||
import org.openqa.selenium.JavascriptExecutor;
|
||||
import org.openqa.selenium.StaleElementReferenceException;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WebElement;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
import pres.auxiliary.work.selenium.element.Element;
|
||||
import pres.auxiliary.work.selenium.element.Element.ElementType;
|
||||
|
||||
/**
|
||||
* <p><b>文件名:</b>JsEvent.java</p>
|
||||
* <p><b>用途:</b>用提供通过Javascript来修改或者获取页面元素内容的方法</p>
|
||||
* <p><b>编码时间:</b>2018年12月2日 下午12:51:19</p>
|
||||
* <p><b>修改时间:</b>2020年5月17日 下午5:21:44</p>
|
||||
* <p>
|
||||
* <b>文件名:</b>JsEvent.java
|
||||
* </p>
|
||||
* <p>
|
||||
* <b>用途:</b>用提供通过Javascript来修改或者获取页面元素内容的方法
|
||||
* </p>
|
||||
* <p>
|
||||
* <b>编码时间:</b>2018年12月2日 下午12:51:19
|
||||
* </p>
|
||||
* <p>
|
||||
* <b>修改时间:</b>2020年5月17日 下午5:21:44
|
||||
* </p>
|
||||
*
|
||||
* @author 彭宇琦
|
||||
* @version Ver1.0
|
||||
* @since JDK 12
|
||||
|
@ -44,10 +57,17 @@ public class JsEvent extends AbstractEvent {
|
|||
* @param attributeName 属性名
|
||||
* @return 元素对应属性的内容
|
||||
*/
|
||||
public String getAttribute(WebElement element, String attributeName) {
|
||||
public String getAttribute(Element element, String attributeName) {
|
||||
// 获取对应元素的内容
|
||||
String text = (String) (js.executeScript("return arguments[0].getAttribute('" + attributeName + "');",
|
||||
element));
|
||||
wait.until(driver -> {
|
||||
try {
|
||||
return element.getWebElement();
|
||||
} catch (StaleElementReferenceException e) {
|
||||
element.findElement();
|
||||
return null;
|
||||
}
|
||||
})));
|
||||
// 返回对应属性的内容,若传入的属性不存在,则返回空串
|
||||
return (text == null) ? "" : text;
|
||||
}
|
||||
|
@ -60,12 +80,19 @@ public class JsEvent extends AbstractEvent {
|
|||
* @param value 需要设置的属性值
|
||||
* @return 属性的原值
|
||||
*/
|
||||
public String putAttribute(WebElement element, String attributeName, String value) {
|
||||
public String putAttribute(Element element, String attributeName, String value) {
|
||||
// 获取原属性中的值
|
||||
String oldValue = getAttribute(element, attributeName);
|
||||
|
||||
// 执行代码
|
||||
js.executeScript("arguments[0].setAttribute('" + attributeName + "','" + value + "');", element);
|
||||
js.executeScript("arguments[0].setAttribute('" + attributeName + "','" + value + "');", wait.until(driver -> {
|
||||
try {
|
||||
return element.getWebElement();
|
||||
} catch (StaleElementReferenceException e) {
|
||||
element.findElement();
|
||||
return null;
|
||||
}
|
||||
}));
|
||||
return oldValue;
|
||||
}
|
||||
|
||||
|
@ -77,7 +104,7 @@ public class JsEvent extends AbstractEvent {
|
|||
* @param elementName 新元素(标签)的名称
|
||||
* @return 新增元素的定位方式
|
||||
*/
|
||||
public String addElement(WebElement element, String elementName) {
|
||||
public String addElement(Element element, String elementName) {
|
||||
// 获取并将其作为
|
||||
String script = "var oldElement = arguments[0];";
|
||||
// 拼接添加元素的代码
|
||||
|
@ -89,34 +116,40 @@ public class JsEvent extends AbstractEvent {
|
|||
script += "oldElement.appendChild(newElement);";
|
||||
|
||||
// 执行代码
|
||||
js.executeScript(script, element);
|
||||
js.executeScript(script, wait.until(driver -> {
|
||||
try {
|
||||
return element.getWebElement();
|
||||
} catch (StaleElementReferenceException e) {
|
||||
element.findElement();
|
||||
return null;
|
||||
}
|
||||
}));
|
||||
|
||||
return "//" + elementName + "[@temp_attribute='" + uuid + "']";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据json中的属性及元素名称信息,在指定元素下添加一个元素,元素将带一个
|
||||
* 名为temp_attribute的属性,其属性的值为一个uuid,并且添加成功后
|
||||
* 方法返回该元素定位xpath,格式为“//标签名[@temp_attribute='uuid']”。
|
||||
* 传入的json格式可以参照{@link #deleteElement(WebElement)}方法中
|
||||
* 返回的json形式
|
||||
* 传入的json格式可以参照{@link #deleteElement(WebElement)}方法中 返回的json形式
|
||||
*
|
||||
* @param element 元素
|
||||
* @param elementJson 新元素(标签)的信息
|
||||
* @return 新增元素的定位方式
|
||||
*/
|
||||
public String addElement(WebElement element, JSONObject elementJson) {
|
||||
//添加元素
|
||||
public String addElement(Element element, JSONObject elementJson) {
|
||||
// 添加元素
|
||||
String elementName = elementJson.getString("tagname");
|
||||
|
||||
//获取新添加元素的xpath
|
||||
|
||||
// 获取新添加元素的xpath
|
||||
String xpath = addElement(element, elementName);
|
||||
//查找新添加的元素(由于是新添加的元素,肯定能查找到,故无需编写等待)
|
||||
WebElement newElement = driver.findElement(By.xpath(xpath));
|
||||
|
||||
//获取元素的所有属性
|
||||
// 查找新添加的元素(由于是新添加的元素,肯定能查找到,故无需编写等待)
|
||||
Element newElement = new Element(driver, ElementType.COMMON_ELEMENT, By.xpath(xpath));
|
||||
|
||||
// 获取元素的所有属性
|
||||
JSONArray attributes = elementJson.getJSONArray("attributes");
|
||||
//遍历属性信息,向元素中添加属性
|
||||
// 遍历属性信息,向元素中添加属性
|
||||
for (int i = 0; i < attributes.size(); i++) {
|
||||
JSONObject attJson = attributes.getJSONObject(i);
|
||||
putAttribute(newElement, attJson.getString("name"), attJson.getString("value"));
|
||||
|
@ -127,6 +160,7 @@ public class JsEvent extends AbstractEvent {
|
|||
|
||||
/**
|
||||
* 删除指定的元素,并以json的格式返回被删除的元素信息,其形式为:
|
||||
*
|
||||
* <pre>
|
||||
* {
|
||||
* "tagname":元素标签名称
|
||||
|
@ -147,9 +181,17 @@ public class JsEvent extends AbstractEvent {
|
|||
* @param element 元素
|
||||
* @return 元素的信息
|
||||
*/
|
||||
public JSONObject deleteElement(WebElement element) {
|
||||
JSONObject json = getElementInfromation(element);
|
||||
|
||||
public JSONObject deleteElement(Element element) {
|
||||
//获取元素信息
|
||||
JSONObject json = getElementInfromation(wait.until(driver -> {
|
||||
try {
|
||||
return element.getWebElement();
|
||||
} catch (StaleElementReferenceException e) {
|
||||
element.findElement();
|
||||
return null;
|
||||
}
|
||||
}));
|
||||
|
||||
// 获取节点
|
||||
String script = "var deleteNode = arguments[0];";
|
||||
// 获取节点的父节点
|
||||
|
@ -157,8 +199,8 @@ public class JsEvent extends AbstractEvent {
|
|||
// 通过父节点来删除子节点
|
||||
script += "parentNode.removeChild(deleteNode)";
|
||||
|
||||
// 执行代码
|
||||
js.executeScript(script, element);
|
||||
// 执行代码,由于在获取元素信息时已经对元素的过期进行了判断,故此处无需在做判断
|
||||
js.executeScript(script, element.getWebElement());
|
||||
|
||||
return json;
|
||||
}
|
||||
|
@ -173,12 +215,12 @@ public class JsEvent extends AbstractEvent {
|
|||
// 执行代码
|
||||
return js.executeScript(script);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 用于执行已经写好的js脚本
|
||||
*
|
||||
* @param script js脚本
|
||||
* @param args 传入的参数
|
||||
* @param args 传入的参数
|
||||
* @return 执行结果
|
||||
*/
|
||||
public Object runScript(String script, Object... args) {
|
||||
|
@ -188,6 +230,7 @@ public class JsEvent extends AbstractEvent {
|
|||
|
||||
/**
|
||||
* 获取元素所有的属性信息,以json的形式返回。格式为:
|
||||
*
|
||||
* <pre>
|
||||
* {
|
||||
* "tagname":元素标签名称
|
||||
|
@ -204,48 +247,49 @@ public class JsEvent extends AbstractEvent {
|
|||
* ]
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* @param element 元素
|
||||
* @return 元素属性的信息,以json的形式返回
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private JSONObject getElementInfromation(WebElement element) {
|
||||
JSONObject elementJson = new JSONObject();
|
||||
|
||||
//添加元素的标签名称
|
||||
|
||||
// 添加元素的标签名称
|
||||
elementJson.put("tagname", element.getTagName());
|
||||
|
||||
|
||||
JSONArray attArray = new JSONArray();
|
||||
//获取元素的所有属性信息
|
||||
// 获取元素的所有属性信息
|
||||
((ArrayList<Object>) js.executeScript("return arguments[0].attributes;", element)).stream().
|
||||
//将属性信息变为字符串的形式
|
||||
map(obj -> obj.toString()).
|
||||
//删除无用的属性信息,并将属性信息存储至一个json中进行返回
|
||||
map(text -> {
|
||||
//将属性按照“, ”进行切分,得到每一个信息
|
||||
String[] atts = text.split("\\,\\ ");
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
//过滤属性信息
|
||||
Arrays.stream(atts).filter(att -> {
|
||||
String key = att.split("=")[0];
|
||||
return "name".equals(key) || "value".equals(key);
|
||||
// 将属性信息变为字符串的形式
|
||||
map(obj -> obj.toString()).
|
||||
// 删除无用的属性信息,并将属性信息存储至一个json中进行返回
|
||||
map(text -> {
|
||||
// 将属性按照“, ”进行切分,得到每一个信息
|
||||
String[] atts = text.split("\\,\\ ");
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
// 过滤属性信息
|
||||
Arrays.stream(atts).filter(att -> {
|
||||
String key = att.split("=")[0];
|
||||
return "name".equals(key) || "value".equals(key);
|
||||
}).
|
||||
// 将过滤后的信息存储至json中(最后只剩下name和value属性)
|
||||
forEach(att -> {
|
||||
String[] kv = att.split("=");
|
||||
json.put(kv[0], kv[1].indexOf("}") > -1 ? kv[1].substring(0, kv[1].length() - 1) : kv[1]);
|
||||
});
|
||||
|
||||
return json;
|
||||
}).
|
||||
//将过滤后的信息存储至json中(最后只剩下name和value属性)
|
||||
forEach(att -> {
|
||||
String[] kv = att.split("=");
|
||||
json.put(kv[0], kv[1].indexOf("}") > -1 ? kv[1].substring(0, kv[1].length() - 1) : kv[1]);
|
||||
// 将每一个json存储至attArray中
|
||||
forEach(json -> {
|
||||
attArray.add(json);
|
||||
});
|
||||
|
||||
return json;
|
||||
}).
|
||||
//将每一个json存储至attArray中
|
||||
forEach(json -> {
|
||||
attArray.add(json);
|
||||
});
|
||||
|
||||
//存储属性
|
||||
|
||||
// 存储属性
|
||||
elementJson.put("attributes", attArray);
|
||||
|
||||
|
||||
return elementJson;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,24 +38,6 @@ public class SelectEvent extends AbstractEvent {
|
|||
return select(element, -1);
|
||||
}
|
||||
|
||||
/*
|
||||
public String selectLast(List<WebElement> elements) {
|
||||
int index = elements.size() - 1;
|
||||
WebElement element = elements.get(index);
|
||||
//元素高亮
|
||||
elementHight(element);
|
||||
//在等待时间内判断元素是否可以点击,若可以点击元素,则进行点击事件
|
||||
new WebDriverWait(driver, waitTime, 200).until(ExpectedConditions.elementToBeClickable(element)).click();
|
||||
|
||||
//记录操作内容
|
||||
result = element.getText();
|
||||
step = "选择“" + ELEMENT_NAME + "”元素的第" + index + "个选项";
|
||||
|
||||
//返回Event类
|
||||
return result;
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* 选择下拉框中的指定下标的元素,下标支持从后向前获取,传入的下标
|
||||
* 与元素实际所在位置一致,当传入0时,则表示随机获取一个元素,如:<br>
|
||||
|
|
|
@ -48,25 +48,20 @@ public class TextEvent extends AbstractEvent {
|
|||
* @return 被清空的文本内容
|
||||
*/
|
||||
public String clear(Element element) {
|
||||
WebElement webElement = element.getWebElement(driver);
|
||||
//元素高亮
|
||||
elementHight(webElement);
|
||||
|
||||
//等待事件可操作后对事件进行操作
|
||||
//由于需要存储步骤,若直接调用getText方法进行返回时,其会更改存储的step,为保证step正确,故存储返回值进行返回
|
||||
String text = getText(element);
|
||||
|
||||
//对元素进行操作,若元素过期,则重新获取
|
||||
wait.until(driver -> {
|
||||
try {
|
||||
webElement.clear();
|
||||
element.getWebElement().clear();
|
||||
return true;
|
||||
} catch (StaleElementReferenceException e) {
|
||||
// TODO: handle exception
|
||||
} catch (Exception e) {
|
||||
element.findElement();
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
//由于需要存储步骤,若直接调用getText方法进行返回时,其会更改存储的step,为保证step正确,故存储返回值进行返回
|
||||
String text = getText(webElement);
|
||||
|
||||
//记录操作
|
||||
step = "清空“" + ELEMENT_NAME + "”元素内的文本";
|
||||
|
||||
|
@ -79,16 +74,23 @@ public class TextEvent extends AbstractEvent {
|
|||
* @param attributeName 属性名称
|
||||
* @return 对应属性的值
|
||||
*/
|
||||
public String getAttributeValue(WebElement element, String attributeName) {
|
||||
//元素高亮
|
||||
elementHight(element);
|
||||
public String getAttributeValue(Element element, String attributeName) {
|
||||
//等待元素中attributeName指向的属性内容出现
|
||||
wait.until(ExpectedConditions.attributeToBeNotEmpty(element, attributeName));
|
||||
wait.until(ExpectedConditions.attributeToBeNotEmpty(element.getWebElement(), attributeName));
|
||||
|
||||
|
||||
//记录操作
|
||||
step = "获取“" + ELEMENT_NAME + "”元素的" + attributeName +"属性的内容";
|
||||
|
||||
return element.getAttribute(attributeName);
|
||||
//对元素进行操作,若元素过期,则重新获取
|
||||
return wait.until(driver -> {
|
||||
try {
|
||||
return element.getWebElement().getAttribute(attributeName);
|
||||
} catch (StaleElementReferenceException e) {
|
||||
element.findElement();
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -96,23 +98,20 @@ public class TextEvent extends AbstractEvent {
|
|||
* @param element 通过查找页面得到的控件元素对象
|
||||
* @return 对应元素中的文本内容
|
||||
*/
|
||||
public String getText(WebElement element) {
|
||||
//元素高亮
|
||||
elementHight(element);
|
||||
|
||||
//等待事件可操作后对事件进行操作
|
||||
String text = wait.until(driver -> {
|
||||
try {
|
||||
return "input".equalsIgnoreCase(element.getTagName()) ? element.getAttribute("value") : element.getText();
|
||||
} catch (Exception e) {
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
public String getText(Element element) {
|
||||
//记录操作
|
||||
step = "获取“" + ELEMENT_NAME + "”元素内的文本";
|
||||
|
||||
return text;
|
||||
//对元素进行操作,若元素过期,则重新获取
|
||||
return wait.until(driver -> {
|
||||
try {
|
||||
WebElement webElement = element.getWebElement();
|
||||
return "input".equalsIgnoreCase(webElement.getTagName()) ? webElement.getAttribute("value") : webElement.getText();
|
||||
} catch (StaleElementReferenceException e) {
|
||||
element.findElement();
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -121,14 +120,11 @@ public class TextEvent extends AbstractEvent {
|
|||
* @param text 需要输入到控件中的
|
||||
* @return 在控件中输入的内容
|
||||
*/
|
||||
public String input(WebElement element, String text) {
|
||||
//元素高亮
|
||||
elementHight(element);
|
||||
|
||||
public String input(Element element, String text) {
|
||||
//等待事件可操作后对事件进行操作
|
||||
wait.until(driver -> {
|
||||
try {
|
||||
element.sendKeys(text);
|
||||
element.getWebElement().sendKeys(text);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
|
@ -150,9 +146,9 @@ public class TextEvent extends AbstractEvent {
|
|||
* @param codeImageElement 通过查找页面得到的验证码图片控件元素对象
|
||||
* @return 输入的内容
|
||||
*/
|
||||
public String codeInput(WebElement textElement, WebElement codeImageElement) {
|
||||
public String codeInput(Element textElement, Element codeImageElement) {
|
||||
// 判断验证码信息是否加载,加载后,获取其Rectang对象
|
||||
Rectangle r = codeImageElement.getRect();
|
||||
Rectangle r = codeImageElement.getWebElement().getRect();
|
||||
// 构造截图对象,并创建截图
|
||||
Screenshot sc = new Screenshot(driver, "Temp");
|
||||
File image = null;
|
||||
|
@ -184,7 +180,7 @@ public class TextEvent extends AbstractEvent {
|
|||
* @param textElements 通过查找页面得到的一组控件元素对象
|
||||
* @return 由于涉及到多个文本框,故其返回值有多个,将以“值1,值2,值3...”的形式进行返回
|
||||
*/
|
||||
public String avgIntergeInput(int num, WebElement... elements) {
|
||||
public String avgIntergeInput(int num, Element... elements) {
|
||||
//定义存储控件数量及需要随机的数量
|
||||
int contrlNum = elements.length;
|
||||
String inputNumText = "";
|
||||
|
@ -230,7 +226,7 @@ public class TextEvent extends AbstractEvent {
|
|||
* @param modes {@link StringMode}枚举,指定字符串输入的类型,可传入多种模型,参见{@link RandomString#RandomString(StringMode...)}
|
||||
* @return 在控件中输入的内容
|
||||
*/
|
||||
public String randomInput(WebElement element, int minLength, int maxLength, StringMode... modes) {
|
||||
public String randomInput(Element element, int minLength, int maxLength, StringMode... modes) {
|
||||
return randomInput(element, minLength, maxLength, new RandomString(modes));
|
||||
}
|
||||
|
||||
|
@ -242,7 +238,7 @@ public class TextEvent extends AbstractEvent {
|
|||
* @param mode 可用的随机字符串抽取范围,参见{@link RandomString#RandomString(String)}
|
||||
* @return 在控件中输入的内容
|
||||
*/
|
||||
public String randomInput(WebElement element, int minLength, int maxLength, String mode) {
|
||||
public String randomInput(Element element, int minLength, int maxLength, String mode) {
|
||||
return randomInput(element, minLength, maxLength, new RandomString(mode));
|
||||
}
|
||||
|
||||
|
@ -252,7 +248,7 @@ public class TextEvent extends AbstractEvent {
|
|||
* @param updataFile 需要上传到控件中的文件
|
||||
* @return 上传的文件路径
|
||||
*/
|
||||
public String updataFile(WebElement element, File updataFile) {
|
||||
public String updataFile(Element element, File updataFile) {
|
||||
//记录操作
|
||||
step = "向“" + ELEMENT_NAME + "”元素中的上传文件";
|
||||
return input(element, updataFile.getAbsolutePath());
|
||||
|
@ -266,7 +262,7 @@ public class TextEvent extends AbstractEvent {
|
|||
* @param rs 随机字符类对象
|
||||
* @return 在控件中输入的内容
|
||||
*/
|
||||
private String randomInput(WebElement element, int minLength, int maxLength, RandomString rs) {
|
||||
private String randomInput(Element element, int minLength, int maxLength, RandomString rs) {
|
||||
// 判断传入的参数是否小于0,小于0则将其都设置为1
|
||||
if (minLength < 0 || maxLength < 0) {
|
||||
minLength = 1;
|
||||
|
|
|
@ -4,6 +4,7 @@ import java.io.File;
|
|||
|
||||
import org.testng.annotations.AfterClass;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import pres.auxiliary.work.selenium.brower.ChromeBrower;
|
||||
|
@ -37,6 +38,11 @@ public class CommonElementTest {
|
|||
cb.getDriver().quit();
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
public void switchRootFrame() {
|
||||
ce.switchRootFrame();
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于测试非xml文件中的传参进行窗体切换与元素的获取
|
||||
*/
|
||||
|
@ -44,7 +50,7 @@ public class CommonElementTest {
|
|||
public void getCommonElementTest() {
|
||||
ce.switchFrame("//iframe[contains(@src, '/Regulatory/admin/index.jsp')]");
|
||||
ce.switchFrame("//iframe[contains(@src, '工资单管理')]");
|
||||
System.out.println(ce.getWebElement("//*[@id=\"listBox\"]/li[1]/div[1]/p/span[1]").getText());
|
||||
System.out.println(ce.getElement("//*[@id=\"listBox\"]/li[1]/div[1]/p/span[1]").getWebElement().getText());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -57,7 +63,7 @@ public class CommonElementTest {
|
|||
ce.setAutoSwitchIframe(false);
|
||||
ce.switchFrame("主窗体");
|
||||
ce.switchFrame("工资发放详情");
|
||||
System.out.println(ce.getWebElement("单位名称").getText());
|
||||
System.out.println(ce.getElement("单位名称").getWebElement().getText());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -67,7 +73,7 @@ public class CommonElementTest {
|
|||
public void autoLocationElementTest() {
|
||||
File xmlFile = new File("src/test/java/pres/auxiliary/work/selenium/element/测试文件.xml");
|
||||
ce.setXmlFile(xmlFile, false);
|
||||
System.out.println(ce.getWebElement("单位名称").getText());
|
||||
System.out.println(ce.getElement("单位名称").getWebElement().getText());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -80,6 +86,6 @@ public class CommonElementTest {
|
|||
//先切主窗体
|
||||
ce.switchFrame("主窗体");
|
||||
//在获取元素前,会判断元素所在窗体,由于主窗体是爷爷辈窗体,获取元素前会切换工资发放详情窗体
|
||||
System.out.println(ce.getWebElement("单位名称").getText());
|
||||
System.out.println(ce.getElement("单位名称").getWebElement().getText());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ package pres.auxiliary.work.selenium.event;
|
|||
import java.io.File;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.openqa.selenium.By;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.testng.annotations.AfterClass;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
|
@ -14,6 +13,8 @@ import com.alibaba.fastjson.JSONObject;
|
|||
|
||||
import pres.auxiliary.work.selenium.brower.ChromeBrower;
|
||||
import pres.auxiliary.work.selenium.brower.ChromeBrower.ChromeOptionType;
|
||||
import pres.auxiliary.work.selenium.element.CommonElement;
|
||||
import pres.auxiliary.work.selenium.element.Element;
|
||||
|
||||
/**
|
||||
* <p><b>文件名:</b>JsEventTest.java</p>
|
||||
|
@ -33,7 +34,7 @@ public class JsEventTest {
|
|||
/**
|
||||
* 输入文本框元素对象
|
||||
*/
|
||||
WebElement inputElemnt;
|
||||
Element inputElemnt;
|
||||
JsEvent event;
|
||||
ChromeBrower cb;
|
||||
|
||||
|
@ -42,11 +43,10 @@ public class JsEventTest {
|
|||
*/
|
||||
@BeforeClass
|
||||
public void init() {
|
||||
cb = new ChromeBrower(new File("Resource/BrowersDriver/Chrom/80.0.3987.163/chromedriver.exe"));
|
||||
cb = new ChromeBrower(new File("Resource/BrowersDriver/Chrom/78.0394.70/chromedriver.exe"));
|
||||
cb.addConfig(ChromeOptionType.CONTRAL_OPEN_BROWER, "127.0.0.1:9222");
|
||||
inputElemnt = new CommonElement(cb).getElement("//*[@id='kw']");
|
||||
|
||||
//获取输入文本框
|
||||
inputElemnt = cb.getDriver().findElement(By.xpath("//*[@id='kw']"));
|
||||
//初始化js类
|
||||
event = new JsEvent(cb.getDriver());
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ public class JsEventTest {
|
|||
*/
|
||||
@Test
|
||||
public void addElementTest_String() {
|
||||
System.out.println(event.addElement(inputElemnt, "bbb"));
|
||||
System.out.println(event.addElement(inputElemnt, "input"));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -99,8 +99,8 @@ public class JsEventTest {
|
|||
*/
|
||||
@Test
|
||||
public void addElementTest_Json() {
|
||||
JSONObject json = event.deleteElement(cb.getDriver().findElement(By.xpath("//*[@value = '百度一下']")));
|
||||
WebElement e = cb.getDriver().findElement(By.xpath("//*[text() = '百度热榜']"));
|
||||
JSONObject json = event.deleteElement(new CommonElement(cb.getDriver()).getElement("//*[@value = '百度一下']"));
|
||||
Element e = new CommonElement(cb.getDriver()).getElement("//*[text() = '我的关注']");
|
||||
System.out.println(event.addElement(e, json));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,105 @@
|
|||
package pres.auxiliary.work.selenium.event;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.testng.annotations.AfterClass;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import pres.auxiliary.work.selenium.brower.ChromeBrower;
|
||||
import pres.auxiliary.work.selenium.brower.ChromeBrower.ChromeOptionType;
|
||||
import pres.auxiliary.work.selenium.element.CommonElement;
|
||||
import pres.auxiliary.work.selenium.element.DataListElement;
|
||||
import pres.auxiliary.work.selenium.element.Element;
|
||||
|
||||
/**
|
||||
* <p><b>文件名:</b>CommonElementTest.java</p>
|
||||
* <p><b>用途:</b>
|
||||
* 对{@link TextEvent}类方法进行单元测试
|
||||
* </p>
|
||||
* <p><b>测试对象:</b>桂建通工资管理的工资单管理模块,获取第一条数据的单位信息</p>
|
||||
* <p><b>编码时间:</b>2020年4月30日上午7:44:34</p>
|
||||
* <p><b>修改时间:</b>2020年4月30日上午7:44:34</p>
|
||||
* @author 彭宇琦
|
||||
* @version Ver1.0
|
||||
* @since JDK 12
|
||||
*
|
||||
*/
|
||||
public class TextEventTest {
|
||||
/**
|
||||
* 列表上编号列第一个元素
|
||||
*/
|
||||
final String FRIST_ID_XPATH = "//*[@class=\"el-table__body-wrapper\"]/table/tbody/tr[1]/td[1]/div/span";
|
||||
/**
|
||||
* 列表上编号列所有元素
|
||||
*/
|
||||
final String ID_LIST_XPATH = "//*[@class=\"el-table__body-wrapper\"]/table/tbody/tr/td[1]/div/span";
|
||||
|
||||
ChromeBrower cb = new ChromeBrower(new File("Resource/BrowersDriver/Chrom/78.0394.70/chromedriver.exe"));
|
||||
CommonElement ce;
|
||||
DataListElement dle;
|
||||
TextEvent t;
|
||||
Element turningButton;
|
||||
|
||||
@BeforeClass
|
||||
public void init() {
|
||||
cb.addConfig(ChromeOptionType.CONTRAL_OPEN_BROWER, "127.0.0.1:9222");
|
||||
ce = new CommonElement(cb.getDriver());
|
||||
dle = new DataListElement(cb.getDriver());
|
||||
|
||||
t = new TextEvent(cb.getDriver());
|
||||
|
||||
turningButton = ce.getElement("//i[@class=\"el-icon el-icon-arrow-right\"]");
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public void qiut() {
|
||||
cb.getDriver().quit();
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
public void switchRootFrame() {
|
||||
ce.switchRootFrame();
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试普通元素调用{@link TextEvent#getText(pres.auxiliary.work.selenium.element.Element)}方法
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
@Test
|
||||
public void getTextTest_CommonElement() throws InterruptedException {
|
||||
System.out.println("第一页:");
|
||||
System.out.println(t.getText(ce.getElement(FRIST_ID_XPATH)));
|
||||
//测试元素过期问题
|
||||
System.out.println("==========================================");
|
||||
System.out.println("第二页:");
|
||||
//测试元素过期问题
|
||||
turningButton.getWebElement().click();
|
||||
Thread.sleep(5000);
|
||||
//翻页后再获取
|
||||
System.out.println(t.getText(ce.getElement(FRIST_ID_XPATH)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试列表元素调用{@link TextEvent#getText(pres.auxiliary.work.selenium.element.Element)}方法
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
@Test
|
||||
public void getTextTest_DataListElement() throws InterruptedException {
|
||||
dle.add(ID_LIST_XPATH);
|
||||
System.out.println("第一页:");
|
||||
dle.getAllWebElement(ID_LIST_XPATH).forEach(element -> {
|
||||
System.out.println(t.getText(element));
|
||||
});
|
||||
//测试元素过期问题
|
||||
System.out.println("==========================================");
|
||||
turningButton.getWebElement().click();
|
||||
Thread.sleep(5000);
|
||||
System.out.println("第二页:");
|
||||
//翻页后再次获取
|
||||
dle.getAllWebElement(ID_LIST_XPATH).forEach(element -> {
|
||||
System.out.println(t.getText(element));
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue