Part 5 – Creating Automated Test Script

In this part of the Test Automation with Selenium WebDriver documentation, we are going to create more complex script that interacts with different pages and elements of our Guestbook Demo Web Application. Please refer to Application Under Test and Test Plan for more details.

In the previous post, we learned that Test Automation tool recognizes objects based on their available properties/values.

For the purpose of this reference all the fields and other page elements in Guestbook Demo web application, have unique IDs so it’s easy to recognize objects and differentiate one from another. That’s not always going to be the case but for the purposes of this reference, we are going to simplify the process by having unique IDs for each object.

Open your Visual Studio program created in the previous parts of these documentation and further modify your program as shown below.

Automated Test Script for Guestbook Demo Web Application

Step 1 – Open Guestbook Demo Home Page, click the ‘Sign The Guestbook’ button – Login Page opens

//navigate to Guestbook Web Application URL
driver.Url = "http://aut.testautomationpro.com/"; 

//click Sign The Guestbook button
var btnSignTheGuestbook = driver.FindElement(By.Id("SignTheGuestbookButton"));
btnSignTheGuestbook.Click();

Step 2 – Enter Username and Password and press ‘Submit’ button – Guestbook Form opens

//Login
var txtUsername = driver.FindElement(By.Id("txt_username"));
txtUsername.SendKeys("Demouser"); //enter Username

var txtPassword = driver.FindElement(By.Id("txt_password"));
txtPassword.SendKeys("Demopass"); //enter Password

var btnSubmit = driver.FindElement(By.Id("btn_submit"));
btnSubmit.Click(); //press submit button

Step 3 – Fill out the form and press ‘Submit’ button – Confirmation Page opens

//enter full name
var txtName = driver.FindElement(By.Id("txt_name"));
txtName.Clear();
txtName.SendKeys("Jane Smith");

//enter email address
var txtEmail = driver.FindElement(By.Id("txt_email"));
txtEmail.Clear();
txtEmail.SendKeys("[email protected]");

//check checkbox
var chkSubscribe = driver.FindElement(By.Id("chk_subscribe"));
chkSubscribe.Click();

//select gender radio button
var rdbGender = driver.FindElement(By.Id("rdb_genderFemale"));
rdbGender.Click();

//select favorite tool option from drop down list
var cmbFavtool = driver.FindElement(By.Id("cmb_favtool"));
var selectElement = new SelectElement(cmbFavtool);
selectElement.SelectByText("Selenium WebDriver");

//enter comment
var txtComment = driver.FindElement(By.Id("txt_comment"));
txtComment.Clear();
txtComment.SendKeys("Test Automation is awesome!");

//press submit button
var btnSubmitForm = driver.FindElement(By.Id("btn_submit"));
btnSubmitForm.Click();

Step 4 – Verify the data displayed matches the data entered and Logout – Login Page opens

//verify data submitted matches the data displayed

var VerifyTitleName = driver.FindElement(By.Id("titlename")).Text;
var strFullName = "Jane Smith";
//name is in uppercase on the page
Assert.AreEqual(VerifyTitleName, strFullName.ToUpper()); 

var VerifyName = driver.FindElement(By.Id("name")).Text;

var VerifyEmail = driver.FindElement(By.Id("email")).Text;
Assert.AreEqual("[email protected]", VerifyEmail);

var VerifyGender = driver.FindElement(By.Id("gender")).Text;
Assert.AreEqual("female", VerifyGender);

var VerifySubscribe = driver.FindElement(By.Id("subscribe")).Text;
Assert.AreEqual("Yes", VerifySubscribe);

var VerifyComment = driver.FindElement(By.Id("comment")).Text;
Assert.AreEqual(VerifyComment, "Test Automation is awesome!");

//logout
driver.FindElement(By.Id("LogoutMenuItem")).Click();

Step 5 – Navigate to Guestbook section on the Home Page – Home Page opens

//navigate to home page
driver.FindElement(By.Id("GuestbookMenuItem")).Click();

Step 6 – Verify the latest entry

