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.

Selenium Remote Control and WebDriver

Leave a comment

For my next project, I had to evaluate selenium remote control (a.k.a Selenium RC 1.0) and selenium webdriver (a.k.a., Selenium 2.0). The automation framework uses selenium core, which is a firefox extension, and hence supports only firefox browser. The goal is to upgrade the framework to use next version of selenium with minimal changes to the test scripts and picking up maximum new features of latest selenium, like multi-browser support, etc.

Here is the comparison

Selenium Remote Control

Selenium RC model consists of Selenium remote control server and selenium client libraries. Below is the architecture diagram (from seleniumhq.org). The test script is written using selenium client libraries. The selenium client libraries talk to the selenium remote control server, which will run the command on the browser specified by the test, gets the result from the browser and passes the result to selenium client.

_images/chapt5_img01_Architecture_Diagram_Simple.png

Selenium RC server is a java jar file. Selenium RC server listens on a port (default is 4444) to receive the commands from the selenium client and passes it to the browser using selenium core JavaScript commands. Browser runs the command received using its JavaScript interpreter and returns the result to the Selenium RC server. The selenium RC server then reports back the result to the Selenium Client.

Selenium Client are the interfaces between the test script and the selenium remote control server. The selenium client are available in multiple languages, allowing the users to write the tests in their favorite language. The Client Driver is available in languages such as C#, Java, Perl, PHP, Python, Ruby.

Limitations of the Selenium RC

  • Since Selenium RC model, internally works with JavaScript, lot of limitations are imposed by the browsers in order to protect user from malicious script. For example using Selenium RC one cannot navigate between the domains.
  • Selenium RC always requires a real browser, whereas using WebDriver, one can test the application using simultated browsers like HtmlUnit.

Selenium WebDriver

Selenium WebDriver is the current version of Selenium 2.0. It provides many new features and improvements over Selenium 1.0.  In the Selenium WebDriver model, there is no intermediate server to send the commands to the browser. Selenium WebDriver includes Selenium WebDriver API, which directly call the native functionality of the respective browser. This means if the test script is completely using Selenium WebDriver API there is no need to use Selenium RC server. However the package (selenium-standalone-server-version.jar) comes with both Selenium WebDriver and Selenium RC

However for backward compatibility, user can use Selenium-backed WebDriver (WebDriver API but with back-end selenium technology), for which Selenium Server (RC) is required.

Limitations of WebDriver

  • Since Selenium WebDriver directly interacts with browser’s native functionality, only a subset of browsers from those supported in Selenium RC are supported currently.
  • Selenium WebDriver API is currently available in Java, Python, C# and Ruby. Selenium officially has removed support for Perl and PHP, but there are third party drivers available for the languages.