1

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'm 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 WebDriver. 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.

Update #1:

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.

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

1 Answer 1

2

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.

2
  • 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. Feb 18, 2018 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. Mar 7, 2018 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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