Selenium C# Tutorial: Using web browser command
Updated: Mar 5
In this article, we will review a list of methods and properties that we will use to control and manage the Web Application under test.
How to open a new web session
To open a new session of the Web Application, we will use the ".GoToUrl" command.
//Open Web Session
public static void openWebSession(IWebDriver driver, string url)
{
driver.Navigate().GoToUrl(url);
}
How to use the Back method
The "Back" method simulates the same operation as pressing on the browser "Back" navigation button (You will go forward one page earlier in the browser).
//Back Command
public static void BackCommand(IWebDriver driver)
{
driver.Navigate().Back();
}
How to use the forward method
The "Forward "method simulates the same operation as pressing on the browser "forward" navigation button (You will go forward one page).
//Forward Command
public static void ForwardCommand(IWebDriver driver)
{
driver.Navigate().Forward();
}
How to refresh the current page
Simulate the same task as pressing F5 to refresh the pageڄ
//Refresh the current page
public static void RefreshCommand(IWebDriver driver)
{
driver.Navigate().Refresh();
}
How to close the current page (Specific TAB)
The close command is used to close a specific TAB of the web application.
//Closing current TAB
public static void CloaseTabCommand(IWebDriver driver)
{
driver.Close();
}
How to quit and ends the current session (All Tabs)
The quit command will close all TABS which ends the session.
//Closing Browser session
public static void EndSession(IWebDriver driver)
{
driver.Quit();
}
How to clear the browser cookies?
In some cases, you will need to remove all history/information saved in the browser cookie files. To do it, we can use the following code:
//Clear Browser Cookies
public static void ClearCookies(IWebDriver driver)
{
driver.Manage().Cookies.DeleteAllCookies();
}
How to Maximize a session window
//Maximize a session window
public static void MaximizeWindow(IWebDriver driver)
{
driver.Manage().Window.Maximize();
}
How to validate the browser title
//Validate the browser title
public static void ValidateBrowserTitle(IWebDriver driver)
{
Assert.AreEqual("David Tzemach's Blog", driver.Title); //Case Sensitive
}
How to validate the URL address
//Validate the browser URL
public static void ValidateBrowserURL(IWebDriver driver)
{
Assert.IsTrue(driver.Url.Equals("URL To Test")); //Case Sensitive
}
How to import the Page Source
//Locating Page Source
public static void HowToGetThePageSource(IWebDriver driver)
{
driver.PageSource.ToString();
}