Search code examples
c#seleniumautomationautomated-testspageobjects

Test Automation - Page Object - Strategy Design Pattern


I am wondering what would be the best design for a problem I am having

I have a page in my application in which you can create a message. There are multiple ways to create a message depending on the user.

I created a class called CreateMessagePage. This class holds all the elements and methods for that page.

I have a few ways to create an message in which each take different parameters:

  • Create Message with Country A
  • Create Message on behalf of Country B
  • Create Message with Fake Account
  • etc.

So I created a class for each type and each class implements CreateMessagePage

                         CreateMessagePage.cs
      |                          |                                  |
CreateMessage.cs      CreateMessageOnBehalfOf.cs      CreateMessageFakeAccount.cs
-CreateMessage()          -CreateMessage()                -CreateMessage()

I then create an instance of these classes to use in my speckflow feature steps(test case class)

for example:

CreateMessage.CreateMessage(string 1, string 2, string 3);

CreateMessageOnBehalfOf.CreateMessage(string 1, string 2, string 3, string 4, string 5);

I want to incorporate the Strategy Design Pattern but unsure what the best approach is which page objects? Should i incorporate a CreateMessage Interface?

Just looking for opinions on how this situation is usually done in test automation frameworks


Solution

  • I would have added this as a comment but the formatting would be all over the place. You are using specflow so why not just incorporate that and add the library Faker.net which will create random fake data?

    Feature:

    Given on the page MyPageName I add a message as a |Country A|
    Given on the page MyPageName I add a message as a |Country B|
    Given on the page MyPageName I add a message as a |Fake Account|
    

    Step:

     using Faker; //add faker.net from nuget
    
    
             var _randMessage = Faker.Lorem.Sentence(3);
    
    
            [Given(@"on the page MyPageName I add a message as a \|(.*)\|")]
        public void GivenOnThePageMyPageNameIAddAMessageAsACountryA(string value)
        {
            switch (value.ToLower())
        {
            case "country a":
                 Pages.CreateMessageOnBehalfOf(_randMessage);
                break;
    
            case "country b":
                 Pages.CreateMessage(_randMessage);
                break;
    
           case "fake account":
                Pages.CreateMessageFakeAccount(_randMessage);
                break;
        }
        }