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().
(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);
}
}
}
Some duplications like parameters that are initialized in this way should be taken out of each test and be separated into parameters and instantiations.
(1) Paremeters set just under test class which is "LoginTests" in our case. so they can be access by all test methods.
(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.
(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().
(4) Same as quit() and close(). We can put it a property called [TestCleanup]
(5)Lets see the resulting coding after removing these duplications of these Test_1() and Test_0_NoInput().