猿问

任何重构建议

我有一个很长的 switch 语句代码(大约 8 种情况),它决定使用什么搜索来查找浏览器中的元素。有什么建议如何重构这段代码吗?


WebElement 当前对象 = null; switch (SearchBy) { case "className": try { CurrentObject = new WebDriverWait(driver, ConstantValues.LONGWAIT) .until(ExpectedConditions.presenceOfElementLocated(By.className(SearchPar))); } catch (Exception e) { System.out.println("未找到元素:" + e); } 休息;


    case "cssSelector":

        try {

            CurrentObject = new WebDriverWait(driver, ConstantValues.LONGWAIT)

                    .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(SearchPar)));

        } catch (Exception e) {

            System.out.println("Element not found: " + e);

        }

        break;


    case "id":

        try {

            CurrentObject = new WebDriverWait(driver, ConstantValues.LONGWAIT)

                    .until(ExpectedConditions.presenceOfElementLocated(By.id(SearchPar)));

        } catch (Exception e) {

            System.out.println("Element not found: " + e);

        }

        break;


    case "linkText":

        try {

            CurrentObject = new WebDriverWait(driver, ConstantValues.LONGWAIT)

                    .until(ExpectedConditions.presenceOfElementLocated(By.linkText(SearchPar)));

        } catch (Exception e) {

            System.out.println("Element not found: " + e);

        }

        break;


    case "name":

        try {

            CurrentObject = new WebDriverWait(driver, ConstantValues.LONGWAIT)

                    .until(ExpectedConditions.presenceOfElementLocated(By.name(SearchPar)));

        } catch (Exception e) {

            System.out.println("Element not found: " + e);

        }

        break;

   default:

        System.out.println(">>> SEARCH BY KEYWORD IS NOT VALID! <<<");

    }



holdtom
浏览 89回答 3
3回答

拉风的咖菲猫

Switch-case是许多编程语言中的反模式。为了避免它们,您可以使用一些类似于Replace conditional with polymorphismJava 的技术。我建议将它们与 一起使用Reflection。这是 Java 的一个功能。

温温酱

我强烈建议不要以这种方式编写代码,而是创建一个框架。创建一个 SeleniumUtility 类(或任何您想要的名称)并编写如下方法: 示例(您可以删除 WebDriver 驱动程序)/**&nbsp;* Method returns WebElement by Xpath.&nbsp;*&nbsp;&nbsp;* @param String xpathExpression&nbsp;* @param WebDriver driver&nbsp;* @return WebElement&nbsp;*/public WebElement getElementByXpath(String xpathExpression, WebDriver driver){&nbsp; &nbsp; return driver.findElement(By.xpath(xpathExpression));}/**&nbsp;* Method returns WebElement by ID.&nbsp;*&nbsp;&nbsp;* @param String id&nbsp;* @param WebDriver driver&nbsp;* @return WebElement&nbsp;*/public WebElement getElementByID(String id, WebDriver driver){&nbsp; &nbsp; return driver.findElement(By.id(id));}或者是这样的:/**&nbsp;* Method returns By.&nbsp;*&nbsp;&nbsp;* @param String identifier Example: xpath,id,name etc&nbsp;* @param String expression Example: //*[@class='text']&nbsp;* @return By&nbsp;*/public By getBy(String identifier, String expression){&nbsp; &nbsp; switch (identifier.toLowerCase()) {&nbsp; &nbsp; case "xpath":&nbsp; &nbsp; &nbsp; &nbsp; return By.xpath(expression);&nbsp; &nbsp; case "id":&nbsp; &nbsp; &nbsp; &nbsp; return By.id(expression);&nbsp; &nbsp; case "name":&nbsp; &nbsp; &nbsp; &nbsp; return By.name(expression);&nbsp; &nbsp; case "classname":&nbsp; &nbsp; &nbsp; &nbsp; return By.className(expression);&nbsp; &nbsp; case "cssselector":&nbsp; &nbsp; &nbsp; &nbsp; return By.cssSelector(expression);&nbsp; &nbsp; case "linktext":&nbsp; &nbsp; &nbsp; &nbsp; return By.linkText(expression);&nbsp; &nbsp; case "partiallinktext":&nbsp; &nbsp; &nbsp; &nbsp; return By.partialLinkText(expression);&nbsp; &nbsp; case "tagname":&nbsp; &nbsp; &nbsp; &nbsp; return By.tagName(expression);&nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException("Invalid identifier passed: " + identifier);&nbsp; &nbsp; }}在不同的类中编写显式等待和流畅等待并反复使用。现在你的整个代码将是这样的:&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CurrentObject = waitTillElementLocated(getBy("cssselector","SearchPar"));&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Element not found: " + e);&nbsp; &nbsp; &nbsp; &nbsp; }或者&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CurrentObject = waitTillElementLocated(getBy("id","SearchPar"));&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Element not found: " + e);&nbsp; &nbsp; &nbsp; &nbsp; }

凤凰求蛊

如果 searchBy 总是匹配方法名称,那么我认为反射可能是解决方案。Method&nbsp;searchMethod&nbsp;=&nbsp;By.class.getMethod(searchBy,&nbsp;returnClass.class); CurrentObject&nbsp;=&nbsp;new&nbsp;WebDriverWait(driver,&nbsp;ConstantValues.LONGWAIT).until(ExpectedConditions.presenceOfElementLocated(searchMethod.invoke(null,searchPar);您必须捕获一些可能的异常。
随时随地看视频慕课网APP

相关分类

Java
我要回答