Book Image

Selenium Testing Tools Cookbook

By : UNMESH GUNDECHA
5 (1)
Book Image

Selenium Testing Tools Cookbook

5 (1)
By: UNMESH GUNDECHA

Overview of this book

This book is an incremental guide that will help you learn and use the advanced features of the Selenium toolset including the WebDriver API in various situations to build a reliable test automation. You start off by setting up the test development environment and gain tips on the advanced locater strategy and the effective use of the Selenium WebDriver API. After that, the use of design patterns such as data - driven tests and PageFactory are demonstrated. You will then be familiarised with extending Selenium WebDriver API by implementing custom tasks and setting up your own distributed environment to run tests in parallel for cross-browser testing. Finally, we give you some tips on integrating Selenium WebDriver with other popular tools and testing mobile applications. By the end of this book, you will have learned enough to solve complex testing issues on your own.
Table of Contents (21 chapters)
Selenium Testing Tools Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Setting up Microsoft WebDriver for Microsoft Edge


Microsoft Edge is a new web browser launched with Microsoft Windows 10. Microsoft Edge implements the W3C WebDriver standard and provides in-built support for Selenium WebDriver.

Similar to Internet Explorer, in order to execute test scripts on the Microsoft Edge browser, we need to use EdgeDriver class and a standalone Microsoft WebDriver Server executable.

Microsoft WebDriver Server is maintained by the Microsoft Edge development team. You can find more information at https://msdn.microsoft.com/en-us/library/mt188085(v=vs.85).aspx.

Let's set up Microsoft WebDriver Server and create a test for testing the search feature on Microsoft Edge.

Getting ready

You need to download and install Microsoft WebDriver Server on Windows 10 from https://www.microsoft.com/en-us/download/details.aspx?id=48212.

How to do it...

Add a new test and name it as GoogleSearchTestOnEdge.java and add the following code:

package com.secookbook.examples.chapter01;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class GoogleSearchTestOnEdge {

  private WebDriver driver;

  @Before
  public void setUp() {
    System.setProperty("webdriver.edge.driver",
        "C:\\Program Files (x86)\\Microsoft Web Driver\\MicrosoftWebDriver.exe");

    EdgeOptions options = new EdgeOptions();
    options.setPageLoadStrategy("eager");

    // Launch a new Edge instance
    driver = new EdgeDriver(options);

    // Navigate to Google
    driver.get("http://www.google.com");
  }

  @Test
  public void testGoogleSearch() {
    // Find the text input element by its name
    WebElement element = driver.findElement(By.name("q"));

    // Clear the existing text value
    element.clear();

    // Enter something to search for
    element.sendKeys("Selenium testing tools cookbook");

    WebElement button = driver.findElement(By.name("btnG"));
    button.click();

    // Google's search is rendered dynamically with JavaScript.
    // Wait for the page to load, timeout after 10 seconds
    new WebDriverWait(driver, 10).until(new ExpectedCondition<Boolean>() {
      public Boolean apply(WebDriver d) {
        return d.getTitle().toLowerCase()
            .startsWith("selenium testing tools cookbook");
      }
    });

    assertEquals("Selenium testing tools cookbook - Google Search",
        driver.getTitle());
  }
  @After
  public void tearDown() throws Exception {
    // Close the browser
    driver.quit();
  }
}

Execute this test and you will see a Microsoft Edge window being launched and all the steps executed.

How it works...

Microsoft WebDriver Server is a standalone server executable that implements WebDriver's JSON-wire protocol, that works as a glue between the test script and the Microsoft Edge browser, as shown in the following diagram:

The tests should specify the path of Microsoft WebDriver Server executable before creating the instance of Microsoft Edge. This is done by setting the webdriver.edge.driver property as shown in the following code:

System.setProperty("webdriver.edge.driver",
        "C:\\Program Files (x86)\\Microsoft Web Driver\\MicrosoftWebDriver.exe");

Tip

We can also specify a path externally through the –Dwebdriver.edge.driver option using the Maven command line options. In this case, we don't need to set up this property in test.