Chapter 3. Selenium WebDriver Functions

Selenium WebDriver API provides an object-oriented approach to test web-based applications. On the one hand, Selenium RC (Remote Control) injects JavaScript into the browser on runtime, and on the other hand, Selenium WebDriver performs direct calls to the browser using each browser's native approach. Meanwhile, WebDriverBackedSelenium lets you combine both Selenium RC and WebDriver. Hopefully, Selenium RC will be deprecated and taken away in Selenium 3.0, which includes the Marionette driver replacing the Firefox driver to automate Firefox OS in mobile platforms. WebDriver API enriched Selenium RC is called Selenium WebDriver or Selenium 2.0. In the future, Selenium will meet all the W3C standards; however, the current Selenium WebDriver API encloses a bunch of functions used for effective web automation tests.

In this chapter, we will learn about the following topics:

  • Basic Selenium WebDriver functions
  • Locating WebElements
  • WebElement and Windows WebDriver functions
  • Navigation and Cookies WebDriver functions
  • Select functions
  • Handling alerts and pop-ups
  • Mouse and keyboard actions

Basic WebDriver functions

Consider a test use case with the user opening a browser, searching for a term, asserting the actual value with the expected value, and then finally exiting the browser. This simple use case can certainly be achieved using the basic Selenium WebDriver functions. The elementary functions, such as click(), close(), submit(), and sendKeys(), are the fundamental keys to start with any test automation tasks. Let's discuss the basic WebDriver functions in more detail:

  • close(): This function exits or closes the current active browser window. The following is the syntax for this function:
    driver.close();
  • quit(): This function halts the running driver and closes every browser window by ending the active session. The following is the syntax for this function:
    driver.quit();
  • getTitle(): This function fetches the current page title. The following is the syntax for this function:
    driver.getTitle();
  • getCurrentUrl(): This function gets the current web page URL. The following is the syntax for this function:
    driver.getCurrentUrl();
  • getPageSource(): This function retrieves the entire page source of the loaded web page and allows the user to assert any text present in the same web page. However, the modified DOM, due to asynchronous (Ajax) calls, is not reflected on some of the browsers. Instead, it returns the page source of the previously loaded web page. The getPageSource() method is not advisable for web pages loading JavaScripts asynchronously. The following is the syntax for this function:
    driver.getPageSource();

    Some of the helpful snippets using this function are given as follows:

    • Verify text:
      driver.getPageSource().contains("your text");
    • Assert text:
      boolean b = driver.getPageSource().contains("your text"); 
      System.out.println(b);
      assertTrue(b);
  • click(): This function lets you click on a link, button, checkbox, or radio button. The following is the syntax for this function:
    driver.findElement(By.locatorType("path")).click();

    The following is an example that covers the click() function to do a simple Google search on clicking the search button:

    driver.get("https://www.google.com");
    driver.findElement(By.name("q")).sendKeys("selenium essentials");
    driver.findElement(By.id("sblsbb")).click();
  • clear(): This function erases or empties the string values present in a text field. The following is the syntax for this function:
    driver.findElement(By.locatorType("path")).clear();

    The following code snippet is a simple Google search, where the clear() method is used to erase the current search keyword from the search text field and prepare for the next search:

    driver.get("https://www.google.com");
    driver.findElement(By.name("q")).sendKeys("selenium essentials");
    driver.findElement(By.name("q")).clear();
    driver.findElement(By.name("q")).sendKeys("prashanth sams");
  • sendKeys(): This function lets you type or insert text in the text field on runtime. The following is the syntax for this function:
    driver.findElement(By.locatorType("path")).sendKeys("your text");

    The following snippet explains the sendKeys() function that lets you insert text or a sentence in the Google search text field:

    driver.get("https://www.google.com");
    WebElement element = driver.findElement(By.name("q"));
    element.sendKeys("selenium essentials");
  • submit(): This function is similar to the click() function. However, this function is used to submit a form with <form> tags. The following is the syntax for this function:
    driver.findElement(By.locatorType("path")).submit();

    The submit() function is commonly used instead of pressing the Enter key. Let's see an example to retrieve results from the Google search:

    driver.get("https://www.google.com");
    WebElement element = driver.findElement(By.name("q"));
    element.sendKeys("selenium essentials");
    element.submit();
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.79.84