My problem/concern is that I am trying to remove duplicate code from my functional tests without breaking the Page Object Pattern that I am following.
If you look at the code below, you will notice that 4 lines are constantly repeated in my tests, breaking the DRY principle - they are marked with a "todo" comment. But the problem is that the methods belong to 2 different Page Objects/classes. Therefore, I don't know how to condense this code because it doesn't belong in either page since it uses 2 different pages.
I'm following the Single Responsibility Principle that the Page Object class actually represents a single web page through my code, essentially doing 1 thing. I don't want to break this principle in order to condense my code. But I also don't want to maintain this duplicate code throughout a bunch of my tests.
The problem my code solves is that they are tests written against a GUI to exercise the functionality under test.
using Automation.Pages;
using NUnit.Framework;
namespace Automation.Tests
{
[TestFixture, Author("nikolay"), Category("LL"), Property("FunctionalityTested","ShortTextPopUp")]
class ShortTextPopupTests : BaseTest
{
[Test]
[Retry(2)]
[Description("Validate that the pop up can open and close when it is clicked.")]
public void GivenCorrectItemIsOpened_WhenTogglingPopup_ThenPopupOpensAndCloses()
{
//todo these 4 lines are repeated everywhere for the setup of this test
var adminLoginPage = new ENAEPAdminLoginPage(Driver);
adminLoginPage.GoToAndLoginWithOperationalBookletSupervisor();
var adminResetPage = new AdminResetPage(Driver);
var studentAssessmentPage = adminResetPage.GoToSpecificBookletLocation(BookletLocations.ShortTextPopup);
studentAssessmentPage.ShortTextPopup.OpenPopUp();
studentAssessmentPage.ShortTextPopup.ClosePopUp();
Assert.IsFalse(studentAssessmentPage.ShortTextPopup.IsPopUpOpen(), "The pop up did not close after trying to close the pop up");
}
[Test]
[Retry(2)]
[Description("Validate that opening a pop up, clicking next, coming back to a page, closes that opened pop up")]
public void NavigationToDifferentPageClosesPopup()
{
//todo these 4 lines are repeated everywhere for the setup of this test
var adminLoginPage = new ENAEPAdminLoginPage(Driver);
adminLoginPage.GoToAndLoginWithOperationalBookletSupervisor();
var adminResetPage = new AdminResetPage(Driver);
var studentAssessmentPage = adminResetPage.GoToSpecificBookletLocation(BookletLocations.ShortTextPopup);
studentAssessmentPage.ShortTextPopup.OpenPopUp();
studentAssessmentPage.ENAEPToolBar.ClickNextButton();
studentAssessmentPage.ENAEPToolBar.ClickBackButton();
Assert.IsFalse(studentAssessmentPage.ShortTextPopup.IsPopUpOpen(), "The pop up did not close after leaving and coming back to the page.");
}
[Test]
[Retry(2)]
[Description("Opening a short text pop up and then clicking on other things on the page should not close the pop up.")]
[Property("TCID", "35458")]
public void ClickingItemsOnScreenShouldNotClosePopUps()
{
//todo these 4 lines are repeated everywhere for the setup of this test
var adminLoginPage = new ENAEPAdminLoginPage(Driver);
adminLoginPage.GoToAndLoginWithOperationalBookletSupervisor();
var adminResetPage = new AdminResetPage(Driver);
var studentAssessmentPage = adminResetPage.GoToSpecificBookletLocation(BookletLocations.ShortTextPopup);
studentAssessmentPage.ShortTextPopup.OpenPopUp();
studentAssessmentPage.ClickRandomPlacesOnPage();
Assert.IsTrue(studentAssessmentPage.ShortTextPopup.IsPopUpOpen(), "The popup did not stay open after clicking in random places in the assessment.");
}
}
}