import lombok.extern.log4j.Log4j;import org.openqa.selenium.By;import java.io.file;import java.io.fileinputStream;import java.util.PropertIEs;/** * @author liwen * 用来读取配置文件 */@Log4jpublic class ObjectMap { private static PropertIEs propertIEs = null; public ObjectMap(String filename) { propertIEs = new PropertIEs(); try { fileinputStream in = new fileinputStream((filename)); propertIEs.load(in); in.close(); } catch (Exception e) { System.out.println("找不到文件:" + filename); e.printstacktrace(); } } /** * 处理元素定位 * * @param name * @return * @throws Exception */ public By getLocateor(String name) throws Exception { String locator = ObjectMap.propertIEs.getProperty(name); String locatortype = locator.split(">>")[0].tolowerCase(); String locatorvalue = locator.split(">>")[1]; locatorvalue = new String(locatorvalue.getBytes("ISO-8859-1"),"UTF-8"); if ("CSSselector".equals(locatortype)) { return By.CSSSelector(locatorvalue); } else if ("ID".equals(locatortype)) { return By.ID(locatorvalue); } else if ("name".equals(locatortype)) { return By.name(locatorvalue); } else if ("classname".equals(locatortype)) { return By.classname(locatorvalue); } else if ("tagname".equals(locatortype)) { return By.tagname(locatorvalue); } else if ("linktext".equals(locatortype)) { return By.linkText(locatorvalue); } else if ("parialllinktest".equals(locatortype)) { return By.partiallinkText(locatorvalue); } else if ("xpath".equals(locatortype)) { return By.xpath(locatorvalue); } else { throw new Exception("该元素没有找到"); } }}元素文件
cn.index.signinBtn=ID>>kwcn.index.cart=ID>>sujd.closeBtn=ID>>closeBtnjd.input=xpath>>//*[@ID="search-Box-app"]/div/div[1]/inputjd.search=xpath>>//*[@ID="search-Box-app"]/div/div[1]/button/i简单 *** 作封装
//封装键盘 *** 作的方法public class KeyBoardUtil { //按Ctrl+F5 public static voID refresh(WebDriver driver) { Actions ctrl=new Actions(driver); ctrl.keyDown(Keys.CONTRol).perform(); try{ pressKeyEvent(KeyEvent.VK_F5); }catch (AWTException e) { e.printstacktrace(); } ctrl.keyUp(Keys.CONTRol).perform(); } //按物理键 public static voID pressKeyEvent(int keycode) throws AWTException{ Robot robot=new Robot(); //robot.keyPress(KeyEvent.VK_ENTER); robot.keyPress(keycode); } //鼠标左键按下和释放 public static voID MouseClickAndRelease(WebDriver driver){ Actions action=new Actions(driver); action.clickAndHold().perform(); try{ Thread.sleep(1000); }catch (Exception e) { e.printstacktrace(); } action.release().perform(); try{ Thread.sleep(1000); }catch (Exception e) { e.printstacktrace(); } } // 鼠标悬浮 public static voID perform(WebDriver driver,WebElement element) { Actions action = new Actions(driver); action.movetoElement(element).perform(); }智能等待封装
import com.Google.common.base.Function;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.webdriverwait;import java.util.List;/**封装各种等待方法*/public class WaitUtil { //显示等待多少秒 public static voID sleep(long seconds) { try { Thread.sleep(seconds); } catch (Exception e) { e.printstacktrace(); } } //显示等待页面标题是否出现了某个关键字 public static voID waitWebElementTitle(WebDriver driver,String Title) { webdriverwait wait = new webdriverwait(driver,10); wait.until((ExpectedConditions.TitleContains(Title))); } //显示等待页面元素的出现 public static voID waitWebElement(WebDriver driver,By by) { webdriverwait wait = new webdriverwait(driver,10); wait.until(ExpectedConditions.presenceOfElementLocated(by)); } //自定义等待某个元素显示 public static WebElement waitForElementVisible(WebDriver driver,final By locator,long timeOutInSeconds) { Function<WebDriver,WebElement> waitFn = new Function<WebDriver,WebElement>() { @OverrIDe public WebElement apply(WebDriver driver) { try { WebElement element = driver.findElement(locator); if (!element.isdisplayed()) { KeyBoardUtil.refresh(driver); WaitUtil.sleep(1000); } if (element.isdisplayed()) { return element; } } catch (Exception e) { return null; } return null; } }; webdriverwait wait = new webdriverwait(driver,timeOutInSeconds); return wait.until(waitFn); } //自定义某个元素列表是否出现 public static List<WebElement> waitForElementVisibleList(WebDriver driver,List<WebElement>> waitFn = new Function<WebDriver,List<WebElement>>() { @OverrIDe public List<WebElement> apply(WebDriver driver) { try { List<WebElement> elementList = driver.findElements(locator); if (elementList.isEmpty()) { KeyBoardUtil.refresh(driver); WaitUtil.sleep(1000); } if (!elementList.isEmpty()) { return elementList; } } catch (Exception e) { return null; } return null; } }; webdriverwait wait = new webdriverwait(driver,timeOutInSeconds); return wait.until(waitFn); } public static Boolean waitForElementPresent(WebDriver driver,Boolean> waitFn = new Function<WebDriver,Boolean>() { @OverrIDe public Boolean apply(WebDriver driver) { try { WebElement element = driver.findElement(locator); if (!element.isdisplayed()) { KeyBoardUtil.refresh(driver); WaitUtil.sleep(1000); } if (element.isdisplayed()) { return true; } } catch (Exception e) { return false; } return false; } }; webdriverwait wait = new webdriverwait(driver,timeOutInSeconds); return wait.until(waitFn); }断言封装 测试对象封装
import jdth.pcautomation.util.ObjectMap;import jdth.pcautomation.util.WaitUtil;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;/** * @author liwen * @Title: thop * @Description: * @date 2019/6/18 / 19:45 */public class topTh { public topTh() { objectMap = new ObjectMap("e:\test\loginIndex.propertIEs"); } private WebElement element = null; private ObjectMap objectMap; public WebElement getSiginBtn(WebDriver driver) throws Exception { WaitUtil.waitWebElement(driver,objectMap.getLocateor("cn.index.signinBtn")); element = driver.findElement(objectMap.getLocateor("cn.index.signinBtn")); return element; } public WebElement getSiginkw(WebDriver driver) throws Exception { WaitUtil.waitWebElement(driver,objectMap.getLocateor("cn.index.cart")); element = driver.findElement(objectMap.getLocateor("cn.index.cart")); return element; }}测试运行
import lombok.extern.log4j.Log4j;import org.openqa.selenium.WebDriver;import org.openqa.selenium.Chrome.ChromeDriver;import org.testng.annotations.AfterClass;import org.testng.annotations.BeforeClass;import org.testng.annotations.Test;/** * @author liwen * @Title: PcAoutTest * @Description: PC自动化 dome * @date 2019/6/18 / 14:10 */@Log4jpublic class PcAoutTest { public String url = "https://www.baIDu.com/"; WebDriver driver = null; @BeforeClass public WebDriver init() { //IE启动程序路径 System.setProperty("webdriver.Chrome.driver","D:\javaspaces\driver\chromedriver.exe"); driver = new ChromeDriver(); driver.manage().window().maximize(); return driver; } @Test public voID getbaIDu() throws Exception {// WebDriver driver = new ChromeDriver(); driver.get(url); log.info("调试"); new topTh().getSiginBtn(driver).sendKeys("测试"); new topTh().getSiginkw(driver).click(); Thread.sleep(2000);// driver.quit(); } }总结
以上是内存溢出为你收集整理的selenium *** 作web自动化小小封装体验全部内容,希望文章能够帮你解决selenium *** 作web自动化小小封装体验所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)