Link to home
Start Free TrialLog in
Avatar of Allan
AllanFlag for United States of America

asked on

Selenium C#: Browser Fires Up Once

Hi Experts!

Trying to create a Selenium test class (MSTest) in C#. Would like the browser to fire up only once before we run all tests from Test Explorer,
so that for each test method it would be able to use the same browser.

Running the following code below we have this error:
SetupToRunOnceBeforeAllTestsRun has wrong signature. The method must be static, public, does not return a value and should take a single parameter of type TestContext. Additionally, if you are using async-await in method then return-type must be Task.

SearchPageTest
[TestClass]
    public class SearchPageTest : BasePageTest
    {

        private SearchPage searchPage { get; private set; }
        private SearchCriteria searchCriteria;

        [ClassInitialize]
        public void SetupToRunOnceBeforeAllTestsRun(TestContext context)
        {
            driver = GetChromeDriver();
            searchPage = new SearchPage(driver);
        }

        [TestMethod]
        [TestCategory("IntegrationAutomation"), TestCategory("UI"), TestCategory("SearchPage")]
        public void SearchPage_GoToSearchPage_Success()
        {
            //Arrange
   
            //Act

            //Assert            
            Assert.IsTrue(searchPage.GoToSearchPage().IsPageVisible);
        }
        
        [TestMethod]
        [TestCategory("IntegrationAutomation"), TestCategory("UI"), TestCategory("SearchPage")]
        public void SearchPage_SomeOtherTest_Success()
        {
            //Arrange
   
            //Act

            //Assert            
            Assert.IsTrue(searchPage.SomeThingElse());
        }
        
        
        [ClassCleanup]
        public void CleanUpAfterAllTestsRan()
        {
            driver.Close();
            driver.Quit();
        }
   }

Open in new window


BasePageTest:
public class BasePageTest
    {
        internal IWebDriver driver { get; set; }

        public IWebDriver GetChromeDriver()
        {
            var outPutDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Driver";
            return new ChromeDriver(outPutDirectory);
        }
    }

Open in new window


Any help is appreciated to get this working.

TIA!
Avatar of Allan
Allan
Flag of United States of America image

ASKER

ok, got it to work. Just needed to make it into a static method.

        public static SearchPage searchPage { get; private set; }
        private SearchCriteria searchCriteria;

        [ClassInitialize]
        public static void SetupForEverySingleTestMethods(TestContext context)
        {
            driver = GetChromeDriver();
            searchPage = new SearchPage(driver);
        }

        [ClassCleanup]
        public static void ClassCleanup()
        {
            driver.Close();
            driver.Quit();
        }

Open in new window


    public class BasePageTest
    {
        internal static IWebDriver driver { get; set; }

        public static IWebDriver GetChromeDriver()
        {
            var outPutDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Driver";
            return new ChromeDriver(outPutDirectory);
        }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Allan
Allan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial