调整获取元素集合下标方法位置

This commit is contained in:
彭宇琦 2020-10-26 08:31:05 +08:00
parent 1e978a50cf
commit 70157cfc80
4 changed files with 45 additions and 41 deletions

View File

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

View File

@ -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}类对象

View File

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

View File

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