Thursday, December 22, 2016

Lesson #7 - Selenium WebDriver - Using Web Browser Commands with C#


Overview

As you probably know, the first thing that we need to do, prior to executing 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 tests.

תוצאת תמונה עבור ‪selenium commands‬‏

Selenium Browser Commands

How to open a new web session?


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

How to validate the page Title?

Very simple task, all we need to do is to use the “Title” IWebDriver property that returns a 'string' containing the web page title.

Example:
public void Tiltle_Validation()
        {
            IWebDriver Firefox = new FirefoxDriver();
            Firefox.Navigate().GoToUrl("Http://www.google.com");
            Assert.AreEqual("Google",Firefox.Title); //Case Sensitive           
        }

 

How to validate the current URL?

Sometimes we will need to validate that the web page URL is similar/Contains a specific syntax, the way to accomplish this task with C#, is to use the ‘String’ class methods as shown in the next code.

Example:

public void URL_Validation()
{
IWebDriver Firefox = new FirefoxDriver();           
Firefox.Navigate().GoToUrl("Http://www.machtested.com");               Assert.IsTrue(Firefox.Url.Contains("machtested.com"));          
}

 

How to refresh the web page?

In any case that your code execution needs to involve a web page refresh operation, the next code will help you to perform this task.

Example:
  
 public void URL_Refresh()
        {
            IWebDriver Firefox = new FirefoxDriver();           
            Firefox.Navigate().GoToUrl("http://www.machtested.com/");
            Firefox.Navigate().Refresh();       
        }

 

How to close a web page Tab?

Simple code that allows the user to close the current working URL

Example:
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 (All Tabs)?

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

 

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

Example:
Firefox.Navigate ().Back ();

 

Move forward

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

Example:
Firefox.Navigate().Forward();

 

Delete all cookies

This function will remove the entire cookies from the selected web page

Example:

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

 

Maximize window

This function will maximize the current window

Example:
Firefox.Manage().Window.Maximize();

 

Get the URL source code

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

Example:

 
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") ;           
        }

No comments:

Post a Comment

My Presentations