【问题标题】:Any refactoring suggestion任何重构建议
【发布时间】:2020-02-01 06:58:09
【问题描述】:

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

WebElement CurrentObject = null; 开关(搜索){ 案例“类名”: 尝试 { CurrentObject = new WebDriverWait(驱动程序,ConstantValues.LONGWAIT) .until(ExpectedConditions.presenceOfElementLocated(By.className(SearchPar))); } 捕捉(异常 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! <<<");
    }

【问题讨论】:

  • 重构问题可能更适合codereview.stackexchange.com/tour
  • 你试过什么?你被困在哪里了?提示:查看重复的代码并尝试将其移出 switch 语句。
  • @ErwinBolwidt 此代码正在运行,发现我只需要建议以使其更好。我已经在考虑删除重复代码并创建一个函数然后调用它的想法

标签: java selenium


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    如果 searchBy 总是匹配一个方法名,那么我认为反射可能是解决方案。

    Method searchMethod = By.class.getMethod(searchBy, returnClass.class);
    CurrentObject = new WebDriverWait(driver, ConstantValues.LONGWAIT).until(ExpectedConditions.presenceOfElementLocated(searchMethod.invoke(null,searchPar);
    

    您必须捕获一些可能的异常。

    【讨论】:

      【解决方案3】:

      我强烈建议不要以这种方式编写代码,而是创建一个框架。

      创建一个 SeleniumUtility 类(或您想要的任何名称)并编写如下方法: 示例(您可以删除 WebDriver 驱动程序)

      /**
       * Method returns WebElement by Xpath.
       * 
       * @param String xpathExpression
       * @param WebDriver driver
       * @return WebElement
       */
      public WebElement getElementByXpath(String xpathExpression, WebDriver driver){
          return driver.findElement(By.xpath(xpathExpression));
      }
      
      /**
       * Method returns WebElement by ID.
       * 
       * @param String id
       * @param WebDriver driver
       * @return WebElement
       */
      public WebElement getElementByID(String id, WebDriver driver){
          return driver.findElement(By.id(id));
      }
      

      或者是这样的:

      /**
       * Method returns By.
       * 
       * @param String identifier Example: xpath,id,name etc
       * @param String expression Example: //*[@class='text']
       * @return By
       */
      public By getBy(String identifier, String expression){
          switch (identifier.toLowerCase()) {
          case "xpath":
              return By.xpath(expression);
          case "id":
              return By.id(expression);
          case "name":
              return By.name(expression);
          case "classname":
              return By.className(expression);
          case "cssselector":
              return By.cssSelector(expression);
          case "linktext":
              return By.linkText(expression);
          case "partiallinktext":
              return By.partialLinkText(expression);
          case "tagname":
              return By.tagName(expression);
          default:
              throw new RuntimeException("Invalid identifier passed: " + identifier);
          }
      }
      

      在不同的类中编写显式等待和流畅等待,并反复使用它。

      现在您的整个代码将是这样的:

              try {
                  CurrentObject = waitTillElementLocated(getBy("cssselector","SearchPar"));
              } catch (Exception e) {
                  System.out.println("Element not found: " + e);
              }
      

              try {
                  CurrentObject = waitTillElementLocated(getBy("id","SearchPar"));
              } catch (Exception e) {
                  System.out.println("Element not found: " + e);
              }
      

      【讨论】:

        猜你喜欢
        • 2021-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多