FenWangNZ / blog

I love learning. This is my wiki.
0 stars 0 forks source link

Create BaseTest #11

Open FenWangNZ opened 4 years ago

FenWangNZ commented 4 years ago

Creating BaseTest is the same as creating our BasePage. When we have some common functions to be shared among multiple classes, this is the best time to inherit. Inheritance allows us to put the function in a parent class so that the child class can use it. Let's see how to implement it.

We now need to define a BaseTest class. This class will store all the things we copy between classes. (For example, LoginTest and InvoicePage copy the driver, clean and setup methods), so store these in them. After the copy is complete, we only need to add the referenced project file. Very simple!

using System.IO;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace CNZBATests
{
    public class BaseTest
    {
        public IWebDriver Driver { get; private set; }

        [TestInitialize]
        public void SetupBeforEverySingleMethod()
        {
            Driver = ChromeDriver();

        }

        [TestCleanup]
        public void CleanupAfterEverySingleMethod()
        {
            Driver.Close();
            Driver.Quit();
        }

        private IWebDriver ChromeDriver()
        {
            //Get the name of the directory of the location of the executable;
            var outPutDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            //Return a new ChromeDriver with the path to the ChromeDriver that we have now: the binary;
            return new ChromeDriver(outPutDirectory);
        }
    }
}
FenWangNZ commented 4 years ago

Update the title