autocomplete is not showing exact value in this below code ,please have a look at it .

11 views
Skip to first unread message

Hara Mohapatra

unread,
Nov 22, 2016, 11:33:21 PM11/22/16
to webdriver
    package calenderhandling;
    
    import static org.junit.Assert.*;
    
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    
    import org.junit.After;
    import org.junit.AfterClass;
    import org.junit.Before;
    import org.junit.BeforeClass;
    import org.junit.Test;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class AutoComplete {
    
    private WebDriver driver;
    private String baseURL;
   
    @Before
    public void setUp() throws Exception {
   
    System.setProperty("webdriver.gecko.driver", "C:\\Users\\dilu316\\Downloads\\selenium workspace\\geckodriver-v0.11.1-win64\\geckodriver.exe");
    driver = new FirefoxDriver();
   
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
   
    }
   
    
    @Test
    public void testAutoComplete() throws InterruptedException {
    driver.get(baseURL);
    String partialText = "new york";
    
    String fullText = "New York/Newark, NJ - EWR"; //expected but enter code herereturning other option
   
    WebElement fromText = driver.findElement(By.id("originAirport_displayed"));
    fromText.click();
    fromText.sendKeys(partialText);
   
    WebElement element = driver.findElement(By.xpath("//div[@class='ac_results']"));
    List<WebElement> results = element.findElements(By.tagName("li"));
   
    int size = results.size();
   
    for(int i=0; i<size; i++)
    {
    System.out.println(results.get(i).getText());
    }
    Thread.sleep(5000);
   
    for(WebElement text:results)
    {   
                //anyway the fulltext is not being validated
    if(text.getText().equals(fullText))
    {
    text.click();
    break;
    }
    }
    }
    
    @After
    public void tearDown() throws Exception {
    }
    
    
    }

darrell grainger

unread,
Nov 23, 2016, 11:44:13 AM11/23/16
to webdriver
If I understand what you are trying to do here, you want:

  1. Go to Southwest Airline booking site
  2. Find the FROM input
  3. Enter the text 'new york'
  4. Search the list of suggestions until you find "New York/Newark, NJ - EWR"
  5. Click this suggestion
At this point the FROM input show have the value "New York/Newark, NJ - EWR" in it. I whipped up the following:

@Test
public void autoCompleteFromInput() throws InterruptedException {
    WebElement fromInput = driver.findElement(By.cssSelector("#originAirport_displayed"));
    sleep(2000);
    fromInput.click();
    fromInput.sendKeys("new york");
    sleep(2000);
    List<WebElement> suggestions = driver.findElements(By.cssSelector("div.ac_results li"));
    for (WebElement suggestion : suggestions) {
        String s = suggestion.getText();
        System.out.println(s);
        if(s.equals("New York/Newark, NJ - EWR")) {
            System.out.println("Clicking it!!!!");
            suggestion.click();
            break;
        }
    }
    sleep(10000);
}

I noticed a few things when running this. If I run it on Chrome, Selenium 3, I can actually remove the sleep(2000) statements and the code works fine. While it is sleeping for 10 seconds at the end, I flip over to the browser and I can see that the suggestion.click() worked. It has selected EWR. With Firefox, I had to add the sleep(2000) statements in. If I did not add the sleep statements in the suggestions list would be empty and it would skip the for loop. For a final implementation I would change the sleep statements to a WebDriverWait.

But even with the sleep statements, I see Firefox find the list of suggestions. I see it go into the if statement and click the web element. But when I look at the browser it has not populated the FROM field with EWR. This leads me to believe that the suggestion.click() is not working on Firefox.

I even checked the web element to make sure it was visible and enabled. Everything looks the same from the debugger. I just find the Firefox cannot seem to click this element. Maybe file a bug with geckodriver.
Reply all
Reply to author
Forward
0 new messages