【问题标题】:How to run multiple test methods in same browser instance without closing it (C#, SeleniumWebDriverz NUnit)?如何在同一个浏览器实例中运行多个测试方法而不关闭它(C#,SeleniumWebDriverz NUnit)?
【发布时间】:2015-11-03 14:50:12
【问题描述】:

我是 c# 和 Selenium 的初学者。我想知道是否可以在同一个浏览器实例中运行多个 [TestMethod]-s 而无需关闭它?

例如“Can_Change_Name_And_Title”完成后,我想继续“Can_Change_Profile_Picture”。

    [TestMethod]
    public void Can_Change_Name_And_Title()
    {
        SidebarNavigation.MyProfile.GoTo();
        ProfilePages.SetNewName("John Doe").SetNewTitle("New Title Test").ChangeNameTitle();
    }

    [TestMethod]
    public void Can_Change_Profile_Picture()
    {
        SidebarNavigation.MyProfile.GoTo();
        ProfilePages.SetNewProfilePicture(Driver.BaseFilePath + "Profile.png").ChangeProfilePicture();
    }

【问题讨论】:

  • 嗯..我需要输入“new FirefoxDriver();”在 [ClassInitialize] 中,如果我理解得很好的话。现在它在我的 [TestInitialze] 部分......但是当我这样做时,我的测试会抛出异常。

标签: c# selenium testing selenium-webdriver automated-tests


【解决方案1】:

看起来您需要测试链,但是让测试依赖于其他测试表明存在设计缺陷。尽管如此,还是有办法实现这一目标的。您可以创建有序的单元测试,它基本上是一个确保测试顺序的单个测试容器。

这是MSDN 的指南,或者您可以使用播放列表

Right click on the test method -> Add to playlist -> New playlist

执行顺序将在您将它们添加到播放列表时,但如果您想更改它,您有文件

如果您需要在执行期间保留测试数据/对象,您可以使用一些全局变量。我正在使用这样的TestDataStore:

private Dictionary<string, yourObjData> _dataStore = new Dictionary<string, yourObjData>();

因此您可以随时添加和检索您需要的内容(包括您的会话详细信息、网络驱动程序等)。

【讨论】:

  • 不,这不是我要找的。我熟悉播放列表。我需要将所有内容保存在一个浏览器中。从 [TestClass] 开始到结束。
  • 如果您需要放置测试数据的位置 - 请参阅我的更新答案。还要在最后一个 [TestMethod] 中调用 driver.Dispose()。
【解决方案2】:

好的,这是我找到的解决方案。而不是:

 using Microsoft.VisualStudio.TestTools.UnitTesting;

我用过:

 using NUnit.Framework;

所以现在我有了下一个层次结构:

[TestFixture]
 [TestFixtureSetup] // this is where I initialize my WebDriver " new FirefoxDriver(); "
  [Test] //this is my first test
    public void Can_Change_Name_And_Title()
    {
        SidebarNavigation.MyProfile.GoTo();
        ProfilePages.SetNewName("John Doe").SetNewTitle("New Title Test").ChangeNameTitle();
    }
  [Test] //this is my second test
    public void Can_Change_Profile_Picture()
    {
        SidebarNavigation.MyProfile.GoTo();
        ProfilePages.SetNewProfilePicture(Driver.BaseFilePath + "Profile.png").ChangeProfilePicture();
    }

[TestFixtureTearDown] // this is where I close my driver

通过此更改,我的浏览器将只为 TestFixture(或 TestClass,如果您使用“使用 Microsoft.VisualStudio.TestTools.UnitTesting;”)打开一次,并且该夹具中的所有 [Test]-s 将在同一个浏览器实例中运行.完成所有测试后,浏览器将关闭。

希望这将在未来对其他人有所帮助。如果您需要其他帮助,请询问我。

【讨论】:

    【解决方案3】:

    您可以将 selenium 属性 restart.browser.each.scenario 设置为 false,也许这已经对您有用了。在 Java 中,您可以使用

    设置此属性

    System.setProperty("restart.browser.each.scenario", "false");

    我认为在 C# 中它会类似于

    System.Environment.SetEnvironmentVariable("restart.browser.each.scenario","false");

    【讨论】:

      【解决方案4】:

      你好,如果你使用的是 NUnit.Framework;

      代码执行计划如下。 对于第一个测试用例

      [TestFixtureSetup]   ---->For each test case this will work so here we can          
                                initialize  the driver instance.              
      [TestMethod]         ----->test method will goes here
      [TearDown]           -----> clean up code
                               **For Second Test Case**
      [TestFixtureSetup]               
      [TestMethod]
      [TearDown]
      

      如果您必须在一个浏览器实例中运行两个测试用例 不要在 TearDown 中关闭驱动程序。 并在 TextFixtureSetup 下初始化驱动程序

          [TestFixture()]
              public class TestClass
              {
      
                  [TestFixtureSetUp]
                  public void Init()
                  {
                      Driver.initialize(new InternetExplorerDriver());
                  }
                  [TearDown]
                 public void Close()
                  {
      //dont do any driver.close()
                  }
               [TestMethod]
              public void TestCase001()
              {
              //your code goes here
              }
       [TestMethod]
              public void TestCase002()
              {
              //your code goes here
              }
      

      【讨论】:

      • 您在解决方案中提到不关闭 TearDown 中的浏览器,然后我应该在第二个测试用例后关闭它吗?并保证测试用例将按照文件中写入的顺序运行?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      • 2019-11-10
      • 1970-01-01
      • 2014-05-02
      • 1970-01-01
      相关资源
      最近更新 更多