FenWangNZ / blog

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

Starting automation framework #7

Open FenWangNZ opened 3 years ago

FenWangNZ commented 3 years ago

After days of self-learning online. I started the automation framework two days ago.

Step One: Prestep for LoginTests(to validate login functionality)

Setup ChromeDriver and open the website that we are going to test.

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

Pass driver into LoginPage() then create LoginPage.cs.

Download ChromeDriver.exe, add it into our solution and set the property to be "Copy if newer"

Set driver in LoginPage.cs:

internal class LoginPage
{
public LoginPage(IWebDriver driver)
{
Driver = driver;
}
public IWebDriver Driver { get; }

Create a new UnitTest Project.

  1. Set the name of the Test method, add TestCategory and Description for this method.
  2. Pre-steps for LoginTests—-set up chromedriver and open the website
var driver = ChromeDriver();
var loginPage = new LoginPage();
loginPage.Open();

Correct each complaining step by step:

(1)Add ChromeDriver() in Test_1.cs and header files.

build it, but it does nothing, only open chrome browser, so we need to set open():

internal void Open()
{
Driver.Navigate().GoToUrl("[https://cbaaccountingwebapptest.azurewebsites.net](https://cbaaccountingwebapptest.azurewebsites.net/)");
}

Build it.

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);
}

(2)Download chromedriver.exe, add it and modify its property to "Copy if newer".

(3)Add a new class named "LoginPage", and in this class define driver because we are going to use this driver to open this page.

public LoginPage(IWebDriver driver)
{
         Driver = driver;
}

public IWebDriver Driver { get; }

(4)Build it and it passes, but currently it does nothing, so we need to pass parameters in LoginPage() method in LoginPage.cs so that we can open this page:

LoginPage loginPage = new LoginPage(driver);

Then we define open(), lets do like this:

Driver.Navigate().GoToUrl("[https://cbaaccountingwebapptest.azurewebsites.net](https://cbaaccounting.net/)");

Then build it. Now we can open the page that we are going to test.

Step two: Writing Test_1: to validate that users are able to login successfully using the correct info.

First we need to write the skeleton of the Test_1; Write it in plain english from users/a tester's perspective.

we will pass a "userinfo" as parameter into InputUserInfoAndLogin() because we've already have that knowledge that we can set a class to store our user's properties: EmaliAddress and Password.

        driver.Manage().Window.Maximize();
        loginPage.InputUserInfoAndLogin(userInfo);
        Assert.AreEqual("CBA Invoicing", driver.Title);

Of course, it complains, now we proceed to refactor it.

(1)Creat a method called InputUserInfoAndLogin() and pass "TestUser User" as its parameter. TesterUser is a class that stores two properties as we said before.

(2)TestUser complains so we create TestUser.class file.

(3)Add two properties in TestUser.cs:EmailAddress PassWord;

public string EmailAddress { get; set; }
public string PassWord { get; set; }

(4)Modify InputUserInfoAndLogin() in Loginpage.cs,set the properties of three elements that we are going to locate on LoginPage:

public IWebElement EmailAddress => Driver.FindElement(By.XPath("//*[@type='text']"));
public IWebElement PassWord => Driver.FindElement(By.XPath("//*[@type='password']"));
public IWebElement LoginButton => Driver.FindElement(By.XPath("//*[@class=''mat-raised-button mat-button-base mat-warn'']"));

Then use these 3 properties in InputUserInfoAndLogin().

internal void InputUserInfoAndLogin(TestUser user)
{
EmailAddress.SendKeys(user.EmailAddress);
PassWord.SendKeys(user.PassWord);
LoginButton.Click();
}

Step Three: Writing Test_0_noInput: to validate given no input the page will give alert in each field.

(1) Write skeleton of this Test_0_noInput():

var driver = ChromeDriver();
LoginPage loginPage = new LoginPage(driver);
loginPage.Open();
Thread.Sleep(1000);
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);

(2) Create NoInput() in LoginPage.cs:

internal void NoInput()
{
Actions actionsObj = new Actions(Driver);
//Move the cursor From the user account to password field
actionsObj.Click(EmailAddress).Perform();
actionsObj.Click(PassWord).Perform();
actionsObj.Click(EmailAddress).Perform();
}

(3) Create AlertforEmailAddress and AlertforPassword for Assertion on LoginPage.cs:

public IWebElement AlertforEmailAddress => Driver.FindElement(By.XPath("//**id='mat-error-0']"));
public IWebElement AlertforPassword => Driver.FindElement(By.XPath("//**[@id='mat-error-1']"));