在編寫完Log類和監聽類之后,終於要回到正軌上來了。我們繼續開始寫UIExcutor的實現類。
PS:如果你想讓你的報告更加美觀一些。推薦使用reportNG這個jar包。
在項目中導入reportng-1.1.5.jar,這個jar包網上可以找到。可是具體在哪里下載的我也忘了。。。。。這里先提一下,到后面我們再來詳細的說明。
還記得我們之前寫過的UIExcutor的接口嗎?我們在這里需要一個一個的實現接口中的方法。(一個都不能少!)
package webui.xUtils; import java.util.Set; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.NoSuchElementException; 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 org.testng.Reporter; public class UIExcutorImpl implements UIExcutor{ private WebDriver driver; static logUtil logs = new logUtil(UIExcutorImpl.class); public UIExcutorImpl(WebDriver driver) { this.driver = driver; } public WebDriver getDriver() { return driver; } public void setDriver(WebDriver driver) { this.driver = driver; } //這里對接口中的方法進行重寫,意味着當我們執行這些方法的時候,可以將過程步驟作為log記錄下來,並加入report中。 @Override public void click(Position position) throws Exception { WebElement ele = getElement(position); ele.click(); logs.info("click元素:"+position.getPositionName()+"["+"By."+position.getType()+":"+position.getPath()+"] success!"); //在Report中顯示相應的log Reporter.log("click元素:"+position.getPositionName()+"["+"By."+position.getType()+":"+position.getPath()+"] success!"); } @Override public void sendKey(Position position, String value) throws Exception { WebElement ele = getElement(position); ele.sendKeys(value); logs.info("input輸入:"+position.getPositionName()+"["+"By."+position.getType()+":"+position.getPath()+" value:"+value+"]"); Reporter.log("input輸入:"+position.getPositionName()+"["+"By."+position.getType()+":"+position.getPath()+" value:"+value+"]"); } @Override public String getText(Position position) throws Exception { WebElement ele = getElement(position); String txt = ele.getText(); return txt; } @Override public WebElement getElement(Position position) throws Exception { WebElement ele = null; String path = position.getPath();
//這里私人指定的時間是3秒等待,用於頁面元素的加載,需要根據實際情況進行設置 WebDriverWait wait = new WebDriverWait(driver, 3); logs.info("查找元素:"+position.getPositionName()+"["+"By."+position.getType()+":"+path+"]"); Reporter.log("查找元素:"+position.getPositionName()+"["+"By."+position.getType()+":"+path+"]");
//使用switch-case分支語句來分別對應selenium八種定位方式 switch(position.getType()) { case xpath: try { wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(path))); ele = driver.findElement(By.xpath(path)); }catch (Exception e) { logs.error("findElment ByXpath:" + path + "-failed! NoSuchElement"); Reporter.log("findElment ByXpath:" + path + "-failed! NoSuchElement"); } break; case id: try { wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.id(path))); ele = driver.findElement(By.id(path)); }catch (Exception e) { logs.error("findElement ById" + path + "-failed! NoSuchELement"); Reporter.log("findElement ById" + path + "-failed! NoSuchELement"); } break; case className: try { wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className(path))); ele = driver.findElement(By.className(path)); }catch (Exception e) { logs.error("findElement ByClassName" + path + "-failed! NoSuchElement"); Reporter.log("findElement ByClassName" + path + "-failed! NoSuchElement"); } break; case linkText: try { wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.linkText(path))); ele = driver.findElement(By.linkText(path)); }catch (Exception e) { logs.error("findElement ByLinkText" + path + "-failed! NoSuchElement"); Reporter.log("findElement ByLinkText" + path + "-failed! NoSuchElement"); } break; case name: try { wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.name(path))); ele = driver.findElement(By.name(path)); }catch (Exception e) { logs.error("findElement ByName" + path + "-failed! NoSuchElement"); Reporter.log("findElement ByName" + path + "-failed! NoSuchElement"); } break; case cssSelector: try { wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector(path))); ele = driver.findElement(By.cssSelector(path)); }catch (Exception e) { logs.error("findElement ByCssSelector" + path + "-failed! NoSuchElement"); Reporter.log("findElement ByCssSelector" + path + "-failed! NoSuchElement"); } case tagName: try { wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.tagName(path))); ele = driver.findElement(By.tagName(path)); }catch (Exception e) { logs.error("findElement ByTagName" + path + "-failed ! NoSuchElement"); Reporter.log("findElement ByTagName" + path + "-failed ! NoSuchElement"); } case partialLinkText: try { wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.partialLinkText(path))); ele = driver.findElement(By.partialLinkText(path)); }catch (Exception e) { logs.error("findElement By partialLinkText" + path + "-failed ! NoSuchElement"); Reporter.log("findElement By partialLinkText" + path + "-failed ! NoSuchElement"); } default: break; } return ele; } @Override public boolean isElementDisplayed(Position position) throws Exception { WebElement ele = getElement(position); boolean flag = ele.isDisplayed(); return flag; } @Override public void switchWindow(String winTitle) { logs.info("切換windows窗口:" + winTitle); Reporter.log("切換windows窗口:" + winTitle); Set<String> handles = driver.getWindowHandles(); for (String handle : handles) { if (handle.equals(driver.getWindowHandle())) { continue; } else { driver.switchTo().window(handle); if (winTitle.contains(driver.getTitle())) { break; } else { continue; } } } } @Override public void switchFrame(Position position) { driver.switchTo().frame(position.getPath()); } @Override public void waitElement(Position position , int sec) { WebDriverWait wait = new WebDriverWait(driver, sec); String add = position.getPath(); switch (position.getType()) { case id: wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.id(add))); break; case xpath: wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(add))); break; case name: wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(add))); break; case linkText: wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(add))); break; case className: wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(add))); break; case tagName: wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(add))); break; case partialLinkText: wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(add))); break; case cssSelector: wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(add))); break; default: break; } } @Override public String getAlertText() { String alertText = ""; try { // webdriver對彈框的處理略坑,所以先等待2s再切換到彈框,否則可能無法切換 try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } Alert alert = driver.switchTo().alert(); alertText = alert.getText(); alert.accept(); } catch (NoSuchElementException e) { logs.error("no alert open,switch to alert failed"); Reporter.log("no alert open,switch to alert failed"); } return alertText; } @Override //獲取元素屬性值(提供屬性名,比如要獲取什么屬性(比如:value),這里就要輸入什么的值) public String getAttribute(Position position , String attributeName) throws Exception { WebElement ele = getElement(position); String value = ele.getAttribute(attributeName); return value; } @Override
//有些元素可以通過javaScript調用來強制點擊某個元素 public void jsClick(Position position) throws Exception { WebElement ele = getElement(position); ((JavascriptExecutor)driver).executeScript("arguments[0].click();", ele); logs.info("JavaScript-click元素:"+position.getPositionName()+"["+"By."+position.getType()+":"+position.getPath()+"] success!"); Reporter.log("JavaScript-click元素:"+position.getPositionName()+"["+"By."+position.getType()+":"+position.getPath()+"] success!"); } }
通過以上的代碼,對於之前的接口中的方法都進行了實現。
可以看出,我們實際上是對selenium提供的api進行了重寫,加入了自己定義的內容。這是一個思路,即可以調用原生api來進行重寫,實現自己的自定義內容。
那么頁面元素操作的實現類完成了之后,是不是就可以開始自動化腳本的編寫了呢?當然不是,我們還得考慮一個問題,就是瀏覽器。下次,我們會針對瀏覽器寫一個類,進行瀏覽器的初始化。