/**
   * Fills an autocomplete field with a given value.
   *
   * @param driver The WebDriver instance we are working with
   * @param value The new value, or null to generate a non-empty value
   * @param autocompleteInputElement The WebElement into which we are inputting the value
   */
  public static void fillAutocompleteField(
      WebDriver driver, WebElement autocompleteInputElement, String value) {

    if (autocompleteInputElement != null) {
      autocompleteInputElement.click();
      autocompleteInputElement.sendKeys(value);

      new WebDriverWait(driver, 10)
          .until(
              ExpectedConditions.visibilityOfElementLocated(By.className("cs-autocomplete-popup")));

      WebElement popupElement = driver.findElement(By.className("cs-autocomplete-popup"));
      WebElement matchesElement =
          popupElement.findElement(By.className("csc-autocomplete-Matches"));
      WebElement matchSpanElement = null;

      new WebDriverWait(driver, 10)
          .until(
              ExpectedConditions.visibilityOfElementLocated(By.className("cs-autocomplete-popup")));

      for (WebElement candidateMatchElement : matchesElement.findElements(By.tagName("li"))) {
        WebElement candidateMatchSpanElement =
            candidateMatchElement.findElement(By.tagName("span"));

        if (candidateMatchSpanElement.getText().equals(value)) {
          matchSpanElement = candidateMatchSpanElement;
          break;
        }
      }

      if (matchSpanElement != null) {
        // Click the value if found
        new WebDriverWait(driver, 10)
            .until(
                ExpectedConditions.visibilityOfElementLocated(
                    By.className("cs-autocomplete-popup")));

        matchSpanElement.click();

      } else {
        // create a new authority item if one matching it does not already exist
        new WebDriverWait(driver, 10)
            .until(
                ExpectedConditions.visibilityOfElementLocated(
                    By.className("cs-autocomplete-popup")));

        WebElement addToPanelElement =
            popupElement.findElement(By.className("csc-autocomplete-addToPanel"));
        WebElement firstAuthorityItem = addToPanelElement.findElement(By.tagName("li"));

        firstAuthorityItem.click();

        while (findElementsWithTimeout(driver, 0, By.className("cs-autocomplete-popup")).size()
            > 0) {
          // Wait for the popup to close
        }
      }
    } else {
    }
  }
  /**
   * @param driver webdriver object to access the browser
   * @param term the search term expected in the results
   * @return is it true or not
   */
  public static Boolean isInSearchResults(WebDriver driver, String term, Integer pageCounter) {
    Boolean result = Boolean.FALSE;
    String xpath = "//tr[@class='csc-row']/td/a[text()='" + term + "']";
    String textTemplate;
    String currentPageIndicatorFieldText;

    if (!driver.findElements(By.xpath(xpath)).isEmpty()) {
      result = Boolean.TRUE;
    } else {
      try {
        driver.findElement(By.className("flc-pager-next")).click();
        new WebDriverWait(driver, 10)
            .until(
                ExpectedConditions.invisibilityOfElementLocated(
                    By.className("cs-loading-indicator")));

        pageCounter += 1;
        WebElement textField = driver.findElement(By.xpath("//*[@id=\"pager-bottom\"]/li[5]"));

        textTemplate = "Viewing page " + pageCounter + ".";
        currentPageIndicatorFieldText = textField.getText();

        if (!(currentPageIndicatorFieldText.contains(textTemplate))) {
          return Boolean
              .FALSE; // fixes infinite loop of button-clicking when the item is not found.
        }
        result = isInSearchResults(driver, term, pageCounter);
      } catch (Exception e) {
        log(e.getMessage());
      }
    }
    return result;
  }