Selenium C# Tutorial: Using web browser command

Updated: Jan 18

When C# is used along with the Selenium testing suite, the IwebDriver interface performs and monitors actions on the web app under tests. To use the methods/properties of the IwebDriver interface, a driver object from IwebDriver should be created. This post will cover the main methods and properties related to the Web Browser session that is created before any test execution.

GoToURL Command

The driver uses this command to open a web session to a particular page "driver. Navigate().GoToUrl(Site Address);". The command takes a string value of the URL as an input parameter and returns a void.

IWebDriver chromedriver = new ChromeDriver();
string siteToAccess = "https://www.agilequalitymadeeasy.com/";
chromedriver.Navigate().GoToUrl(siteToAccess);


URL Command

Using this Selenium Webdriver command, you can Get or Set the URL/web page that needs to be displayed.

IWebDriver chromedriver = new ChromeDriver();
string siteToAccess = "https://www.agilequalitymadeeasy.com/";
chromedriver.Url(siteToAccess);



Title Command

The property IWebDriver.title{get; set} will return the title of the current page has a string.

IWebDriver chromedriver = new ChromeDriver();
string siteToAccess = "https://www.agilequalitymadeeasy.com/";
chromedriver.Navigate().GoToUrl(siteToAccess);

//Example 1: 
string pageTitle = chromedriver.Title;

//Example 2: 
Console.WriteLine(chromedriver.Title);

//Example 3:
if (chromedriver.Title == "Quality Assurance | AgileQualityMadeEasy")
{ Console.WriteLine("Test Pass"); }
else{ Console.WriteLine("Test Fail"); } 

Back Command

Back command is used to navigate to the previous page in the browser history, simulating the same operation as pressing the browser "Back" navigation button.

chromedriver.Navigate().Back();

Back Command

Back command is used to navigate to the previous page in the browser history, simulating the same operation as pressing the browser "Back" navigation button.

chromedriver.Navigate().Back();

Forward command

This method simulates the same operation as pressing the browser "forward" button in your browser (Note: Work when you have some history in the forwarding direction).

chromedriver.Navigate().Forward();



Refresh Command

This command refreshes the page as if you have pressed F5 on the browser.

chromedriver.Navigate().Refresh();

Close Command

This method is used to close a specific window the IWebDriver is currently controlling (Quit the entire session if it's the last window).

chromedriver.Close();

Quit Command

This method Closes all windows opened by the IWebDriver.

chromedriver.Quit();

Page Source command

You can get the page source loaded by the web browser by using the PageSource command.

chromedriver.Navigate().GoToUrl(siteToAccess);
string PageSource = chromedriver.PageSource;

How to delete the browser cookies?

chromedriver().Cookies.DeleteAllCookies ();

How to maximize a session window?

chromedriver.Manage().Window.Maximize ();
66 views0 comments