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

  • Question

  • I want to run

    • Particular test case with particular browser
    • Testcases with multiple browsers

      My code is:

       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;
        }
    }

    1. I want to run these two testcases with multiple browser must include chrome and firefox.
    2. TestMethod1 with Chrome and TestMethod2 with firefox at the same time in such way that both browsers initialize at the same time and testcases run at the same time

    I am using Mstest selenium webdriver and C#.

    Friday, April 24, 2020 9:58 AM

All replies

  • Hi Amna,

    Welcome to MSDN forum.

    Not sure if there are some conflictions if you try to open and use multiple browsers, you could try to use “[assembly: Parallelize(Workers=0, Scope=ExecutionScope.MethodLevel)]” to check if multiple browsers could work well in parallel.

    Refer more here: MSTest V2: in-assembly parallel test execution.

    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.

    Monday, April 27, 2020 9:42 AM
  • "[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)]" adding this in project is showing errors like •'Parallelize' could not be found (are you missing a using directive or an assembly reference?) •'ExecutionScope' does not exist in the current context •'Scope' could not be found (are you missing a using directive or an assembly reference?) •'Workers' could not be found (are you missing a using directive or an assembly reference?)

    How can I resolve this?

    [assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)]
    namespace TestCases
    {
    public class Test
                {
                   private IWebDriver WDriver;
                    [TestInitialize]
                    public void initilize()
                    {
    
                        WDriver = UniverslM.selectdriver(Chrome);
                     }
                    [TestMethod]
                    public void TestMethod1()
                    {
                       sometestcase
                    }
                    [TestMethod]
                    public void TestMethod2()
                    {
                        sometestcase1
                    }
                }
    }

    Monday, April 27, 2020 11:20 AM
  • Hi Amna,

    Thanks for your feedback.

    This error is caused by missing reference. Please right-click your test project > Manage NuGet Packages… > “Installed” and check if you have installed MSTest.TestFramework which describes as “This is MSTest V2, the evolution of Microsoft’s Test Framework”.

    If you didn’t install MSTest V2 framework, please try to install it(the NuGet Package I mentioned above). After that, make sure you have added “using Microsoft.VisualStudio.TestTools.UnitTesting;” to the top of the program.

    Feel free to let me know if you have any update.

    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.

    Tuesday, April 28, 2020 3:35 AM
  • I have already installed MSTest.TestFramework and i am already using Microsoft.VisualStudio.TestTools.UnitTesting. With this I am still getting the errors.
    Tuesday, April 28, 2020 5:03 AM
  • Hi Amna,

    Thanks for your feedback.

    It’s strange, if you try to create a new simple Unit Test project (.net framework) and modify codes as below, will this error persist?

    using System;
    using System.Threading;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    
    [assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)]
    namespace XXXXXXXXX
    {
        [TestClass]
        public class UnitTest1
        {
            [TestMethod]
            public void Test1() => Thread.Sleep(1000);
    
            [TestMethod]
            public void Test2() => Thread.Sleep(5000);
    
            [TestMethod]
            public void Test3() => Thread.Sleep(10000);
        }
    }

    If yes, I may suggest you to double-check if you use MSTest V2 framework(instead of MSTest V1). 

    If not, please check if you miss some necessary parts, but normally, there isn’t any limitation for [Parallelize] attribute.

    Looking forward to hearing from you.

    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.

    Tuesday, April 28, 2020 7:29 AM
  • My code is 

    using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Linq; [assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)] namespace TestCases {

    [TestClass] public class Test { private IWebDriver WDriver; [TestInitialize] public void initilize() { some code } [TestMethod] public void TestMethod1() {

    WDriver = Chrome sometestcase } [TestMethod] public void TestMethod2() {

    WDriver = Firefox sometestcase1 } } }

    And yes i had already installed the same package.

    Please note that i am using two different browsers to run test cases in parallel. please see my code.

    • Edited by Mehwish 001 Tuesday, April 28, 2020 8:19 AM
    Tuesday, April 28, 2020 8:03 AM
  • Hi Amna,

    Thank you for your feedback.

    Yes, I notice that you use two different browsers, since it is hard for me to reproduce your issue, I test with a simple sample on my side and the attribute works well.

    Perhaps you could try to add a .runsettings file and configure it like this then check if this issue persists:

    <?xml version="1.0" encoding="utf-8"?>
    <RunSettings>
      <!-- MSTest adapter -->
      <MSTest>
        <Parallelize>
          <Workers>0</Workers>
          <Scope>ClassLevel</Scope>
        </Parallelize>
      </MSTest>
    </RunSettings>

    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 11:12 AM