I build my code with the POM approach to demonstrate how can
you build a simple and readable code in your own projects, pay attention to the
importance and relevance of each class that I describe.
Class 1: Object Mapping
class ObjectMapphing
{
//Creating
a general Element for all functions
public static IWebElement General_Element_for_All_Functions = null;
//Login
page - Title
public static IWebElement Login_Page_Title(IWebDriver FirefoxDriver)
{
General_Element_for_All_Functions =
FirefoxDriver.FindElement(By.XPath("/html/body/h1"));
return General_Element_for_All_Functions;
}
//The
User Name Field
public static IWebElement UserNameField(IWebDriver FirefoxDriver)
{
General_Element_for_All_Functions =
FirefoxDriver.FindElement(By.Name("userid"));
return General_Element_for_All_Functions;
}
//The
Password field
public static IWebElement PasswordField(IWebDriver FirefoxDriver)
{
General_Element_for_All_Functions =
FirefoxDriver.FindElement(By.Name("pswrd"));
return General_Element_for_All_Functions;
}
//The
Login Button
public static IWebElement LoginButton(IWebDriver FirefoxDriver)
{
General_Element_for_All_Functions =
FirefoxDriver.FindElement(By.XPath("/html/body/form/input[3]"));
return General_Element_for_All_Functions;
}
//The
Cancel Button
public static IWebElement CancelButton(IWebDriver FirefoxDriver)
{
General_Element_for_All_Functions =
FirefoxDriver.FindElement(By.Name("pswrd"));
return General_Element_for_All_Functions;
}
//Blog
Main Title(Used in the verification scenario)
public static IWebElement Main_Title_Verification(IWebDriver FirefoxDriver)
{
General_Element_for_All_Functions =
FirefoxDriver.FindElement(By.XPath("//h1[@class='title']"));
return General_Element_for_All_Functions;
}
//Will
be used to handle the failed login attempt
public static string JavaScriptValidation(IWebDriver FirefoxDriver , String ValidationString)
{
IAlert alert = FirefoxDriver.SwitchTo().Alert();
string PopUpText = alert.Text;
return PopUpText;
}
}
Class 2: Debugg Class
class DebuggClass
{
//Print
the test results into a .TXT file(V.1)
public static void PrintToFile(String FileInput , String Expected)
{
using (FileStream fs = new FileStream(@"d:\Debugg.txt", FileMode.Append, FileAccess.Write))
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine("---------------------------------------------------------------------" + Environment.NewLine + FileInput + Expected + Environment.NewLine + "---------------------------------------------------------------------" + Environment.NewLine);
}
}
//Print
the test results into a .TXT file(V.2)
public static void PrintToFileV2(String FileInput)
{
using (FileStream fs = new FileStream(@"d:\Debugg.txt", FileMode.Append, FileAccess.Write))
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine( FileInput+Environment.NewLine);
}
}
//Open
the file when the execution ended
public static void OpenFile()
{
System.Diagnostics.Process.Start(@"d:\Debugg.txt");
}
//Print
the Execution Start/End time
public static void Start_And_End_of_the_Execution(int StartOrEnd)
{
switch (StartOrEnd)
{
//The
START time of the test execution
case 0:
PrintToFileV2("---------------------------------------------------------------------");
PrintToFileV2("Test Execition is started | Start Time : "+ DateTime.Now.ToString());
PrintToFileV2("---------------------------------------------------------------------");
break;
//The END time of the test execution
case 1:
PrintToFileV2("---------------------------------------------------------------------");
PrintToFileV2("Test Execition is Ended |
End Time" + DateTime.Now.ToString());
PrintToFileV2("---------------------------------------------------------------------");
break;
default:
break;
}
}
Class 3: The Main class
[TestClass]
public class UnitTest1
{
IWebDriver FirefoxInstance;
[TestInitialize]
public void TestSetup()
{
FirefoxInstance = new FirefoxDriver();
FirefoxInstance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(150));
FirefoxInstance.Navigate().GoToUrl("file:///C:/Users/D/Desktop/d.html");
}
[TestCleanup]
public void CloseTest()
{
FirefoxInstance.Quit();
}
[TestMethod]
public void WebPageData()
{
DebuggClass.Start_And_End_of_the_Execution(0); //0 - Starting the tests , 1- Ending the test
DebuggClass.PrintToFile("Test
Name: Web Page Data" + Environment.NewLine + "Tab Name: " +
FirefoxInstance.Title + Environment.NewLine, "Url Name:
" + FirefoxInstance.Url);
}
[TestMethod]
public void TitleValidation()
{
if (ObjectMapphing.Login_Page_Title(FirefoxInstance).Text.Equals("Login page") == true)
{
DebuggClass.PrintToFile("Test
Name: Title Validation" + Environment.NewLine + "Status = Pass" +
Environment.NewLine
+"Exp: Login page" + Environment.NewLine + "Actual:", ObjectMapphing.Login_Page_Title(FirefoxInstance).Text);
}
else
{
DebuggClass.PrintToFile("Test
Name: Title Validation" + Environment.NewLine + "Status = Failed" +
Environment.NewLine
+ "Exp: Login page" + Environment.NewLine + "Actual:", ObjectMapphing.Login_Page_Title(FirefoxInstance).Text);
}
}
[TestMethod]
public void Validate_Objects_In_Page()
{
if (ObjectMapphing.UserNameField(FirefoxInstance).Displayed == true && ObjectMapphing.UserNameField(FirefoxInstance).Enabled
== true)
{
DebuggClass.PrintToFile("Test
Name: Validate Page Objects (User Name Field)" + Environment.NewLine + "Status =
Pass" + Environment.NewLine + "Exp: Object
is Available" + Environment.NewLine,"");
}
else
{
DebuggClass.PrintToFile("Test
Name: Validate Page Objects (User Name Field)" + Environment.NewLine + "Status =
Failed" + Environment.NewLine + "Exp: Object is Available" + Environment.NewLine, "Actual:The
Object Is Not Available");
}
if (ObjectMapphing.PasswordField(FirefoxInstance).Displayed == true && ObjectMapphing.PasswordField(FirefoxInstance).Enabled
== true)
{
DebuggClass.PrintToFile("Test
Name: Validate Page Objects (Password Field)" + Environment.NewLine + "Status =
Pass" + Environment.NewLine + "Exp: Object
is Available" + Environment.NewLine, "");
}
else
{
DebuggClass.PrintToFile("Test
Name: Validate Page Objects (Password Field)" + Environment.NewLine + "Status =
Failed" + Environment.NewLine + "Exp: Object is Available" + Environment.NewLine, "Actual:The
Object Is Not Available");
}
if (ObjectMapphing.LoginButton(FirefoxInstance).Displayed == true && ObjectMapphing.LoginButton(FirefoxInstance).Enabled)
{
DebuggClass.PrintToFile("Test
Name: Validate Page Objects (Log in Button)" +
Environment.NewLine
+ "Status = Pass" + Environment.NewLine + "Exp: Object
is Available" + Environment.NewLine, "");
}
else
{
DebuggClass.PrintToFile("Test
Name: Validate Page Objects (Log in Button)" +
Environment.NewLine
+ "Status = Failed" + Environment.NewLine + "Exp: Object
is Available" + Environment.NewLine, "Actual:The Object Is Not Available");
}
if (ObjectMapphing.CancelButton(FirefoxInstance).Displayed == true && ObjectMapphing.CancelButton(FirefoxInstance).Enabled)
{
DebuggClass.PrintToFile("Test
Name: Validate Page Objects (Cancel Button)" +
Environment.NewLine
+ "Status = Pass" + Environment.NewLine + "Exp: Object
is Available" + Environment.NewLine, "");
}
else
{
DebuggClass.PrintToFile("Test
Name: Validate Page Objects (Cancel Button)" +
Environment.NewLine
+ "Status = Failed" + Environment.NewLine + "Exp: Object
is Available" + Environment.NewLine, "Actual:The Object Is Not Available");
}
}
[TestMethod]
public void Validate_Log_In_Failed_attempt()
{
ObjectMapphing.UserNameField(FirefoxInstance).SendKeys("Invalid User Name");
ObjectMapphing.PasswordField(FirefoxInstance).SendKeys("Invalid User Password");
ObjectMapphing.LoginButton(FirefoxInstance).Click();
ObjectMapphing.JavaScriptValidation(FirefoxInstance,"");
if (ObjectMapphing.JavaScriptValidation(FirefoxInstance, "") == "Error Password or Username")
{
DebuggClass.PrintToFile("Test
Name: Validate Log In Failed attempt" + Environment.NewLine + "Status = Pass" +
Environment.NewLine
+ "Exp: Error Password or
Username" + Environment.NewLine + "Actual:", ObjectMapphing.JavaScriptValidation(FirefoxInstance,
""));
}
else
{
DebuggClass.PrintToFile("Test
Name: Validate Log In Failed attempt" + Environment.NewLine + "Status = Failed" +
Environment.NewLine
+ "Exp: Error Password or
Username" + Environment.NewLine + "Actual:", ObjectMapphing.JavaScriptValidation(FirefoxInstance,
""));
}
//Option
2 :
//Assert.AreEqual(ObjectMapphing.JavaScriptValidation(FirefoxInstance,
""), "Error Password or Username");
}
[TestMethod]
public void Validate_Log_In_Success_attempt()
{
// Store
the current window handle
string primaryWindowHandle = FirefoxInstance.CurrentWindowHandle;
//Perform
the click operation that opens new window
ObjectMapphing.UserNameField(FirefoxInstance).SendKeys("David");
ObjectMapphing.PasswordField(FirefoxInstance).SendKeys("Tzemach");
ObjectMapphing.LoginButton(FirefoxInstance).Click();
//New
Array that host the new handle ID
IList<string> OpenWindows = FirefoxInstance.WindowHandles;
//Switch
to the new opned window
FirefoxInstance.SwitchTo().Window(OpenWindows[1].ToString());
//Validate
the Log-in attempt
if ((ObjectMapphing.Main_Title_Verification(FirefoxInstance).Text) == "David Tzemach - Technical Vision")
{
DebuggClass.PrintToFile("Test
Name: Validate Log In Success attempt" + Environment.NewLine + "Status = Pass" +
Environment.NewLine
+ "Exp: David Tzemach - Technical
Vision" + Environment.NewLine + "Actual:", ObjectMapphing.Main_Title_Verification(FirefoxInstance).Text);
//Close
the current window
FirefoxInstance.Close();
//Return
to main Window
FirefoxInstance.SwitchTo().Window(primaryWindowHandle);
}
else
{
DebuggClass.PrintToFile("Test
Name: Validate Log In Success attempt" + Environment.NewLine + "Status = Failed" +
Environment.NewLine
+ "Exp: Login page" + Environment.NewLine + "Actual:", "Failure in
validation");
}
//
DebuggClass.Start_And_End_of_the_Execution(1);
DebuggClass.OpenFile();
}
private void LoadPage(int NumberOfSeconds , string Url)
{
FirefoxInstance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(NumberOfSeconds));
FirefoxInstance.Navigate().GoToUrl(Url);
}
}
}
Test Results:
---------------------------------------------------------------------
--------------------------------------------------------------------
Test
Execition is started | Start Time :
07/08/2015 10:59:49
---------------------------------------------------------------------
---------------------------------------------------------------------
Test Name: Web Page Data
Tab Name: Authentication Tests
Url Name: file:///C:/Users/D/Desktop/d.html
---------------------------------------------------------------------
---------------------------------------------------------------------
Test Name: Title Validation
Status = Pass
Exp: Login page
Actual:Login page
---------------------------------------------------------------------
---------------------------------------------------------------------
Test Name: Validate Page Objects (User Name Field)
Status = Pass
Exp: Object is Available
---------------------------------------------------------------------
--------------------------------------------------------------------
Test Name: Validate Page Objects (Password Field)
Status = Pass
Exp: Object is Available
---------------------------------------------------------------------
---------------------------------------------------------------------
Test Name: Validate Page Objects (Log in Button)
Status = Pass
Exp: Object is Available
---------------------------------------------------------------------
---------------------------------------------------------------------
Test Name: Validate Page Objects (Cancel Button)
Status = Pass
Exp: Object is Available
---------------------------------------------------------------------
---------------------------------------------------------------------
Test Name: Validate Log In Failed attempt
Status = Pass
Exp: Error Password or Username
Actual:Error Password or Username
---------------------------------------------------------------------
---------------------------------------------------------------------
est Name: Validate Log In Success attempt
Status = Pass
Exp: David Tzemach - Technical Vision
Actual:David Tzemach - Technical Vision
---------------------------------------------------------------------
---------------------------------------------------------------------
Test Execition is Ended
| End Time07/08/2015 11:00:19
---------------------------------------------------------------------
---------------------------------------------------------------------