1
\$\begingroup\$

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

}

\$\endgroup\$
2
\$\begingroup\$

Move your 4 lines into a method with a [TestFixtureSetUp] Attribute. This will run everytime a test is run.

See here: http://www.nunit.org/index.php?p=fixtureSetup&r=2.2.10

For example:

class ShortTextPopupTests : BaseTest
{
    private ENAEPAdminLoginPage adminLoginPage;
    private AdminResetPage adminResetPage;
    private AppropriateType studentAssessmentPage;

    [TestFixtureSetUp]
    public void SetupFixture()
    {
        adminLoginPage = new ENAEPAdminLoginPage(Driver);
        adminLoginPage.GoToAndLoginWithOperationalBookletSupervisor();
        adminResetPage = new AdminResetPage(Driver);
        studentAssessmentPage = adminResetPage.GoToSpecificBookletLocation(BookletLocations.ShortTextPopup);
        studentAssessmentPage.ShortTextPopup.OpenPopUp();
    }

    [Test]
    [Retry(2)]
    [Description("Validate that the pop up can open and close when it is clicked.")]
    public void GivenCorrectItemIsOpened_WhenTogglingPopup_ThenPopupOpensAndCloses()
    {
        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()
    {
        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()
    {
        studentAssessmentPage.ClickRandomPlacesOnPage();
        Assert.IsTrue(studentAssessmentPage.ShortTextPopup.IsPopUpOpen(), "The popup did not stay open after clicking in random places in the assessment.");
    }
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I thought about the same thing. 2 problems that I can see here. First, the test doesn't read like a "test" anymore. For example, "do a, then b, then c". Now it reads like "do c". If tests are living documentation, then that poses a problem. Second, now that means each [TestFixture] which focuses on a special functionality will have hidden Setups that give you access to the appropriate Page Objects. So rather than having clean tests guided by methods inside of a page, you need to know that you shouldn't start from scratch. Maybe these aren't problems, just thinking out loud. \$\endgroup\$ – Nikolay Advolodkin Aug 11 '16 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.