dropDowns - denhoefel/EcommerceSeleniumProject GitHub Wiki

Page Object

image

package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

import java.util.List;
import java.util.stream.Collectors;

public class DropDownPage {


    private WebDriver driver;
    private By dropdown = By.id("dropdown");

    public DropDownPage(WebDriver driver){
        this.driver = driver;
    }

    private Select findDropDownElement(){ //This method finds a dropdown element
        return new Select(driver.findElement(dropdown));
    }

    public void selectFromDropDown(String option){ // this method will select the option you want from the dropdown
        findDropDownElement().selectByVisibleText(option);
    }



    public List<String> getSelectedOptions(){ //Get the number of options selected
        List<WebElement> selectedElements = findDropDownElement().getAllSelectedOptions();//get all the options from the dropdown
        return selectedElements.stream().map(e->e.getText()).collect(Collectors.toList());//
    }

}

Tests

package DropDown;

import base.BaseTests;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

public class DropDownTests extends BaseTests {

    @Test
    public void testSelectOption(){
        var dropDownPage = homePage.clickDropDown();

        String option = "Option 1";
        dropDownPage.selectFromDropDown(option);
        var selectedOptions = dropDownPage.getSelectedOptions();
        assertEquals(selectedOptions.size(), 1, "Incorrect number of selections");
        assertTrue(selectedOptions.contains(option), "Option not selected");
    }
}
⚠️ **GitHub.com Fallback** ⚠️