Find if an option exist in select or not

Leave a comment

Here is the soluton to determine if the given option exist in the page (select item) or not

public boolean isSelectOptionPresent(WebDriver webdriver, String selectLocator, String optionToCheck) {
    if (webdriver == null) {
        throw new NullPointerException("WebDriver instance is null.");
    }

    if (null == optionToCheck || optionToCheck.isEmpty()) {
        throw new IllegalArgumentException("Invalid value
    }

    String[] availableOptions = webdriver.getSelectOptions(selectLocator);
    for(String option: availableOptions) {
        if (optionToCheck.equals(option) {
            return true;
        }
    }
    return false;
}

 

Compare selenium assertions

Leave a comment

Assertions in general are used to make sure what is expected is actually present or available. Selenium has no different meaning for assertions. Assertions in selenium are made on a web page. Selenium provides three type of assertions, as “waitFor”, “assert” and “verify”. An example of exact command can be ‘waitForTextPresent’, ‘assertTextPresent’ and ‘verifyTextPresent’.

When assertion is to ensure what is expected is actually present, why are there 3 types ? The answer is while these three types check for existence of what is expected, they differ in how they check and what they do when they succeed or fail.

waitFor : Returns ‘OK’ immediately if the condition is true. If the condition is not true, it waits for the specified (or set) timeout period and checks if the condition is true. Returns ‘OK’ if condition is met within timeout period. Returns ‘ERROR’ if the condition is not true within timeout period. The test continues after failure of “waitFor” command.

assert : Returns ‘OK’ if the condition is met when command is run. ‘ERROR’ otherwise.  It does not wait and re-check as it is done by ‘waitFor’ command. Also unlike ‘waitFor’ command (and ‘verify’ too), the test would be aborted if an ‘assert’ command fails.

verify : Returns ‘OK’ if the condition is met when command is run. It does not wait and re-check as it is done by ‘waitFor’ command. It returns ‘ERROR’ upon failure to meet the condition and the test continues.