Selenium Automating Web Applications for Testing

Selenium automates browsers. That's it! What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well.

Selenium has the support of some of the largest browser vendors who have taken (or are taking) steps to make Selenium a native part of their browser. It is also the core technology in countless other browser automation tools, APIs and frameworks.

A. Selenium IDE

The Selenium-IDE (Integrated Development Environment) is the tool you use to develop your Selenium test cases. It's an easy-to-use Firefox plug-in and is generally the most efficient way to develop test cases. It also contains a context menu that allows you to first select a UI element from the browser's currently displayed page and then select from a list of Selenium commands with parameters pre-defined according to the context of the selected UI element. This is not only a time-saver, but also an excellent way of learning Selenium script syntax.

See Selenium IDE

B. Selenium-WebDriver API Commands and Operations (by Java)

  1. Fetching a Page

  2. Locating UI Elements (WebElements)

    // find element by ID
    // e.g. <p id="coolestWidgetEvah">...</p>
    WebElement element = driver.findElement("coolestWidgetEvah"));
    // find element by Class Name
    // e.g.
    //   <p class="cheese"><span>Cheddar</span></p>
    //   <p class="cheese"><span>Gouda</span></p>
    List cheeses = driver.findElements(By.className("cheese"));
    // find element by Tag Name
    // e.g. <iframe src="..."></iframe>
    WebElement frame = driver.findElement(By.tagName("iframe"));
    // find element by Name
    // e.g. <input name="cheese" type="text"/>
    WebElement cheese = driver.findElement("cheese"));
    // find element by Link Text
    // e.g. <a href="" rel="nofollow">cheese</a>
    WebElement cheese = driver.findElement(By.linkText("cheese"));
    // find element by Partial Link Text
    // e.g. <a href="" rel="nofollow">search for cheese</a>
    WebElement cheese = driver.findElement(By.partialLinkText("cheese"));
    // find element by CSS
    // e.g. <p id="food"><span class="dairy">milk</span><span class="dairy aged">cheese</span></p>
    WebElement cheese = driver.findElement(By.cssSelector("#food span.dairy.aged"));
    // find element by XPATH
    // e.g. <input type="text" name="example">
    List inputs = driver.findElements(By.xpath("//input"));
    // find element by JavaScript
    // simple example on a page that has jQuery loaded
    WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('.cheese')[0]");
    // Finding all the input elements to the every label on a page
    List<WebElement> labels = driver.findElements(By.tagName("label"));
    List<WebElement> inputs = (List) ((JavascriptExecutor)driver).executeScript(
      "var labels = arguments[0], inputs = []; for (var i=0; i < labels.length; i++){" +
      "inputs.push(document.getElementById(labels[i].getAttribute('for'))); } return inputs;", labels);
    // check element exist or not, using findElements
    boolean isPresent = driver.findElements(By.yourLocator).size() > 0
  3. User Input - Filling In Forms

    WebElement select = driver.findElement(By.tagName("select"));
    List allOptions = select.findElements(By.tagName("option"));
    for (WebElement option : allOptions) {
      System.out.println(String.format("Value is: %s", option.getAttribute("value")));;
    Select select = new Select(driver.findElement(By.tagName("select")));
  4. Moving Between Windows and Frames

    // switch to window
    // how do you know the window's name
    for (String handle : driver.getWindowHandles()) {
    // switch to frame
    // store the current window handle
    String winHandleBefore = driver.getWindowHandle();
    // perform the click operation that opens new window
    // switch to new window opened
    for (String winHandle : driver.getWindowHandles()) {
    // perform the actions on new window
    // close the new window, if that window no more required
    // switch back to original browser (first window)
    // continue with original browser (first window)
    Window Util
    // use to wait for window popup until available
    public static void waitForNumberOfWindowsEqual(final WebDriver driver, final int numberOfWindows) {
      new WebDriverWait(driver, 10).until(new ExpectedCondition() {
        public Boolean apply(WebDriver arg0) {
          return driver.getWindowHandles().size() == numberOfWindows;
  5. Popup Dialogs

    Alert alert = driver.switchTo().alert();
  6. Navigation: History and Location

  7. Cookies

    // set the cookie, this one's valid for the entire domain
    Cookie cookie = new Cookie("key", "value");
    // output all the available cookies for the current URL
    Set allCookies = driver.manage().getCookies();
    for (Cookie loadedCookie : allCookies) {
      System.out.println(String.format("%s -> %s", loadedCookie.getName(), loadedCookie.getValue()));
    // delete cookies in 3 ways
    // by name
    // by Cookie
    // all of them
  8. Changing the User Agent

    FirefoxProfile profile = new FirefoxProfile();
    profile.addAdditionalPreference("general.useragent.override", "some UA string");
    WebDriver driver = new FirefoxDriver(profile);
  9. Drag And Drop

    WebElement element = driver.findElement("source"));
    WebElement target = driver.findElement("target"));
    (new Actions(driver)).dragAndDrop(element, target).perform();
  10. Explicit and Implicit Waits

    Explicit Waits

    WebDriver driver = new FirefoxDriver();
    WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated("myDynamicElement")));
    // Expected Conditions
    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement element = wait.until(ExpectedConditions.elementToBeClickable("someid")));

    Implicit Waits

    WebDriver driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    WebElement myDynamicElement = driver.findElement("myDynamicElement"));


Popular posts from this blog

Reduce TIME_WAIT Socket Connections