0

I need to write a test case that clicks on dropdown menu, selects item size and then verifies if the selected size was chosen. Unfortunately I can't find any solution to do this. Below is my code:

private By sizeDropdownLocator = By.id("group_1");

public void selectItemSize() {

    WebDriverWait wait = new WebDriverWait(driver,10);

    WebElement dropdownElement = findElement(sizeDropdownLocator);

    wait.until(ExpectedConditions.visibilityOf(dropdownElement));

    Select dropdown = new Select(dropdownElement);

    dropdown.selectByVisibleText("M");

}


@Test
public void assertIfUserCanSelectItemSize() {
    HomePage homePage = new HomePage(driver, log);
    homePage.openHomePage();

    ItemDetailPage itemDetailPage = homePage.clickOnCard();

    itemDetailPage.selectItemSize();

    Assert.assertEquals(itemDetailPage.getSizeText(),"M");

}

Can anyone help me with this issue? Here's website that I'm using for Selenium practice: http://automationpractice.com/index.php?id_product=1&controller=product#/color-orange/size-m I'm not sure if I'm using wrong locators or if I don't use correct Selenium wait method.

EDIT: Ok so I managed to solve that. Here's my solution:

public void selectItemSize(String itemSize) {
    waitFor(ExpectedConditions.presenceOfElementLocated(sizeDropdownLocator),5);

    WebElement dropdownElement = findElement(sizeDropdownLocator);

    Select dropdown = new Select(dropdownElement);

    dropdown.selectByVisibleText(itemSize);
}

public String getSizeText() {
    WebElement dropdownElement = findElement(sizeDropdownLocator);

    Select dropdown = new Select(dropdownElement);

    return dropdown.getFirstSelectedOption().getText();
}

 @Test
public void assertIfUserCanSelectItemSize() {
    HomePage homePage = new HomePage(driver, log);
    homePage.openHomePage();

    ItemDetailPage itemDetailPage = homePage.clickOnCard();
    itemDetailPage.selectItemSize("M");

    Assert.assertEquals(itemDetailPage.getSizeText(), "M");
}

If anyone has any tips and reviews regarding my code please let me know. I'm still learning selenium and every piece of advice will be much appreciated :)

Your Answer

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

Browse other questions tagged or ask your own question.