locked
How to run testcases with cross browsers using Selenium C# Mstest? RRS feed

  • Question

  • hi,

    I am using C# selenium webdriver MStest framework. I have 2 testcases. I want to run first testcase against chrome and second testcase against firefox at the same time. The testcase that has to run on chrome has URL1 and second testcase that has to run on firefox has different URL that is URL2. Both testcases are in same class. Code is

    namespace TestAutomation
    {
        [TestClass]
    public class Test
                {
                   private IWebDriver WDriver;
                    [TestInitialize]
                    public void initilize()
                    {
    
                        WDriver = UniverslM.selectdriver(Chrome);
                     }
                    [TestMethod]
                    public void TestMethod1()
                    {
                       sometestcase
                    }
                    [TestMethod]
                    public void TestMethod2()
                    {
                        sometestcase1
                    }
                }
    }

    The code in UniverslM.cs is

      static public IWebDriver selectdriver(brt)
            {
                IWebDriver driver;
                            switch (brt)
                            {
                                case Chrome:
                                    ChromeOptions cOption = new ChromeOptions();
                                    driver = new ChromeDriver(cOption);
                                    driver.Navigate().GoToUrl(URL);
                                    return driver;
                                case FireFox:
                                    FirefoxOptions fOption = new FirefoxOptions();
                                    driver = new FirefoxDriver(fOption);
                                    driver.Navigate().GoToUrl(URL);
                                    bool ele1 = AutoItX.WinExists("[CLASS:MozillaDialogClass]") == 1;
    
                            if (ele1)
                            {
                                AutoItX.WinActivate("[CLASS:MozillaDialogClass]");
                                AutoItX.Send("someusername");
                                AutoItX.Send("somepassword");
                            }
                            return driver;
        }
    }
    How can I do that?
    Wednesday, April 29, 2020 7:24 AM

All replies

  • Hi Amna,

    Welcome to MSDN forum.

    Maybe you could refer to this similar issue: MSTest-Cross browser testing C# with Selenium? and get some help.

    Try to use .RunSettings file and set like below:

    <?xml version="1.0" encoding="utf-8" ?>
    <RunSettings>
      <TestRunParameters>
        <Parameter name="browser" value="chrome" />
      </TestRunParameters>
    </RunSettings>

    Modify the test codes like this:

    [TestInitialize]
        public void initilize()
        {
            if ( TestContext.Properties["browser"].ToString() == "chrome" ) {
              // initilize ChromeDriver()
            } else {
              //initilize FirefoxDriver
            }
        }

    Hope this helps.

    Best Regards,

    Tianyu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 30, 2020 10:52 AM
  • @Dylan

    not even test methods could be run parallely on cross browsers using Mstest?

    Wednesday, May 6, 2020 5:18 AM
  • Hi Amna Khalid 001,

    Sorry for my misleading information.

    I write a sample which can run parallely on cross browsers using Mstest.

    Note: since the time required to start the browser is different, we will see that the chrome browser will be launched first.

    The sample code:

    using System;
    using System.Threading;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Firefox;
    
    [assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)]
    namespace UnitTestProject10
    {
        [TestClass]
        public class UnitTest1
        {
            private IWebDriver driver;
            public string URL = "http://www.google.com";
    
            [TestMethod]
            public void Chrome()
            {
                driver = new ChromeDriver();
                Thread.Sleep(5000);
                driver.Navigate().GoToUrl(URL);
                driver.Close();
            }
    
            [TestMethod]
            public void Fox()
            {
                driver = new FirefoxDriver();
                driver.Navigate().GoToUrl(URL);
                driver.Close();
            }
        }
    }
    

    The result:

    Any feedback will be expected.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Wednesday, May 6, 2020 10:30 AM
  • Thankyou so much. But this 
    [assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)]" 
    shows error in my code but if create sample project just like yours it works fine.
    Wednesday, May 6, 2020 11:54 AM
  • Hi Amna Khalid 001,

    What is the error shown on your side? If possible, could you share some codes which could help us reproduce it on our side?

    Looking forward to your reply.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Friday, May 8, 2020 9:41 AM