目录
2、对应xml的实体类Page.java、UIElement.java
3、解析xml的工具类UILibraryUtil.java。以下代码可以使用,但是了好优化一下取元素的方式。(每次多重循环取值,太费时间了。)
1、新建UILibrary.xml
<Pages>
<Page keyword="登录页面" url="http://www.baidu.com">
<UIElement keyword="登录名" by="id" value="name"/>
<UIElement keyword="登录密码" by="id" value="password"/>
<UIElement keyword="验证码" by="id" value="validCode"/>
<UIElement keyword="登录按钮" by="tagName" value="button"/>
</Page>
<Page keyword="系统管理-日志管理" url="http://www.baidu.com?m=1_104">
<UIElement keyword="菜单名" by="id" value="name"/>
<UIElement keyword="查询按钮" by="link_text" value="查询"/>
</Page>
</Pages>
2、对应xml的实体类Page.java、UIElement.java
package com.lz.pojo;
import java.util.List;
public class Page {
private String keyword;
private String url;
private List<UIElement> uiElements;
public Page(String keyword, String url, List<UIElement> uiElements) {
this.keyword = keyword;
this.url = url;
this.uiElements = uiElements;
}
}
package com.lz.pojo;
public class UIElement {
private String keyword;
private String by;
private String value;
public UIElement(String keyword, String by, String value) {
this.keyword = keyword;
this.by = by;
this.value = value;
}
}
3、解析xml的工具类UILibraryUtil.java。以下代码可以使用,但是了好优化一下取元素的方式。(每次多重循环取值,太费时间了。)
package com.lz.util;
import com.lz.cases.Base;
import com.lz.pojo.Page;
import com.lz.pojo.UIElement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class UILibraryUtil {
public static List<Page> pages = new ArrayList<>();
static {
loadPages("src/test/resources/ui/UILibrary.xml");
}
/**
* 解析ui库
*
* @param uiLibraryPath
*/
public static void loadPages(String uiLibraryPath) {
try {
SAXReader reader = new SAXReader();
InputStream in;
in = new FileInputStream(new File(uiLibraryPath));
Document document = reader.read(in);
Element root = document.getRootElement();
List<Element> pagesElement = root.elements("Page");
for (Element pageElement : pagesElement) {
String url = pageElement.attributeValue("url");
String pageKeyword = pageElement.attributeValue("keyword");
List<Element> uiElements = pageElement.elements("UIElement");
List<UIElement> uiElementsList = new ArrayList<>();
for (Element uiElement : uiElements) {
String uiElementKeyword = uiElement.attributeValue("keyword");
String by = uiElement.attributeValue("by");
String value = uiElement.attributeValue("value");
UIElement uiEle = new UIElement(uiElementKeyword, by, value);
uiElementsList.add(uiEle);
}
Page page = new Page(pageKeyword, url, uiElementsList);
pages.add(page);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("解析xml异常");
}
}
/**
* 获取元素属性
*
* @param pageKeyword
* @param elementKeyword
* @return
*/
public static WebElement getElementByKeyword(String pageKeyword, String elementKeyword) {
WebElement element = null;
for (Page page : pages) {
if(pageKeyword.equals(page.getKeyword())){
List<UIElement> uiElements = page.getUiElements();
for (UIElement uiEle : uiElements) {
if(elementKeyword.equals(uiEle.getKeyword())){
element=getVisibleElement(uiEle.getBy(),uiEle.getValue());
}
}
}
}
return element;
}
/**
*
* @param by
* @param value
* @return
*/
private static WebElement getVisibleElement(String by, String value) {
WebDriverWait wait=new WebDriverWait(Base.driver,20);
By locatorBy=null;
WebElement element=null;
if("id".equalsIgnoreCase(by)){
locatorBy=By.id(value);
}else if("name".equalsIgnoreCase(by)){
locatorBy=By.name(value);
}else if("className".equalsIgnoreCase(by)){
locatorBy=By.className(value);
}else if("tagName".equalsIgnoreCase(by)){
locatorBy=By.tagName(value);
}else if("linkText".equalsIgnoreCase(by)){
locatorBy=By.linkText(value);
}else if("partialLinkText".equalsIgnoreCase(by)){
locatorBy=By.partialLinkText(value);
}else if("cssSelector".equalsIgnoreCase(by)){
locatorBy=By.cssSelector(value);
}else if("xpath".equalsIgnoreCase(by)){
locatorBy=By.xpath(value);
}else {
System.out.println("暂不支持类型:【"+by+"】");
}
try {
element=wait.until(ExpectedConditions.visibilityOfElementLocated(locatorBy));
} catch (Exception e) {
if(e instanceof TimeoutException){
System.out.println("根据by:["+by+"],value["+value+"]定位元素超时");
}else {
e.printStackTrace();
System.out.println("根据by:["+by+"],value["+value+"]定位元素异常");
}
}
return element;
}
}
4、最后,测试用例中使用工具类
package com.lz.cases;
import com.lz.util.UILibraryUtil;
import com.lz.util.xls.LoginCaseUtil;
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
* @Des
* @auther
* @Date 2021/5/20 15:21
*/
public class LoginCase extends Base{
/**
* 反向
* @param loginName
* @param password
* @param verifyCode
* @throws InterruptedException
*/
@Test(dataProvider = "reverseDatas")
public void test1(String loginName,String password,String verifyCode) throws InterruptedException {
driver.get("http://www.baidu.com");
UILibraryUtil.getElementByKeyword("登录页面","登录名").sendKeys(loginName);
UILibraryUtil.getElementByKeyword("登录页面","登录密码").sendKeys(password);
UILibraryUtil.getElementByKeyword("登录页面","验证码").sendKeys(verifyCode);
UILibraryUtil.getElementByKeyword("登录页面","登录按钮").click();
Assert.assertEquals("0","0");
}
/**
* 反向测试数据
* @return
*/
@DataProvider
public Object[][] reverseDatas() {
return LoginCaseUtil.getDatas(cellNames,"0");
}
}