PAGE 34 : HOW TO WORK WITH AUTOCOMPLETE TEXT BOX IN SELENIUM WEBDRIVER

Working with AutoComplete Text box

Now a days, in most of the applications, we can see a 'Auto Complete' textboxes which will help users to quickly find the option from a pre-populated list of values based on the text that is entered by the user. It mainly concentrates on providing suggestions to users while typing into the field.
Let us now see a basic example. When we enter any text, we can select the value from the pre-populated list by using 'String' or 'Index value'
auto complete textbox
<input id="searchTerm" class="text ui-autocomplete-input" type="text" value="" name="searchTerm" placeholder="Search" autocomplete="off"/>
<input id="propertyFacet" type="hidden" value="" name="propertyFacet"/>
By Looking at the DOM you can identify whether its a auto complete box or not.
Example :package com.package1; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.annotations.Test; public class AutomComplete { WebDriver driver; @Test(priority=1) public void setUp() { driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("http://materials.springer.com"); driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); driver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS); } @Test(priority=2) public void autoCompleteBox() throws InterruptedException { WebElement autoCompleteBox = driver.findElement(By.id("searchTerm")); autoCompleteBox.click(); autoCompleteBox.sendKeys("Testosterone hydrate"); // Create object on Actions class Actions builder = new Actions(driver); // find the element which we want to Select from auto suggestion WebElement ele = driver.findElement(By.xpath("//li[@class='ui-menu-item']/a/strong[1]")); // use Mouse hover action for that element builder.moveToElement(ele).build().perform(); // Give wait for 2 seconds WebDriverWait wait = new WebDriverWait(driver,5); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[@class='ui-menu-item']/a/strong[1]")));; // finally click on that element builder.click(ele).build().perform(); } } Another Example:package com.pack.auto; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class AutoCompleteExample { WebDriver driver; WebDriverWait wait; String URL = "http://jqueryui.com/autocomplete/"; private By frameLocator = By.className("demo-frame"); private By tagText = By.id("tags"); @BeforeClass public void Setup() { driver = new FirefoxDriver(); driver.manage().window().maximize(); wait = new WebDriverWait(driver, 5); } @Test public void rightClickTest() { driver.navigate().to(URL); WebElement frameElement=driver.findElement(frameLocator); driver.switchTo().frame(frameElement); wait.until(ExpectedConditions.presenceOfElementLocated(tagText)); WebElement textBoxElement = driver.findElement(tagText); textBoxElement.sendKeys("a"); selectOptionWithText("Java"); //selectOptionWithIndex(2); }
Below is the code to select the Option based on the string passed in the Test. We are List as option can be more than one. By iterating the list we will select the required option.
 public void selectOptionWithText(String textToSelect) {
  try {
   WebElement autoOptions = driver.findElement(By.id("ui-id-1"));
   wait.until(ExpectedConditions.visibilityOf(autoOptions));

   List<WebElement> optionsToSelect = autoOptions.findElements(By.tagName("li"));
   for(WebElement option : optionsToSelect){
          if(option.getText().equals(textToSelect)) {
           System.out.println("Trying to select: "+textToSelect);
              option.click();
              break;
          }
      }
   
  } catch (NoSuchElementException e) {
   System.out.println(e.getStackTrace());
  }
  catch (Exception e) {
   System.out.println(e.getStackTrace());
  }
 }
Below is the method to select Option based on the index value. We need to pass the index value to select the required value. If you are not specific to the value, we can just select first value always.
public void selectOptionWithIndex(int indexToSelect) {
  
  try {
   WebElement autoOptions = driver.findElement(By.id("ui-id-1"));
   wait.until(ExpectedConditions.visibilityOf(autoOptions));

   List<WebElement> optionsToSelect = autoOptions.findElements(By.tagName("li"));
          if(indexToSelect<=optionsToSelect.size()) {
           System.out.println("Trying to select based on index: "+indexToSelect);
             optionsToSelect.get(indexToSelect).click();
          }
  }   
  catch (NoSuchElementException e) {
   System.out.println(e.getStackTrace());
  }
  catch (Exception e) {
   System.out.println(e.getStackTrace());
  }
 }

 @AfterClass
 public void tearDown() {
  driver.quit();
 }

}

No comments:

About Me

My photo
Pune, Maharastra, India
You can reach me out at : jimmiamrit@gmail.com