修改类名,添加下拉选项元素获取类

This commit is contained in:
彭宇琦 2020-05-23 21:00:09 +08:00
parent e62c68b777
commit 16492d5dfb
11 changed files with 185 additions and 51 deletions

View File

@ -31,7 +31,7 @@ import pres.auxiliary.work.selenium.brower.Page;
* @version Ver1.0
* @since JDK 12
*/
public abstract class AbstractElement {
public abstract class AbstractBy {
/**
* 用于存储浏览器的WebDriver对象设为静态保证所有的子类只使用一个WebDriver对象以避免造成WebDriver不正确导致的Bug
*/
@ -73,7 +73,7 @@ public abstract class AbstractElement {
*
* @param driver {@link WebDriver}对象
*/
public AbstractElement(WebDriver driver) {
public AbstractBy(WebDriver driver) {
this.driver = driver;
browserHandles = this.driver.getWindowHandle();
}
@ -82,7 +82,7 @@ public abstract class AbstractElement {
* 通过浏览器对象{@link AbstractBrower}进行构造
* @param brower {@link AbstractBrower}对象
*/
public AbstractElement(AbstractBrower brower) {
public AbstractBy(AbstractBrower brower) {
this.brower = brower;
this.driver = brower.getDriver();
}

View File

@ -22,13 +22,13 @@ import pres.auxiliary.work.selenium.brower.AbstractBrower;
* @since JDK 12
*
*/
public class CommonElement extends AbstractElement {
public class CommonBy extends AbstractBy {
/**
* 构造对象并存储浏览器的WebDriver对象
*
* @param driver 浏览器的WebDriver对象
*/
public CommonElement(WebDriver driver) {
public CommonBy(WebDriver driver) {
super(driver);
}
@ -36,7 +36,7 @@ public class CommonElement extends AbstractElement {
* 通过浏览器对象{@link AbstractBrower}进行构造
* @param brower {@link AbstractBrower}对象
*/
public CommonElement(AbstractBrower brower) {
public CommonBy(AbstractBrower brower) {
super(brower);
}

View File

@ -26,7 +26,7 @@ import pres.auxiliary.work.selenium.brower.AbstractBrower;
* @version Ver1.0
* @since JDK 12
*/
public class DataListElement extends ListElement {
public class DataListBy extends ListBy {
/**
* 存储获取到的元素列表中最多元素列的元素个数
*/
@ -63,7 +63,7 @@ public class DataListElement extends ListElement {
* 通过浏览器对象{@link AbstractBrower}进行构造
* @param brower {@link AbstractBrower}对象
*/
public DataListElement(AbstractBrower brower) {
public DataListBy(AbstractBrower brower) {
super(brower);
}
@ -72,7 +72,7 @@ public class DataListElement extends ListElement {
*
* @param driver 浏览器的{@link WebDriver}对象
*/
public DataListElement(WebDriver driver) {
public DataListBy(WebDriver driver) {
super(driver);
}

View File

@ -88,7 +88,8 @@ public class Element {
element = driver.findElements(by).get(index);
break;
case SELECT_OPTION_ELEMENT:
new Select(element).getOptions().get(index);
element = new Select(driver.findElement(by)).getOptions().get(index);
break;
default:
throw new IllegalArgumentException("Unexpected value: " + elementType);
}

View File

@ -22,7 +22,7 @@ import pres.auxiliary.work.selenium.brower.AbstractBrower;
* @since JDK 12
*
*/
public abstract class ListElement extends MultiElement {
public abstract class ListBy extends MultiBy {
/**
* 用于存储获取到的列表一列元素key为列表名称value为列表元素
*/
@ -32,7 +32,7 @@ public abstract class ListElement extends MultiElement {
* 通过浏览器对象{@link AbstractBrower}进行构造
* @param brower {@link AbstractBrower}对象
*/
public ListElement(AbstractBrower brower) {
public ListBy(AbstractBrower brower) {
super(brower);
}
@ -41,7 +41,7 @@ public abstract class ListElement extends MultiElement {
*
* @param driver 浏览器的{@link WebDriver}对象
*/
public ListElement(WebDriver driver) {
public ListBy(WebDriver driver) {
super(driver);
}
@ -167,7 +167,7 @@ public abstract class ListElement extends MultiElement {
}
// 转义下标
index = getIndex(elementMap.get(element).size(), index);
index = getIndex(elementMap.get(element).size(), index, true);
// 转义下标后返回对应的元素
return elementMap.get(element).get(index);

View File

@ -21,12 +21,12 @@ import pres.auxiliary.work.selenium.brower.AbstractBrower;
* @since JDK 12
*
*/
public abstract class MultiElement extends AbstractElement {
public abstract class MultiBy extends AbstractBy {
/**
* 通过浏览器对象{@link AbstractBrower}进行构造
* @param brower {@link AbstractBrower}对象
*/
public MultiElement(AbstractBrower brower) {
public MultiBy(AbstractBrower brower) {
super(brower);
}
@ -35,7 +35,7 @@ public abstract class MultiElement extends AbstractElement {
*
* @param driver 浏览器的{@link WebDriver}对象
*/
public MultiElement(WebDriver driver) {
public MultiBy(WebDriver driver) {
super(driver);
}
@ -87,10 +87,11 @@ public abstract class MultiElement extends AbstractElement {
* 故可通过该方法对下标的含义进行转义得到java能识别的下标
* @param length 元素的个数
* @param index 传入的下标
* @param randomZero 标记是否可以随机出数字0
* @return 可识别的下标
* @throws NoSuchElementException 当元素无法查找到时抛出的异常
*/
int getIndex(int length, int index) {
int getIndex(int length, int index, boolean randomZero) {
//判断元素下标是否超出范围由于可以传入负数故需要使用绝对值
if (Math.abs(index) > length) {
throw new NoSuchElementException("指定的选项值大于选项的最大值。选项总个数:" + length + ",指定项:" + index);
@ -104,8 +105,13 @@ public abstract class MultiElement extends AbstractElement {
//选择元素由于index为负数则长度加上选项值即可得到需要选择的选项
return length + index;
} else {
//为0则随机进行选择
return new Random().nextInt(length);
//为0则进行随机选择但需要判断是否允许随机出0第一个元素
int newindex = 0;
do {
newindex = new Random().nextInt(length);
} while(newindex == 0 && !randomZero);
return newindex;
}
}
}

View File

@ -2,6 +2,7 @@ package pres.auxiliary.work.selenium.element;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
@ -12,7 +13,7 @@ import org.openqa.selenium.support.ui.WebDriverWait;
import pres.auxiliary.selenium.xml.ByType;
import pres.auxiliary.work.selenium.brower.AbstractBrower;
public class SelectElement extends MultiElement {
public class SelectBy extends MultiBy {
/**
* 用于存储获取下拉选项时的信息
*/
@ -27,34 +28,47 @@ public class SelectElement extends MultiElement {
ArrayList<String> optionText = new ArrayList<>();
/**
* 设置获取的下拉选项的类型0表示标准的select-option型1表示非标准的下拉选项
* 设置标记的下拉选项的类
*/
private ElementType elementType;
private boolean fristIsEmpty = false;
/**
* 通过浏览器对象{@link AbstractBrower}进行构造
* 通过浏览器对象{@link AbstractBrower}进行构造并指定第一个选项是否为空选项或者为请选择等文本
* 的选项若为该选项时则在随机选择时不会选择到该选项
*
* @param brower {@link AbstractBrower}对象
* @param fristIsEmpty 指定第一个元素是否为空串或为请选择等文本
*/
public SelectElement(AbstractBrower brower) {
public SelectBy(AbstractBrower brower, boolean fristIsEmpty) {
super(brower);
this.fristIsEmpty = fristIsEmpty;
}
/**
* 构造对象并存储浏览器的{@link WebDriver}对象
* 构造对象并存储浏览器的{@link WebDriver}对象并指定第一个选项是否为空选项或者为请选择等文本
* 的选项若为该选项时则在随机选择时不会选择到该选项
*
* @param driver 浏览器的{@link WebDriver}对象
* @param fristIsEmpty 指定第一个元素是否为空串或为请选择等文本
*/
public SelectElement(WebDriver driver) {
public SelectBy(WebDriver driver, boolean fristIsEmpty) {
super(driver);
this.fristIsEmpty = fristIsEmpty;
}
@Override
public void add(String name, ByType byType) {
add(new ElementInformation(name, byType));
elementInfo = new ElementInformation(name, byType);
add(elementInfo);
}
@Override
void add(ElementInformation elementInformation) {
//清除原存储的内容
clear();
//获取元素的By对象
By by = recognitionElement(elementInformation);
//根据By获取元素
@ -64,7 +78,7 @@ public class SelectElement extends MultiElement {
//根据获取到的元素个数来判断下拉选项的类型
if (size == 1) {
elementInformation.elementType = ElementType.SELECT_OPTION_ELEMENT;
elementType = ElementType.SELECT_OPTION_ELEMENT;
//若是标准下拉选项型则需要改变size的值方便后续添加数据
Select select = new Select(driver.findElement(by));
@ -72,7 +86,7 @@ public class SelectElement extends MultiElement {
elementList = select.getOptions();
size = elementList.size();
} else {
elementInformation.elementType = ElementType.SELECT_DATAS_ELEMENT;
elementType = ElementType.SELECT_DATAS_ELEMENT;
}
//构造Element对象
@ -80,7 +94,7 @@ public class SelectElement extends MultiElement {
//获取元素
option.add(new Element(driver, elementType, by, i));
//获取元素的文本内容
optionText.add(elementList.get(0).getText());
optionText.add(elementList.get(i).getText());
}
}
@ -91,7 +105,8 @@ public class SelectElement extends MultiElement {
* @return 相应的选项元素
*/
public Element getElement(int index) {
return option.get(getIndex(option.size(), index));
//当首选项为空时则在随机时不允许产生0
return option.get(getIndex(option.size(), index, !fristIsEmpty));
}
/**
@ -114,8 +129,7 @@ public class SelectElement extends MultiElement {
@Override
public void againGetElement() {
option.clear();
optionText.clear();
clear();
add(elementInfo);
}
@ -124,8 +138,37 @@ public class SelectElement extends MultiElement {
//当查找到元素时则返回true若查不到元素则会抛出异常故返回false
return new WebDriverWait(driver, waitTime, 200).
until((driver) -> {
WebElement element = driver.findElement(by);
return element != null;
List<WebElement> elements = driver.findElements(by);
//根据是否能查找到元素进行判断
if (elements.size() > 0) {
//若获取到的第一个元素的标签名为select标准下拉则可以直接返回true
if ("select".equals(elements.get(0).getTagName())) {
return true;
}
//若查到元素再进一步判断元素内容是否完全加载
int textSize = elements.stream().filter(element -> {
return !element.getText().isEmpty();
}).collect(Collectors.toList()).size();
//若首选项为空时则加载的内容必须大于或等于总选项个数-1
//若首选项不为空时则加载的内容必须与原选项个数一致
if (fristIsEmpty) {
return textSize >= elements.size() - 1;
} else {
return textSize == elements.size();
}
} else {
return false;
}
});
}
/**
* 用于清除原存储的内容
*/
void clear() {
option.clear();
optionText.clear();
}
}

View File

@ -13,7 +13,7 @@ import pres.auxiliary.work.selenium.brower.ChromeBrower.ChromeOptionType;
/**
* <p><b>文件名</b>CommonElementTest.java</p>
* <p><b>用途</b>
* {@link CommonElement}类方法进行单元测试
* {@link CommonBy}类方法进行单元测试
* </p>
* <p><b>测试对象</b>桂建通工资管理的工资单管理模块获取第一条数据的单位信息</p>
* <p><b>编码时间</b>2020年4月30日上午7:44:34</p>
@ -23,14 +23,14 @@ import pres.auxiliary.work.selenium.brower.ChromeBrower.ChromeOptionType;
* @since JDK 12
*
*/
public class CommonElementTest {
public class CommonByTest {
ChromeBrower cb = new ChromeBrower(new File("Resource/BrowersDriver/Chrom/78.0394.70/chromedriver.exe"));
CommonElement ce;
CommonBy ce;
@BeforeClass
public void initDate() {
cb.addConfig(ChromeOptionType.CONTRAL_OPEN_BROWER, "127.0.0.1:9222");
ce = new CommonElement(cb);
ce = new CommonBy(cb);
}
@AfterClass

View File

@ -0,0 +1,84 @@
package pres.auxiliary.work.selenium.element;
import java.io.File;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
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.event.ClickEvent;
/**
* <p><b>文件名</b>SelectByText.java</p>
* <p><b>用途</b>
* {@link SelectBy}类进行测试
* </p>
* <p><b>页面</b>
* 对标准型下拉选项测试页面为jira提BUG弹窗对非标准型下拉为运营系统测试环境消息推送管理页面
* </p>
* <p><b>编码时间</b>2020年5月23日下午4:29:10</p>
* <p><b>修改时间</b>2020年5月23日下午4:29:10</p>
* @author 彭宇琦
* @version Ver1.0
* @since JDK 12
*
*/
public class SelectByTest {
ChromeBrower cb = new ChromeBrower(new File("Resource/BrowersDriver/Chrom/78.0394.70/chromedriver.exe"));
SelectBy s;
CommonBy cby;
ClickEvent ce;
@BeforeClass(alwaysRun = true)
public void init() {
cb.addConfig(ChromeOptionType.CONTRAL_OPEN_BROWER, "127.0.0.1:9222");
s = new SelectBy(cb, false);
cby = new CommonBy(cb);
ce = new ClickEvent(cb.getDriver());
}
@AfterClass(alwaysRun = true)
public void quit() {
cb.getDriver().quit();
}
/**
* 用于测试选择非标准型下拉选项
* @throws InterruptedException
*/
@Test(groups = "list")
public void changeElement_List() throws InterruptedException {
Element e = cby.getElement("/html/body/div[1]/div/div/section/div/div[1]/div[4]/div[1]/input");
ce.click(e);
s.add("/html/body/div/div/div[1]/ul/li/span");
//按照下标进行选择(第三个选项待审核)
ce.click(s.getElement(3));
Thread.sleep(2000);
ce.click(e);
//按照文本进行选择
ce.click(s.getElement("审核通过"));
}
/**
* 测试标准型拉下选项的选择
* @throws InterruptedException
*/
@Test(groups = "commom")
public void addTest_Common() throws InterruptedException {
s.add("//label[text()='严重等级']/../select");
//按照下标进行选择(第三个选项轻微)
ce.click(s.getElement(3));
Thread.sleep(2000);
//按照文本进行选择
ce.click(s.getElement("致命"));
s.add("//label[text()='缺陷来源']/../select");
//按照下标进行选择(第三个选项与需求不一致)
ce.click(s.getElement(3));
}
}

View File

@ -13,7 +13,7 @@ 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.CommonBy;
import pres.auxiliary.work.selenium.element.Element;
/**
@ -45,7 +45,7 @@ public class JsEventTest {
public void init() {
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 = new CommonBy(cb).getElement("//*[@id='kw']");
//初始化js类
event = new JsEvent(cb.getDriver());
@ -99,8 +99,8 @@ public class JsEventTest {
*/
@Test
public void addElementTest_Json() {
JSONObject json = event.deleteElement(new CommonElement(cb.getDriver()).getElement("//*[@value = '百度一下']"));
Element e = new CommonElement(cb.getDriver()).getElement("//*[text() = '我的关注']");
JSONObject json = event.deleteElement(new CommonBy(cb.getDriver()).getElement("//*[@value = '百度一下']"));
Element e = new CommonBy(cb.getDriver()).getElement("//*[text() = '我的关注']");
System.out.println(event.addElement(e, json));
}
}

View File

@ -9,8 +9,8 @@ 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.CommonBy;
import pres.auxiliary.work.selenium.element.DataListBy;
import pres.auxiliary.work.selenium.element.Element;
/**
@ -37,16 +37,16 @@ public class TextEventTest {
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;
CommonBy ce;
DataListBy 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());
ce = new CommonBy(cb.getDriver());
dle = new DataListBy(cb.getDriver());
t = new TextEvent(cb.getDriver());
@ -89,7 +89,7 @@ public class TextEventTest {
public void getTextTest_DataListElement() throws InterruptedException {
dle.add(ID_LIST_XPATH);
System.out.println("第一页:");
dle.getAllWebElement(ID_LIST_XPATH).forEach(element -> {
dle.getAllElement(ID_LIST_XPATH).forEach(element -> {
System.out.println(t.getText(element));
});
//测试元素过期问题
@ -98,7 +98,7 @@ public class TextEventTest {
Thread.sleep(5000);
System.out.println("第二页:");
//翻页后再次获取
dle.getAllWebElement(ID_LIST_XPATH).forEach(element -> {
dle.getAllElement(ID_LIST_XPATH).forEach(element -> {
System.out.println(t.getText(element));
});
}