2

Can anyone help me solution for query

There are 2 dropdown. One dropdown contains all the "Countries" and 2nd dropdown contains all the "States". You have to fetch all the Values from "Country" Dropdow. Select the Country as "US" and have to Count,retrieve,verify all the "states"(in 2nd Dropdown) of Country("US"). I have Written the Code but can't complete

public class test {
static{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
System.setProperty("webdriver.gecko.driver", "./drivers/geckodriver.exe");  
}
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver= new FirefoxDriver();

driver.manage().window().maximize();
int count=0, total=0;


driver.get("file:///C:/Users/usetr/Desktop/Country.html");

WebElement alllistBox = driver.findElement(By.id("Country"));


Select slt = new Select(alllistBox);
//Select Country India
slt.selectByVisibleText("India");

//Printing All country in dropdown and thier counts
List<WebElement> alloptions = slt.getOptions();
for (WebElement option : alloptions) {

System.out.println(option.getText());
count++;
}
System.out.println("Number of country present " + count);
2
  • similar to the manner you selected India, select india in first drop dowm. Then get the count from second drop down as you did for the first dropdown in the code... Commented Jul 16, 2017 at 18:10
  • What is your exact manual step? In the description you mentioned about Count,retrieve,verify all the "states", Yes we can Count and retrieve but verify all the "states" with respect to what? Thanks Commented Jul 16, 2017 at 18:47

1 Answer 1

1

Here is the Answer to your Question:

Assuming that you want to retrieve all the options from a DropDown, count and verify the selected option you can consider to use the following code block. This code block logs into Facebook, gets all the options from Day DropDown, then selects 28 as the date, verifies the date with the pre-defined value of 28 and prints out the result Pass or Fail:

import java.util.List;

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.support.ui.Select;

public class Q45131741_DropDown_options 
{

    static
    {
        System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");  
    }

    public static void main(String[] args) 
    {

        String date_to_select = "28";
        WebDriver driver= new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("https://www.facebook.com/");
        WebElement birthdayDay = driver.findElement(By.id("day"));
        Select selectDay = new Select(birthdayDay);
        List<WebElement> day_list = selectDay.getOptions();
        System.out.println("The options for this DropDown are : ");
        for (WebElement day:day_list)
        {
            System.out.println(day.getAttribute("innerHTML"));
        }
        selectDay.selectByVisibleText("28");
        String date_selected = selectDay.getFirstSelectedOption().getText();
        System.out.println("Date selected is : "+date_selected);
        System.out.println("Date to be selected was : "+date_to_select);
        if(date_selected.contains(date_to_select))
        {
            System.out.println("Testcase Passed");
        }
        else
        {
            System.out.println("Testcase Failed");
        }
    }
}

Let me know if this Answers your Question.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.