Wednesday, March 1, 2017

Selenium WebDriver - Using web browser command (C#)

תמונה קשורהAs you probably know, the first thing that we need to do before execute the test plan is to open a new URL session of the tested application.

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

Using the browser Navigate method

How to open a new web session?

To open a new session of the application, we will use the  ".GoToUrl" command.

Example:

[TestMethod]
public void HowToOpenANewWebSession()
{
IWebDriver Firefox = new FirefoxDriver();
Firefox.Navigate().GoToUrl("http://www.machtested.com/");
}

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).

Firefox.Navigate().Back();

How to use the forward button?

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

Firefox.Navigate().Forward();

How to refresh the current page?

Simulate the same task as pressing F5 to refresh the page

Firefox.Navigate().Refresh(); 

Using the browser global functions

How to close the current page (Specific TAB)?


Example:
public void URL_Close()
{
IWebDriver Firefox = new FirefoxDriver();
Firefox.Navigate().GoToUrl("Http://www.machtested.com");
Firefox.Close();
}


How to quit and ends the current session (All Tabs)?


Example:
public void URL_Quit()
{
IWebDriver Firefox = new FirefoxDriver();
Firefox.Navigate().GoToUrl("Http://www.machtested.com");
Firefox.Quit();
}

How to delete the browser cookies?

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

How to maximize a session window?

Firefo.Manage().Window.Maximize ();

How to validate the browser title?

Sometimes, we will need to verify the current location of the tests before continuing the test execution.

Example:
[TestMethod]
public void HowToOpenANewWebSession ()
{
IWebDriver Firefox = new FirefoxDriver ();
Firefox.Navigate().GoToUrl("http://www.machtested.com/");
Assert.AreEqual("David Tzemach's Blog", Firefox.Title); //Case Sensitive
}

How to validate the URL address?

Similar to the last example, but now we will do the validation on the page URL.

Example:


How to get the URL source code?


Example:

[TestMethod]
public void HowToGetThePageSource()
{
IWebDriver Firefox = new FirefoxDriver();
Firefox.Navigate().GoToUrl("http://www.machtested.com/");
Firefox.PageSource.ToString();
}

No comments:

Post a Comment

My Presentations