It's possible to navigate the browser directly, like using the standard toolbar commands available on all browsers:
You can create a navigation object by calling
Navigate() on the driver:
IWebDriver driver INavigation navigation = driver.Navigate();
A navigation object allows you to perform numerous actions that navigate the browser around the web:
//like pressing the back button navigation.Back(); //like pressing the forward button on a browser navigation.Forward(); //navigate to a new url in the current window navigation.GoToUrl("www.stackoverflow.com"); //Like pressing the reload button navigation.Refresh();
For Navigate to any url :
For move backwards :
For move forwards :
For refresh the page :
WebDriver, The main interface to use for testing, which represents an idealised web browser. The methods in this class fall into three categories:
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");
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.
//Getting current url loaded in browser & comparing with expected url String pageURL = driver.getCurrentUrl(); Assert.assertEquals(pageURL, "https://www.google.com");
It returns the title of the current page, with leading and trailing whitespace stripped, or null if one is not already set.
//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 current page source String pageSource = driver.getPageSource();
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.
//Close the current window driver.close();
Quits this driver, closing every associated window. After calling this method we can not use any other method using same driver instance.
//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.