• 基于Java+Selenium的WebUI自动化测试框架(五)------页面操作实现类


          在编写完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来进行重写,实现自己的自定义内容。

        那么页面元素操作的实现类完成了之后,是不是就可以开始自动化脚本的编写了呢?当然不是,我们还得考虑一个问题,就是浏览器。下次,我们会针对浏览器写一个类,进行浏览器的初始化。

  • 相关阅读:
    如何用Spring Security OAuth2 实现登录互踢,面试必学
    你确定Java注释不会被执行吗?80%的人都不知道
    常见Java性能问题一招帮你解决,80%的人都不知道
    Spring中的反射与反射的原理,案例详解
    10大Java方向最热门的开源项目,高薪必学
    2020最新Python学习常见十大问题总结
    Java源码系列-LinkedHashMap,高薪面试必学
    Java8的Lambda表达式你了解吗?80%的都不知道
    分块矩阵
    矩阵--逆矩阵和伴随矩阵
  • 原文地址:https://www.cnblogs.com/generalli2019/p/11419175.html
Copyright © 2020-2023  润新知