Forum Discussion

rezza's avatar
rezza
Occasional Contributor
4 years ago

How to select an option from selectBox

Hi
I can't select one of the options of the select box.
Actually, the select box opens when I run the test. But it doesn't select the option I want.
This is the function that I use in my stepDefinition:

 

    @Throws(Throwable::class)
    fun selectGender(gender: String?): UserProfileCompletionPage {
        clickOnTextFromDropdownList(selectBoxGender!!, gender!!)
        return UserProfileCompletionPage()
    }

 

 

And this is the content of clickOnTextFromDropdownList function:

 

@Throws(Exception::class)
    fun clickOnTextFromDropdownList(list: WebElement, textToSearchFor: String) {
        val tempWait: Wait<WebDriver> = WebDriverWait(DriverFactory.driver, 30)
        try {
            tempWait.until(ExpectedConditions.elementToBeClickable(list)).click()
            list.sendKeys(textToSearchFor)
            list.sendKeys(Keys.ENTER)
            println("Successfully sent the following keys: $textToSearchFor, to the following WebElement: <$list>")
        } catch (e: Exception) {
            println("Unable to send the following keys: $textToSearchFor, to the following WebElement: <$list>")
            Assert.fail("Unable to select the required text from the dropdown menu, Exception: " + e.message)
        }
    }

 

 

I'll be grateful if you guide me.
Thanks in advance.

No RepliesBe the first to reply