FenWangNZ / blog

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

First refactor of the Test_1 and Test_0_NoInput #8

Open FenWangNZ opened 3 years ago

FenWangNZ commented 3 years ago

Some duplications like parameters that are initialized in this way should be taken out of each test and be separated into parameters and instantiations.

var driver = ChromeDriver();
LoginPage loginPage = new LoginPage(driver);

(1) Paremeters set just under test class which is "LoginTests" in our case. so they can be access by all test methods.

IWebDriver driver;
LoginPage loginPage;

(2) Instantiations(code below) should fall under a new method called "SetupBeforEverySingleMethod()". This method should be " public void". We will put a property[TestInitialize] before this method so that it can be executed before every single test.

driver = ChromeDriver();
loginPage = new LoginPage(driver);

(3) The following codes are duplications because every time I test login I need to open the website and maximise it. so I can also put these codes into SetupBeforEverySingleMethod().

loginPage.Open();
driver.Manage().Window.Maximize();

(4) Same as quit() and close(). We can put it a property called [TestCleanup]

driver.Close();
driver.Quit();

(5)Lets see the resulting coding after removing these duplications of these Test_1() and Test_0_NoInput().

using [System.IO](http://system.io/);
using System.Reflection;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace CNZBATests
{
[TestClass]
[TestCategory("LoginModule")]
public class LoginTests
{
IWebDriver driver;
LoginPage loginPage;
```csharp
    [TestInitialize]
    public void SetupBeforEverySingleMethod()
    {
        driver = ChromeDriver();
        loginPage = new LoginPage(driver);
        loginPage.Open();
        driver.Manage().Window.Maximize();
    }

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

    [TestMethod]
    [Description("Validate that the user is able to login successfully using validate data")]
    public void Test_1()
    {
        var userInfo = new TestUser();
        userInfo.EmailAddress = "guest@guest.com";
        userInfo.PassWord = "q1111111";

        loginPage.InputUserInfoAndLogin(userInfo);
        Assert.AreEqual("CBA Invoicing", driver.Title);
    }

    [TestMethod]
    [Description("Validate no input")]
    public void Test_0_NoInput()
    {
        loginPage.NoInput();
        Assert.IsTrue(loginPage.AlertforEmailAddress.Text.Contains("You Must Enter A Value."));
        Assert.IsTrue(loginPage.AlertforPassword.Text.Contains("You Must Enter A Value."));
        Assert.IsFalse(loginPage.LoginButton.Enabled);

    }

    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 3 years ago

refactor--->refactoring