mcdcorp / opentest

Open source test automation tool for web applications, mobile apps and APIs
https://getopentest.org
MIT License
447 stars 107 forks source link

How to get selected value from Select drop-down #543

Closed IamRajB closed 2 years ago

IamRajB commented 2 years ago

Hi @adrianth

I want to get the selected option's text/value from a drop-down. Is there any way to get that?

For e.g. In a scenario where I want to verify there is no option selected at all. How can I achieve that?

adrianth commented 2 years ago

If you just need to validate the selected value in the dropdown, you can use the AssertListOption keyword. You can validate the selected item by option number, option value or the option text.

If you need to read the value so you can use it further, the only option I can think of is to just run a client-side script using ExecuteScript.

For the time being, the only way to verify that there is no option selected is by using the ExecuteScript keyword and to run JavaScript on the client side, something like this:

- description: Read dropdown value
  action: org.getopentest.selenium.ExecuteScript
  args:
    script: |
        var e = document.getElementById("drop-down-id");
        return e.selectedIndex; // will be -1 if nothing is selected

- script: |
    $log("The selected index is: " + $output.result)

Keep in mind that this also depends on the implementation of the dropdown. For example, most implementations use a first option with an empty value, so in this case the selectedIndex property will actually be 0 instead of -1. If that's the case, then you can simply validate the selected option by using the AssertListOption keyword that I mentioned in the beginning.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.