Skip to content

ProjectTestBase class

Jakub Raczek edited this page Apr 25, 2019 · 9 revisions

ProjectTestBase class should be added as a content to you project during installation of nuget package. Implementation of ProjectTestBase classes differs depending on unit test framework you selected to use with our test framework

Each of ProjectTestBase class contains methods for the internet browser to configure it with proper settings and start it before each of test:

this.DriverContext.CurrentDirectory = TestContext.CurrentContext.TestDirectory;
this.DriverContext.TestTitle = TestContext.CurrentContext.Test.Name;
this.LogTest.LogTestStarting(this.driverContext);
this.DriverContext.Start();

after each test the browser is stopped and additional action are taken in case of test failure, like taking the screen shots

this.DriverContext.IsTestFailed = TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed || !this.driverContext.VerifyMessages.Count.Equals(0);
this.SaveTestDetailsIfTestFailed(this.driverContext);
this.DriverContext.Stop();
this.LogTest.LogTestEnding(this.driverContext);
if (this.IsVerifyFailedAndClearMessages(this.driverContext) && TestContext.CurrentContext.Result.Outcome.Status != TestStatus.Failed)
   {
       Assert.Fail();
   }

You can modify ProjectTestBase class to fit your needs, e.g. to start the browser once before all tests from your test class instead of starting it before each of tests methods from that test class.

Implementation for NUnit:

namespace Ocaramba.Tests.NUnit
{
    using global::NUnit.Framework;
    using global::NUnit.Framework.Interfaces;

    using Ocaramba;
    using Ocaramba.Logger;

    /// <summary>
    /// The base class for all tests
    /// </summary>
    public class ProjectTestBase : TestBase
    {
        private readonly DriverContext driverContext = new DriverContext();

        /// <summary>
        /// The browser manager
        /// </summary>
        protected DriverContext DriverContext
        {
            get
            {
                return this.driverContext;
            }
        }

        /// <summary>
        /// Logger instance for driver
        /// </summary>
        public TestLogger LogTest
        {
            get
            {
                return this.DriverContext.LogTest;
            }

            set
            {
                this.DriverContext.LogTest = value;
            }
        }

        /// <summary>
        /// Before the class.
        /// </summary>
        [OneTimeSetUp]
        public void BeforeClass()
        {
            this.DriverContext.CurrentDirectory = TestContext.CurrentContext.TestDirectory;
            this.DriverContext.Start();
        }

        /// <summary>
        /// After the class.
        /// </summary>
        [OneTimeTearDown]
        public void AfterClass()
        {
             this.DriverContext.Stop();
        }

        /// <summary>
        /// Before the test.
        /// </summary>
        [SetUp]
        public void BeforeTest()
        {
            this.DriverContext.TestTitle = TestContext.CurrentContext.Test.Name;
            this.LogTest.LogTestStarting(this.driverContext);
        }

        /// <summary>
        /// After the test.
        /// </summary>
        [TearDown]
        public void AfterTest()
        {
            this.DriverContext.IsTestFailed = TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed || !this.driverContext.VerifyMessages.Count.Equals(0);
            this.SaveTestDetailsIfTestFailed(this.driverContext);
            this.LogTest.LogTestEnding(this.driverContext);
            if (this.IsVerifyFailedAndClearMessages(this.driverContext) && TestContext.CurrentContext.Result.Outcome.Status != TestStatus.Failed)
            {
                Assert.Fail();
            }
        }
    }
}
Clone this wiki locally