19.2 WebDriver browser methods

19.2.1| getPageSource

Returns the page source of the last loaded page. The page source returned is a representation of the underlying DOM. It may not be formatted or escaped in the same way as the response sent from the server.

                                                                                                                                                           
            Syntex               java.lang.String getPageSource
      parameters            
      returns              The page source of the current page

                                                                                                                                                       

Write a code to open testinganswers.com and retrieve its page source

getPageSource in selenium


Note: If the page is modified after page reload (e.g. page refresh after selecting some list box option), then there is no guarantee that the returned page source text is of the reloaded page. WebDriver documentation can be referenced to know the exact behavior of the specific driver.


19.2.2| navigate

Simulates browser back, forward, refresh and navigate to options.

                                                                                                                                                           
            Syntex               WebDriver.Navigation navigate()
      parameters            
      returns              A WebDriver.Navigation that allows the 
                           selection of what to do next

                                                                                                                                                       

This method allows WebDriver test script to simulate user interactions with browsers such as navigating to specific URL, clicking on browser back or forward button, clicking on browser refresh button, etc. 


Simulates browser back, forward, refresh and navigate to options.




























19.2.3| getWindowHandles

Returns all browser windows of the WebDriver instance.

                                                                                                                                                         
            Syntex               java.util.Set<java.lang.String>
      parameters            
      returns              A set of window handles which can be used
                           to iterate over all the open windows.

                                                                                                                                                       

Suppose the user opens testinganswers.com and then clicks on the link available on the site in another/ new window. So, see how to handle all windows and perform an action on all the windows (Driver instances) in Selenium.


getWindowHandles in Selenium

19.2.4| getWindowHandle

Returns the currently active browser window of the WebDriver instance.

                                                                                                                                                         
            Syntex               java.lang.String getWindowHandle()
      parameters            
      returns              Returns the currently active window
                           handle. 

                                                                                                                                                       

Suppose the user opens testinganswers.com and then clicks on the link available on the site in another/ new window. So, see how to handle both windows and perform an action on both windows (Driver instances) in Selenium.

getWindowHandle in Selenium


19.1 WebDriver browser methods

WebDriver provides a variety of methods to simulate user actions on a browser. Some of those methods are described below 

19.1.1| get


Loads a new page in the current browser window. This is achieved using HTTP GET operation. This method blocks the WebDriver test execution until the page load is complete.


                                                                                                                                                             
            Syntex               get (java.lang.String URL)
      parameter            URL   - URL of the site to navigate to
      returns              void
                                                                                                                                                       


Write WebDriver code to open the Chrome browser and navigate to the https://www.testinganswers.com site

get



19.1.2| getCurrentUrl

Returns the current URL of the browser page

                                                                                                                                                           
            Syntex         java.lang.String getCurrentUrl
      parameter            
      returns        URL of the page currently loaded in the 
                     browser
                                                                                                                                                       


Write WebDriver code to open the Chrome browser, navigate to the https://www.testinganswers.com site and then verify that the current page is testinganswers.com or not.


getCurrentUrl


19.1.3| getTitle

Returns the title of the current web page

                                                                                                                                                          
            Syntex         java.lang.String getTitle
      parameter            
      returns        the title of the current web page with 
                     leading and trailing whitespace stripped or
                     null if one is not already set.
                                                                                                                                                       


Write WebDriver code to open the Chrome browser, navigate to the https://www.testinganswers.com site and then verify that the current page title is correct or not.


getTitle


19.1.4| findElements


Finds all the matched elements on the current page. This method continuously tries to identify the elements till zero or more elements are found or timeout time is reached, whichever happens first.

                                                                                                                                                          
            Syntex         java.util.List<WebElement> findElements(By by)
      parameter      by - the locating mechanism to use      
      returns        the list of all matched elements or an empty
                     list if no match is found.
                                                                                                                                                       

Find all-day elements of a calendar.


Find all-day elements of a calendar.










19.1.5| findElement


Finds the first matched web element on the current page. This method continuously tries to identify the elements until the element is found or timeout time is reached, whichever happens first. If no matched element is found until timeout time, then it returns an exception. This method implicitly waits to identify the element until the configured timeout is reached.

                                                                                                                                                          
            Syntex         WebElement findElement(By by)
      parameter      by - the locating mechanism to use      
      returns        the first matched elements else throws 
                     NoSuchElementException if no matched element
                     found
                                                                                                                                                       

Select an element from the calendar year drop-down.


18 Working with Browsers

A web browser is a software application that allows users to retrieve, present and traverse information resources on the World Wide Web (www). An information resource is identified by a Uniform Resource Identifier (URI). The information may be a web page, image, audio, video or any other content. There are various browsers available in the market as of now. These include internet explorer, chrome, firefox, safari, opera, etc. This chapter describes how to use the WebDriver method to simulate user actions on a firefox browser.

18.1.1| Introduction

  • Open Firefox Browser: WebDriver provided FirefoxDriver() class to open a new Firefox browser session. The following code shows how to create an instance of FirefoxDriver() and open the www.testinganswers.com site.
Open Firefox Browser

  • Open Chrome Browser: WebDriver provided ChromeDriver() class to open a new chrome browser session. The following code shows how to create an instance of ChromeDriver() and open the www.testinganswers.com site.
Open Chrome Browser

  • Open Internet Explorer Browser: WebDriver provided InternetExplorerDriver() class to open a new chrome browser session. The following code shows how to create an instance of InternetExplorerDriver() and open the www.testinganswers.com site.
Open Internet Explorer Browser


18.1.2| Choose a browser at run time

In Selenium WebDriver, we can automate test cases using Internet Explorer, Firefox, Chrome, etc browsers. Especially, in Cross Browser Testing. Cross Browser Testing is a type of functional test to check that your web application works as expected in different browsers. So, let's see, how to choose a browser at run time in Selenium WebDriver in below code piece

Choose a browser at run time