Code & QA

Assertion


One more thing I want to change in the test look is assertion. At the moment it is carrying out in this way:

nUnit.Assert.IsTrue(driver.Title == searchTerm1 + ” – Google Search”);

Report.AddInfo(testScope.ElementAt(0).Key, testScope.ElementAt(0).Value, driver.TakeScreenshot(testScope.ElementAt(0).Value));

I do not like it. Because Assert class of either NUnit or MSTool throws an exception which terminates test execution if something is going wrong. In addition it has no logging functionality.

For the improvement I created simple method named Assert and put it into the BaseTest class:

protected void Assert(bool condition, string expectedResult)

{

if (condition)

Report.AddInfo(“Assertion”, expectedResult, driver.TakeScreenshot(expectedResult));

else

Report.AddError(“Assertion”, expectedResult, condition.ToString() + ” is ” + condition, driver.TakeScreenshot(expectedResult));

}

The method is just a wrapper for assertion. I do not invoke embedded NUnit or MSTest assertion methods. I just verify equality of expected and actual results and report appropriate message. You may create any other implementation of verification. This is only one of hundreds possible approaches, as for me the simplest one.

Now assertion in a test may look like the following:

[Test]

[TestMethod]

public void SearchWithGoogle()

{

#region Test data

const string searchTerm1 = “jobs”;

const string searchTerm2 = “steve”;

#endregion

var testScope = new Dictionary<string, string> {

{ “Perform search with ” + searchTerm1 + ” term on New tab page”, “title of the page is ” + searchTerm1 + ” – Google Search” },

{ “Perform search with ” + searchTerm1 + ” ” + searchTerm2 + ” term on New tab page”, “Title of the page is ” + searchTerm1 + ” ” + searchTerm2 + ” – Google Search” },

{ “Close New tab page”, “Home page is displayed” }};

Report.StartTestCase(GetType().Name, MethodBase.GetCurrentMethod().Name, “Verification of search with Google”, testScope);

Report.RunStep(“Step 1: Search with ” + searchTerm1 + ” term”);

TestedApplication.NewTabPage.TextboxSearch.TypeText(searchTerm1);

TestedApplication.NewTabPage.ButtonSearch.Click();

nUnit.Assert.IsTrue(driver.Title == searchTerm1 + ” – Google Search”);

Report.AddInfo(testScope.ElementAt(0).Key, testScope.ElementAt(0).Value, driver.TakeScreenshot(testScope.ElementAt(0).Value));

Assert(driver.Title == searchTerm1 + ” – Google Search”, “Page title is “ + searchTerm1 + ” – Google Search”);

Report.RunStep(“Step 2: Search with ” + searchTerm1 + ” ” + searchTerm2 + ” term”);

TestedApplication.Backward();

TestedApplication.NewTabPage.TextboxSearch.AppendText(” ” + searchTerm2);

TestedApplication.NewTabPage.CheckboxSearchImagesOnly.Select();

TestedApplication.NewTabPage.ButtonSearch.Click();

nUnit.Assert.IsTrue(driver.Title == searchTerm1 + ” ” + searchTerm2 + ” – Google Search”);

Report.AddInfo(testScope.ElementAt(1).Key, testScope.ElementAt(1).Value, driver.TakeScreenshot(testScope.ElementAt(1).Value));

Assert(driver.Title == searchTerm1 + ” “ + searchTerm2 + ” – Google Search”, testScope.ElementAt(1).Value);

Report.RunStep(“Step 3: Close New tab page”);

TestedApplication.Backward();

TestedApplication.NewTabPage.ButtonClose.Click();

nUnit.Assert.IsTrue(TestedApplication.HomePage.IsLoaded);

Report.AddInfo(testScope.ElementAt(2).Key, testScope.ElementAt(2).Value, driver.TakeScreenshot(testScope.ElementAt(2).Value));

Assert(TestedApplication.HomePage.IsLoaded, “Home page is displayed”);

}

As for me now the assertion looks more elegant.

[row]
[column lg=”4″ md=”12″ sm=”12″ xs=”12″ ]
Test structure [/column]
[column lg=”4″ md=”12″ sm=”12″ xs=”12″ ]
Table Of Content
[/column]
[column lg=”4″ md=”12″ sm=”12″ xs=”12″ ]
Test execution step by step in details. How things work
[/column]
[/row]

Leave a Reply

Your email address will not be published. Required fields are marked *


Notice: Undefined offset: 0 in /home/chyqruxavcln/public_html/learnseleniumtesting.com/wp-content/plugins/cardoza-facebook-like-box/cardoza_facebook_like_box.php on line 924