Selenium WebDriver - Using Web Browser Commands with C#

In this post, we will review a list of methods and properties that we will use to control and manage the application under tests.


How to open a new web session?


using NUnit.Framework;

using OpenQA.Selenium;

using OpenQA.Selenium.Chrome;

using OpenQA.Selenium.Firefox;

using System;

using System.Threading;


namespace SeleniumCSharpCourse2021

{

public class Tests

{

public IWebDriver Driver;

[SetUp]

public void Setup()

{

Driver = new FirefoxDriver();

Driver.Navigate().GoToUrl("https://demowf.aspnetawesome.com/");//To open the site

Thread.Sleep(3000);

}

}

}


How to validate the page Title?


public class Tests

{

public IWebDriver Driver;

[SetUp]

public void Setup()

{

Driver = new FirefoxDriver();

Driver.Navigate().GoToUrl("https://www.google.com/");//To open the site

Thread.Sleep(3000);

}


[Test]

public void Tiltle_Validation()

{

Assert.AreEqual("Google", Driver.Title);

}


How to validate the current URL?


public class Tests

{

public IWebDriver Driver;

[SetUp]

public void Setup()

{

Driver = new FirefoxDriver();

Driver.Navigate().GoToUrl("https://www.google.com/");//To open the site

Thread.Sleep(3000);

}


[Test]

public void URL_Validation()

{

Assert.IsTrue(Driver.Url.Contains("google.com"));

}

}


How to refresh the web page?


public void URL_Refresh()

{

IWebDriver Firefox = new FirefoxDriver();

Firefox.Navigate().GoToUrl("http://www.machtested.com/");

Firefox.Navigate().Refresh();

}


How to close the current web page Tab?


public void URL_Close()

{

IWebDriver Firefox = new FirefoxDriver();

Firefox.Navigate().GoToUrl("http://www.machtested.com/");

Firefox.Close();

}


How to quit and end the current session?


public void URL_Quit()

{

IWebDriver Firefox = new FirefoxDriver();

Firefox.Navigate().GoToUrl("http://www.machtested.com/");

Firefox.Quit();

}


How to Return back and forward

Firefox.Navigate ().Back ();

This method simulates the same operation as pressing on the browser "Back" navigation button (You will move back one page earlier in the browser)


Firefox.Navigate().Forward();

This method simulates the same operation as pressing on the browser "forward" navigation button (You will go forward one page)


Delete all cookies

Firefox.Manage().Cookies.DeleteAllCookies();

This function will remove all cookies from the selected web page


Get the URL source code

This code will get the URL source code and return a Boolean value for the assert command.


public void URL_Validation()

{

IWebDriver Firefox = new FirefoxDriver();

Firefox.Navigate().GoToUrl("Http://www.dtvisiontech.com");

Assert.IsTrue(Firefox.PageSource.StartsWith("StringA") || Firefox.PageSource.EndsWith("StringB") ;

}







26 views0 comments