//verify the latest entry 
var VerifyRecentPost = driver.FindElement(By.Id("postid1")).Text;
Assert.AreEqual("Jane Smith", VerifyRecentPost);

Full Program

Additional references

  • Right click on ‘References’ in the Solution Explorer, select ‘Add References’
  • In Reference Manager search by keyword ‘test’
  • Select Microsoft.VisualStudio.QualityTools.UnitTestFramework

Additional Name Spaces

using System.Globalization; //to enable ToUpper()
using Microsoft.VisualStudio.TestTools.UnitTesting; //to enable Assert

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Globalization; //to enable ToUpper()
using System.Text.RegularExpressions;

using Microsoft.VisualStudio.TestTools.UnitTesting; //to enable Assert

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

namespace Guestbook
{
class Program
  {
static void Main(string[] args)
    {
	//create new instance of Firefox driver
	IWebDriver driver = new ChomeDriver();

	//navigate to Guestbook Web Application URL
	driver.Url = "http://aut.testautomationpro.com/";

	//click Sign The Guestbook button
	var btnSignTheGuestbook = driver.FindElement(By.Id("SignTheGuestbookButton"));
	btnSignTheGuestbook.Click();

	//Login
	var txtUsername = driver.FindElement(By.Id("txt_username"));
	txtUsername.SendKeys("Demouser");//enter Username

	var txtPassword = driver.FindElement(By.Id("txt_password"));
	txtPassword.SendKeys("Demopass"); //enter Password

	var btnSubmit = driver.FindElement(By.Id("btn_submit"));
	btnSubmit.Click();//press submit button

	//fill out the Guestbook form

	//enter name
	var txtName = driver.FindElement(By.Id("txt_name"));
	txtName.Clear();
	txtName.SendKeys("Jane Smith");

	//enter email address
	var txtEmail = driver.FindElement(By.Id("txt_email"));
	txtEmail.Clear();
	txtEmail.SendKeys("[email protected]");

	//check checkbox
	var chkSubscribe = driver.FindElement(By.Id("chk_subscribe"));
	chkSubscribe.Click();

	//select gender radio button
	var rdbGender = driver.FindElement(By.Id("rdb_genderFemale"));
	rdbGender.Click();

	//select favorite tool option from drop down list
	var cmbFavtool = driver.FindElement(By.Id("cmb_favtool"));
	var selectElement = new SelectElement(cmbFavtool);
	selectElement.SelectByText("Selenium WebDriver");

	//enter comment
	var txtComment = driver.FindElement(By.Id("txt_comment"));
	txtComment.Clear();
	txtComment.SendKeys("Test Automation is awesome!");

	//press submit button
	var btnSubmitForm = driver.FindElement(By.Id("btn_submit"));
	btnSubmitForm.Click();

	//verify data submitted matches the data displayed

	var VerifyTitleName = driver.FindElement(By.Id("titlename")).Text;
	var strFullName = "Jane Smith";
	//name is in uppercase on the page
	Assert.AreEqual(VerifyTitleName, strFullName.ToUpper());

	var VerifyName = driver.FindElement(By.Id("name")).Text;

	var VerifyEmail = driver.FindElement(By.Id("email")).Text;
	Assert.AreEqual("[email protected]", VerifyEmail);

	var VerifyGender = driver.FindElement(By.Id("gender")).Text;
	Assert.AreEqual("female", VerifyGender);

	var VerifySubscribe = driver.FindElement(By.Id("subscribe")).Text;
	Assert.AreEqual("Yes", VerifySubscribe);

	var VerifyComment = driver.FindElement(By.Id("comment")).Text;
	Assert.AreEqual(VerifyComment, "Test Automation is awesome!");

	//logout
	driver.FindElement(By.Id("LogoutMenuItem")).Click();

	//navigate to home page
	driver.FindElement(By.Id("GuestbookMenuItem")).Click();

	//verify the latest entry 
	var VerifyRecentPost = driver.FindElement(By.Id("postid1")).Text;
	Assert.AreEqual("Jane Smith", VerifyRecentPost);

	driver.Quit();
    }
  }
}

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.