Saturday, November 7, 2015

Browser methods in WebDriver

WebDriver, The main interface to use for testing, which represents an idealised web browser. The methods in this class fall into three categories:

  1. Control of the browser itself
  2. Selection of WebElements
  3. Debugging aids

Key methods are get(String), which is used to load a new web page, and the various methods similar to findElement(By), which is used to find WebElements. In this post we are going to learn browser controlling methods.

get

void get(java.lang.String url)

Load a new web page in the current browser window. This is done using an HTTP GET operation, and the method will block until the load is complete. it is best to wait until this timeout is over, since should the underlying page change whilst your test is executing the results of future calls against this interface will be against the freshly loaded page.

      Usage

//Initialising driver
 WebDriver driver = new FirefoxDriver();
 
 //setting timeout for page load
 driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
 
 //Call Url in get method
 driver.get("https://www.google.com");
 //or
 driver.get("https://seleniumhq.org");

getCurrentUrl

java.lang.String getCurrentUrl()

Get a string representing the current URL that the browser is looking at. It returns the URL of the page currently loaded in the browser.

Usage

//Getting current url loaded in browser & comparing with expected url
 String pageURL = driver.getCurrentUrl();
 Assert.assertEquals(pageURL, "https://www.google.com");

getTitle

java.lang.String getTitle()

It returns the title of the current page, with leading and trailing whitespace stripped, or null if one is not already set.

Usage

//Getting current page title loaded in browser & comparing with expected title
 String pageTitle = driver.getTitle();
 Assert.assertEquals(pageTitle, "Google");

getPageSource

java.lang.String getPageSource()

Get the source of the last loaded page. If the page has been modified after loading (for example, by Javascript) there is no guarantee that the returned text is that of the modified page.

Usage

//get the current page source
     String pageSource = driver.getPageSource();

close

void close()

Close the current window, quitting the browser if it's the last window currently open. If there are more than one window opened with that driver instance this method will close the window which is having current focus on it.

Usage

//Close the current window
     driver.close();

quit


void quit()

Quits this driver, closing every associated window. After calling this method we can not use any other method using same driver instance.

Usage

//Quit the current driver session / close all windows associated with driver
     driver.quit();


These are all very useful methods available in Selenium 2.0 to control browser as required. 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.