CSharp -Selenium - MSTEST - Running several methods in the same browser



  • Hello
    I want to run a Selenium test with several Methods and run them i order in the same browser.
    When i try to do that the browser is closed after the first Method is finished, but i want to keep it alive.

    Can i achieve this?

    On this simple example i have two Asserts. One that should pass and one that shold not.
    Using Chrome

    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    
    
    namespace MSTESTdotnetcoreSelenium
    {
    
        [TestClass]
        public class SeleniumTest
        {
    
            private IWebDriver driver;
            readonly String test_url = "https://google.com";
    
    
            [TestMethod]
            public void A_TheGoogleSearchTest()
            {
                driver.Url = test_url;
                System.Threading.Thread.Sleep(1000);
                Assert.IsTrue(driver.Title.Contains("Google"), "Verified Title");
    
            }
    
            [TestMethod]
            public void B_TheGoogleSearchTest()
            {
                System.Threading.Thread.Sleep(1000);
                Assert.IsTrue(driver.Title.Contains("Google_123"), "Verified Title");
            }
    
    
    
            [TestInitialize()]
            public void MySetupTest()
            {
                driver = new ChromeDriver();
                driver.Manage().Window.Maximize();
            }
    
            [TestCleanup()]
            public void MyTestCleanup()
            {
                driver.Quit();
            }
        }
    }
    


  • [TestCleanup()]
    

    is performed after each test, so thats why your browser is closed after finishing the 1st test

    If you want to close your browser after finishing all the tests in "SeleniumTest" class you should use [ClassCleanup()] instead of [TestCleanup()]



  • @Dquan
    So simple misstake. Thanks!



  • @Dquan said in CSharp -Selenium - MSTEST - Running several methods in the same browser:

    [ClassCleanup()]

    Hi again.
    I have now tested to update the code with [ClassCleanup()].
    An now the driver won't start the browser. Also tested to update the start to.[ClassInitialize()].
    Any suggestions?

    Thanks in advance.
    Kind regards, Fred




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2