0

I have created a test framework in VS 2017. The framework is written in the Page Object Pattern. To avoid having one test class and n number of test methods I have decided to split all my tests into separate test classes which each has one test method and one cleanup method.

Currently, I have two test classes. One of them checks for the correct user login and the other one checks login page validation by inputting false credentials.

I am adding the code below:

using System;
using System.Runtime.Remoting.Channels;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SystemUiAutomationTestFramework;

namespace TestSuite.LoginPage
{
    [TestClass]
    public class LoginPageTest
    {
        [TestMethod]
        public void Can_Go_LoginPage()
        {
            Pages.LoginPage.Goto();
            Assert.IsTrue(Pages.LoginPage.IsAtLoginPage());
            Assert.IsTrue(Pages.LoginPage.IsAtLoginPageTitle());
            Assert.IsTrue(Pages.LoginPage.IsAtLoginField());
            Assert.IsTrue(Pages.LoginPage.IsAtPswdField());
            Assert.IsTrue(Pages.LoginPage.IsAtLoginButton());
            Pages.LoginPage.InputCredentials(SettingsService.Username, SettingsService.Userpassword);
            Pages.HomePage.IsAtHomePage();

            Pages.LoginPage.ReportPageSuccessLogin();
        }

        [TestCleanup]
        public void CleanUp()
        {
            Browser.Close();
        }
    }
}

Validation login page:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SystemUiAutomationTestFramework;

namespace TestSuite.LoginPage
{
    [TestClass]
    public class LoginPageValidationTest
    {
        [TestMethod]
        public void Can_Validate_LoginPage()
        {
            Pages.LoginPage.Goto();
            Assert.IsTrue(Pages.LoginPage.IsAtLoginPage());
            Assert.IsTrue(Pages.LoginPage.IsAtLoginPageTitle());
            Assert.IsTrue(Pages.LoginPage.IsAtLoginField());
            Assert.IsTrue(Pages.LoginPage.IsAtPswdField());
            Assert.IsTrue(Pages.LoginPage.IsAtLoginButton());
            Pages.LoginPage.InputCredentials("abcd.efgh", "test123");
            Pages.LoginPage.WaitErrorMessage();
            Assert.IsTrue(Pages.LoginPage.IsAtErrorMessage());

            Pages.LoginPage.ReportPageValidationTest();

        }

        [TestCleanup]
        public void CleanUp()
        {
            Browser.Close();
        }
    }
}

The issue starts when I want to execute both test classes... one test finishes correctly but the other fails because of the error message regarding the instance of web driver. I would like to know how can I run those test either one by one or is there any different way to do that? Should I use NUnit framework for that and if so then how should refactor my test page code? I would like to have my test independent.

* Updated *

I thought that I would place my whole code for the Browser which is a class where Webdriver is stored:

using System;
using System.Dynamic;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

namespace SystemUiAutomationTestFramework
{
    public static class Browser
    {
        private static readonly IWebDriver _webDriver = new ChromeDriver(Properties.Settings.Default.ChromePathDriver);

        public static IWebDriver WebDriver {
            get { return _webDriver; }
        }
        public static ISearchContext Driver
        {
            get { return _webDriver; }
        }

        public static string Url
        {
            get { return _webDriver.Url; }
        }

        public static string Title
        {
            get { return _webDriver.Title; }
        }

        public static void Goto(string url)
        {
            _webDriver.Manage().Window.Maximize();
            _webDriver.Url = url;

        }

        public static void Close()
        {
            _webDriver.Quit();
        }
    }
}

My Page API:

public static class Pages
    {
        public static LoginPage LoginPage
        {
            get
            {
                var loginPage = new LoginPage();
                PageFactory.InitElements(Browser.Driver, loginPage);
                return loginPage;
            }
        }

If you need to see my page object class for LoginPage let me know.

Thank you for your answers and time,

  • Where are you instantiating the web driver? – Kate Paulk Feb 15 '18 at 15:53
  • Hello Kate Paulk, my method Pages.LoginPage.Goto() starts up the webdriver – Automation_Padawan Feb 15 '18 at 16:19
1

Does your tests runs in parallel? If that is the case, then you can not run parallel tests with this static instance to the Webdriver. Also you can see how the tests behaves in nunit 3.xx and 2.xx (because there could be a difference). See if there is a difference using /noshadow and /nothread switches. Finally I would advice you to refactor your framework to not use static instance to the Webdriver.

  • Dear chesthar, thank you for your answer I do understand your point of view it goes for the parallel test running and I agree that the static idea for parellel running is not good but here I do not want to run test in parallel but one by one. I am going to try with NUnit versions and see if it is going to help me. – Automation_Padawan Feb 18 '18 at 13:43
  • I was able to solve the issue on my own. I have created an property for my driver class and an Initialize method which is called each time a test class is called. Also refactored my code to be more flexible. – Automation_Padawan Mar 7 '18 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.