April 23, 2020

Getting controls of all action classes to one base class object

  April 23, 2020
If you have five type of actions classes in your framework and you want to control methods in all those classes from a single class. The object of that base class will be our single point for the framework related actions, whether it is Selenium actions, reporting or data base connections.

This can be used even if we are planning to include one more tool to our framework.

We can design this kind of framework by structuring our classes as below.


For getting control of all these action classes to one base class , we need to create an object of all action classes in the base class constructor as below.



Now in this base class, we have control of all the action class, So if we create an object of this base class that object will be having access to all the methods inside these action classes.

For giving this access, we need to pass the object of the base class to each of the action class in the form of a constructor argument as below.


1
2
3
4
5
6
7
8
9
    public class ReportLogger
    {
        BaseClass objBaseClass;

        public ReportLogger(BaseClass objBaseClass)
        {
            this.objBaseClass = objBaseClass;

        }

When coming to the AUT project, we can extend this class to our Project Base class to use these in out test project.

Project Base class will be a similer class with objects created in constructor. But in this case we will be creating objects of each pages in POM instead of action classes.

A Sample test hooks class in given below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace Project
{
    [TestClass]
  
    public class AutoF_Handler : AutoF_BaseClass
    {
        public TestContext TestContext { get; set; }

        /// <summary>
        /// This method will trigger before every Test
        /// </summary>
        [TestInitialize]
        public void BeforeTest()
        {

            testContextInstance = TestContext;
            reportLogger.ConfigureExtentReports();
            exParentTest = extent.CreateTest(TestContext.TestName);
            exChildTest = exParentTest.CreateNode("Browser Launch");
            driver = genericMethods.LaunchBrowser(testDataLoader.GetRunSettingValue("Browser"));
            
        }


        /// <summary>
        /// This method will run after every test to perform Clean Up
        /// </summary>
        [TestCleanup]
        public void AfterTest()
        {
            String resStatus = "PASS";
            if (boolFailFlag == true || !(TestContext.CurrentTestOutcome == UnitTestOutcome.Passed))
            {
                reportLogger.WriteLog("fail", "Test Outcome :" + TestContext.CurrentTestOutcome+ " - Refer stack trace for details");
                resStatus = "FAIL";
            }

            extent.Flush();
            driver.Close();
            driver.Quit();
            driver.Dispose();

            TestContext.AddResultFile(genericMethods.changeResultFileStatus(RunFile, resStatus));

            if (boolFailFlag == true || !(TestContext.CurrentTestOutcome == UnitTestOutcome.Passed))
            {
                Assert.Fail("Test failed due to following issues:" + strFailLogs);
            }

        }
       
    }
}

Now we can extend this Test handler alias test hooks class to each Test Classes, So the Before and After running each test methods, these hooks will be called.
logoblog

Thanks for reading Getting controls of all action classes to one base class object

Previous
« Prev Post

1 comment:

Bookmark this website for more workarounds and issue fixes.

Verify Zip file contents without extracting using C# + Selenium

While doing automation testing, we may get a scenario where we need to download a zip file and to validate the contents inside it. One way t...