调整获取元素集合下标方法位置
This commit is contained in:
parent
1e978a50cf
commit
70157cfc80
|
@ -2,8 +2,10 @@ package pres.auxiliary.work.selenium.element;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import org.openqa.selenium.By;
|
||||
import org.openqa.selenium.NoSuchElementException;
|
||||
import org.openqa.selenium.TimeoutException;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.support.ui.WebDriverWait;
|
||||
|
@ -296,4 +298,31 @@ public abstract class AbstractBy {
|
|||
return new ArrayList<WebElement>();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 由于方法允许传入负数和特殊数字0为下标,并且下标的序号由1开始,故可通过该方法对下标的含义进行转义,得到java能识别的下标
|
||||
*
|
||||
* @param length 元素的个数
|
||||
* @param index 传入的下标
|
||||
* @return 可识别的下标
|
||||
* @throws NoSuchElementException 当元素无法查找到时抛出的异常
|
||||
*/
|
||||
protected int toElementIndex(int length, int index, boolean firstEmpty) {
|
||||
// 判断元素下标是否超出范围,由于可以传入负数,故需要使用绝对值
|
||||
if (Math.abs(index) > length) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 判断index的值,若大于0,则从前向后遍历,若小于0,则从后往前遍历,若等于0,则随机输入
|
||||
if (index > 0) {
|
||||
// 选择元素,正数的选项值从1开始,故需要减小1
|
||||
return index - 1;
|
||||
} else if (index < 0) {
|
||||
// 选择元素,由于index为负数,则长度加上选项值即可得到需要选择的选项
|
||||
return length + index;
|
||||
} else {
|
||||
Random ran = new Random();
|
||||
return firstEmpty ? (ran.nextInt(length - 1) + 1) : ran.nextInt(length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ import pres.auxiliary.work.selenium.brower.AbstractBrower;
|
|||
* @version Ver1.0
|
||||
*
|
||||
*/
|
||||
public class DataListBy extends MultiBy {
|
||||
public class DataListBy extends MultiBy<DataListBy> {
|
||||
/**
|
||||
* 构造方法,初始化浏览器对象
|
||||
* @param brower {@link AbstractBrower}类对象
|
||||
|
|
|
@ -2,9 +2,7 @@ package pres.auxiliary.work.selenium.element;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import org.openqa.selenium.NoSuchElementException;
|
||||
import org.openqa.selenium.TimeoutException;
|
||||
|
||||
import pres.auxiliary.work.selenium.brower.AbstractBrower;
|
||||
|
@ -27,7 +25,7 @@ import pres.auxiliary.work.selenium.brower.AbstractBrower;
|
|||
* @version Ver1.0
|
||||
*
|
||||
*/
|
||||
public abstract class MultiBy extends AbstractBy {
|
||||
public abstract class MultiBy<T extends MultiBy<T>> extends AbstractBy {
|
||||
/**
|
||||
* 用于记录当前元素集合中第一个元素是否为不可选择的元素,用于对元素的随机返回
|
||||
*/
|
||||
|
@ -60,7 +58,8 @@ public abstract class MultiBy extends AbstractBy {
|
|||
* @param elementName 元素名称
|
||||
* @param linkKeys 外链词语
|
||||
*/
|
||||
public void find(String elementName, String... linkKeys) {
|
||||
@SuppressWarnings("unchecked")
|
||||
public T find(String elementName, String... linkKeys) {
|
||||
//根据元素名称,获取元素信息数据
|
||||
elementData = new ElementData(elementName, read);
|
||||
elementData.addLinkWord(linkKeys);
|
||||
|
@ -76,6 +75,8 @@ public abstract class MultiBy extends AbstractBy {
|
|||
} catch (TimeoutException e) {
|
||||
elementList = new ArrayList<>();
|
||||
}
|
||||
|
||||
return (T)this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -98,9 +99,9 @@ public abstract class MultiBy extends AbstractBy {
|
|||
public boolean removeElement(int index) {
|
||||
//当且仅当元素集合存在,并且下标传入正确时,将元素删除,并返回成功
|
||||
if (elementList != null && elementList.size() != 0) {
|
||||
int newIndex = toElementIndex(elementList.size(), index);
|
||||
int newIndex = toElementIndex(elementList.size(), index, firstEmpty);
|
||||
if (newIndex != -1) {
|
||||
elementList.remove(toElementIndex(elementList.size(), index));
|
||||
elementList.remove(toElementIndex(elementList.size(), index, firstEmpty));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -130,7 +131,7 @@ public abstract class MultiBy extends AbstractBy {
|
|||
* @return {@link Element}类对象
|
||||
*/
|
||||
public Element getElement(int index) {
|
||||
return new Element(toElementIndex(elementList.size(), index), elementList, elementData, this);
|
||||
return new Element(toElementIndex(elementList.size(), index, firstEmpty), elementData, this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -151,36 +152,9 @@ public abstract class MultiBy extends AbstractBy {
|
|||
|
||||
//循环,遍历类中的elementList,根据其长度,构造Elemenet对象,并进行添加
|
||||
for (int index = 0; index < this.elementList.size(); index++) {
|
||||
elementList.add(new Element(index, this.elementList, elementData, this));
|
||||
elementList.add(new Element(index, elementData, this));
|
||||
}
|
||||
|
||||
return elementList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 由于方法允许传入负数和特殊数字0为下标,并且下标的序号由1开始,故可通过该方法对下标的含义进行转义,得到java能识别的下标
|
||||
*
|
||||
* @param length 元素的个数
|
||||
* @param index 传入的下标
|
||||
* @return 可识别的下标
|
||||
* @throws NoSuchElementException 当元素无法查找到时抛出的异常
|
||||
*/
|
||||
protected int toElementIndex(int length, int index) {
|
||||
// 判断元素下标是否超出范围,由于可以传入负数,故需要使用绝对值
|
||||
if (Math.abs(index) > length) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 判断index的值,若大于0,则从前向后遍历,若小于0,则从后往前遍历,若等于0,则随机输入
|
||||
if (index > 0) {
|
||||
// 选择元素,正数的选项值从1开始,故需要减小1
|
||||
return index - 1;
|
||||
} else if (index < 0) {
|
||||
// 选择元素,由于index为负数,则长度加上选项值即可得到需要选择的选项
|
||||
return length + index;
|
||||
} else {
|
||||
Random ran = new Random();
|
||||
return firstEmpty ? (ran.nextInt(length - 1) + 1) : ran.nextInt(length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ import org.openqa.selenium.support.ui.Select;
|
|||
|
||||
import pres.auxiliary.work.selenium.brower.AbstractBrower;
|
||||
|
||||
public class SelectBy extends MultiBy {
|
||||
public class SelectBy extends MultiBy<SelectBy> {
|
||||
/**
|
||||
* 定义标准下拉选项的tagname
|
||||
*/
|
||||
|
@ -59,11 +59,11 @@ public class SelectBy extends MultiBy {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void find(String elementName, String... linkKeys) {
|
||||
public SelectBy find(String elementName, String... linkKeys) {
|
||||
super.find(elementName, linkKeys);
|
||||
//判断当前存储的元素集合中是否为空,若为空,则直接返回
|
||||
if (elementList == null || elementList.size() == 0) {
|
||||
return;
|
||||
return this;
|
||||
}
|
||||
|
||||
//判断当前元素集合中,第一个元素的tagName是否为select,若为select,则表示其为标准下拉
|
||||
|
@ -71,12 +71,13 @@ public class SelectBy extends MultiBy {
|
|||
if (SELECT_TAGNAME.equals(elementList.get(0).getTagName())) {
|
||||
elementList = new Select(elementList.get(0)).getOptions();
|
||||
if (elementList == null || elementList.size() == 0) {
|
||||
return;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
//按照设置的查找方式,查找元素内容
|
||||
findElementContent();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -93,7 +94,7 @@ public class SelectBy extends MultiBy {
|
|||
if (index > -1) {
|
||||
return getElement(index + 1);
|
||||
} else {
|
||||
return new Element(-1, elementList, elementData, this);
|
||||
return new Element(-1, elementData, this);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue