• Webdriver实现对菜单栏的灵活切换功能,附上代码,类似的菜单栏切换可以自己封装


    有时一级菜单下可能会有二级菜单,这时就需要对其下面的元素进行判断,如果使用webdriver原生的方法去获取未知的元素进行判断,显然是不可能的,因为webdriver本身就是基于明确的元素进行定位的,如果涉及到未知元素的判断,需要调用原生js或者Jquery

    一般页面效果图会如下所示:

    页面HTML源码:

    <li class="active"><a data-toggle="collapse" class="nav-header" href="#menuL1_3">中标目录管理</a><ul class="nav nav-list menuL2 in" id="menuL1_3" style="height: auto;"><li><a target="mainFrame" class="nav-page" href="gbc/gbcRequest/main.do">我的申请</a></li><li class="active"><a data-toggle="collapse" class="nav-sub" href="#menuL3_2_2">审核管理</a><ul class="nav nav-list menuL3 in" id="menuL3_2_2" style="height: auto;"><li><a target="mainFrame" class="nav-page" href="gbc/gbcCheckPending/main.do">待审核</a></li><li><a target="mainFrame" class="nav-page" href="gbc/gbcAudited/main.do">已审核</a></li></ul></li><li><a target="mainFrame" class="nav-page" href="gbc/gbcBiddingCatalogue/main.do">中标目录</a></li><li><a target="mainFrame" class="nav-page" href="gbc/gpoTags/main.do">标签管理</a></li><li><a target="mainFrame" class="nav-page" href="gbc/gbcPublishedHistory/main.do">发布历史记录</a></li><li><a target="mainFrame" class="nav-page" href="gbc/gbcCatLog/main.do">操作日志</a></li><li><a target="mainFrame" class="nav-page" href="gbc/gbcProcurementCatalogue/main.do">带量采购目录</a></li></ul></li>

    对菜单的所有切换与点击功能进行封装:

    import java.util.List;
    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;

    public class SelectMenue {
        private WebDriver driver;
        
    /**
     *
     * @param
     * driver 构造方法参数,取所调用页面的driver
     */
        public SelectMenue(WebDriver driver) {
            this.driver = driver;
        }

        /**
         * 此方法用于只有二级菜单时的切换
         * @param
         * ParentElement 父菜单
         * @param
         * TargetElement 目标菜单
         */
        public void switchMenue(String ParentElement, String TargetElement) {
            WebDriverWait wait = new WebDriverWait(driver, 10);
            JavascriptExecutor js = (JavascriptExecutor) driver;
            driver.switchTo().defaultContent();
            // 展开主菜单
            WebElement parentElement = driver.findElement(By.linkText(ParentElement));
            // 获取父菜单的下一个兄弟节点,并将其强制转换成WebElement类型
            WebElement brotherElement = (WebElement) js.executeScript("return arguments[0].parentNode.children[1]",
                    parentElement);
            // 当父菜单不是展开状态的时候就去点击父菜单
            if (!brotherElement.isDisplayed()) {
                parentElement.click();
                // 获取所有子菜单的集合,并不包含子菜单的子元素
                List<WebElement> elements = (List<WebElement>) js.executeScript("return arguments[0].children",
                        brotherElement);
                // 等待brotherElement下所有的li标签都变成可见
                wait.until(ExpectedConditions.visibilityOfAllElements(elements));
            }
            // 点击目标菜单
            brotherElement.findElement(By.linkText(TargetElement)).click();
            // 切换到主页面
            driver.switchTo().frame("mainFrame");
            wait.until(ExpectedConditions.presenceOfElementLocated(By.id("pageNavigationBar")));
        }

        /**
         * 此方法用于有三级菜单时的切换
         * @param
         * ParentElement 父菜单
         * @param
         * SecElement 二级菜单
         * @param
         * TargetElement 目标菜单
         */
        public void switchMenue(String ParentElement, String SecElement, String TargetElement) {
            WebDriverWait wait = new WebDriverWait(driver, 10);
            JavascriptExecutor js = (JavascriptExecutor) driver;
            //调用方法的第一件事,切换到默认文本,清除所有状态
            driver.switchTo().defaultContent();
            // 展开主菜单
            WebElement parentElement = driver.findElement(By.linkText(ParentElement));
            // 获取父菜单的下一个兄弟节点,并将其强制转换成WebElement类型
            WebElement brotherElement = (WebElement) js.executeScript("return arguments[0].parentNode.children[1]",
                    parentElement);
            // 当父菜单不是展开状态的时候就去点击父菜单
            if (!brotherElement.isDisplayed()) {
                parentElement.click();
                // 获取所有二级菜单的集合,并不包含二级菜单的子元素
                List<WebElement> elements = (List<WebElement>) js.executeScript("return arguments[0].children",
                        brotherElement);
                // 等待brotherElement下所有的li标签都变成可见
                wait.until(ExpectedConditions.visibilityOfAllElements(elements));
            }
            // 获取二级菜单
            WebElement secElement = brotherElement.findElement(By.linkText(SecElement));
            // 获取二级菜单的下一个兄弟节点,并将其强制转换成WebElement类型
            WebElement secBrotherElement = (WebElement) js.executeScript("return arguments[0].parentNode.children[1]",
                    secElement);

            if (!secBrotherElement.isDisplayed()) {
                // 展开二级菜单
                secElement.click();
                // 等待二级菜单下的所有li标签加载完成
                wait.until(ExpectedConditions.visibilityOfAllElements(secBrotherElement.findElements(By.tagName("li"))));
            }
            // 点击目标菜单
            secBrotherElement.findElement(By.linkText(TargetElement)).click();
            // 切换到主页面
            driver.switchTo().frame("mainFrame");
            wait.until(ExpectedConditions.presenceOfElementLocated(By.id("pageNavigationBar")));
        }
    }

  • 相关阅读:
    C# winform应用程序运行后,bin文件夹中会自动生成3个文件和一个应用程序
    c# 进程间通信
    C# Winform 右下角弹出框
    C#面向对象设计模式纵横谈(1):面向对象设计模式与原则 笔记
    C#面向对象设计模式纵横谈(2):Singleton 单件(创建型模式) 笔记
    《C# 设计模式》笔记: 第5章 继承
    《C# 设计模式》笔记: 第8章 简单工厂模式
    手动汉化 VS 2005 的代价
    《C# 设计模式》笔记: 第7章 C#中的数组、文件和异常
    收到微软寄给我的 SQL Server 2005 Beta3 光盘
  • 原文地址:https://www.cnblogs.com/zw520ly/p/5950342.html
Copyright © 2020-2023  润新知