3

I am working in Selenium. I'm using Java language. I am following page object design pattern. I want to automate an autocomplete search test box. After entering a single character or multiple character autocomplete should display the name in the list. I want to automate this scenario.

Here is my code:

@FindBy(how=How.ID,using="category")
    WebElement category;
 public void setCategory(String cat){
        category.sendKeys(cat);

        selectOptionWithText(cat);
    }

Steps:

AppointmentPage appointmentPageobj= PageFactory.initElements(DriverManager.driver,AppointmentPage.class);
appointmentPageobj.setCategory(cat);

Test:

 AppointmentPage ex=log.Valid("9845612345","naveen","nav@gmail.com","Demo","2017-03-03","Dr. Bijoy Balakrishnan");

Screenshot: Screenshot of website with dropdown

1

I will write pseudocode that might pose some challenge (or not at all, depending on your experience), but is very robust when coded properly. Use explicit waits only.

I hope this way, you will learn something and also find your solution.

selectOptionWithText(string cat)
{
  sendKeys(cat); // put this in your method instead of before
  wait.Until(dropdown with expected value == visible);
  click expected value;
  wait.Until(dropdown != visible);
  wait.Until(expected value is in the input field);
}
| improve this answer | |
0

Try this:

driver.findElement(By.id("your searchBox")).sendKeys("your partial keyword");
List <WebElement> listItems = driver.findElements(By.xpath("your list item locator"));
listItems.get(0).click(); //make sure you have enough implicit wait on Global level to wait till list appear
| improve this answer | |
-1

I use following approach in C#, you can use parallel approach in Java Define a WebElement collection element with IList type for values in dropdown,as each dropdown value will have same class

[FindsBy(How = How.ClassName, Using = "<className Of the dropdown values>")]
public IList<IWebElement> DropDownOptionsList { get; set; }

public void selectOptionWithText(string cat)
        {
            if (cat.Length >= 1)
            {
                foreach (IWebElement item in DropDownOptionsList)
                {

                    if (item.Text ==cat)
                    {
                        Thread.Sleep(1000);
                        item.Click();
                        break;
                    }
                }
            }
        }

 public void setCategory(String cat){
        category.sendKeys(cat.Substring(0, <NumberofCharactersYOuWantToEnter>));

        selectOptionWithText(cat);
    }
| improve this answer | |
  • @AmoIO please help me to write the code in my above page object pattern. so I can understand – user24314 Mar 3 '17 at 9:57
  • Send me the class name of dropdown elements, and minimum number of caharacters to enter in dropdown. I will share sample C# code with you, as I am not a java person. – Amol Dakhane Mar 3 '17 at 10:01
  • Thread.Sleep = bad. Also this method isn't robust at all - what if the correct value takes a few seconds to load? – FDM May 2 '17 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy