如何在不关闭同一浏览器实例的情况下 运行 多个测试方法(C#、SeleniumWebDriverz NUnit)?
How to run multiple test methods in same browser instance without closing it (C#, SeleniumWebDriverz NUnit)?
我是 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();
}
您可以将 selenium 属性 restart.browser.each.scenario
设置为 false
,也许这已经适合您了。在 Java 中,您可以将此 属性 设置为
System.setProperty("restart.browser.each.scenario", "false");
我认为在 C# 中它会像
System.Environment.SetEnvironmentVariable("restart.browser.each.scenario","false");
看起来您需要测试链,但是让测试依赖于其他测试表明存在设计缺陷。然而,有一些方法可以实现这一目标。您可以创建有序的单元测试,它基本上是确保测试顺序的单个测试容器。
这是 MSDN 上的指南,或者您可以使用 播放列表
Right click on the test method -> Add to playlist -> New playlist
执行顺序将在您将它们添加到播放列表时执行,但如果您想更改它,您有文件
如果你需要在执行过程中保持测试data/objects,你可以使用一些全局变量。我正在使用这样的 TestDataStore:
private Dictionary<string, yourObjData> _dataStore = new Dictionary<string, yourObjData>();
因此您可以随时添加和检索您需要的内容(包括您的会话详细信息、网络驱动程序等)。
好的,这是我找到的解决方案。而不是:
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,如果您使用 "using Microsoft.VisualStudio.TestTools.UnitTesting;")打开一次,并且来自该夹具的所有 [Test]-s 将在同一浏览器实例中 运行 .完成所有测试后,浏览器将关闭。
希望这对以后的其他人有所帮助。问我是否需要其他帮助。
HI 如果您使用 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
}
我是 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();
}
您可以将 selenium 属性 restart.browser.each.scenario
设置为 false
,也许这已经适合您了。在 Java 中,您可以将此 属性 设置为
System.setProperty("restart.browser.each.scenario", "false");
我认为在 C# 中它会像
System.Environment.SetEnvironmentVariable("restart.browser.each.scenario","false");
看起来您需要测试链,但是让测试依赖于其他测试表明存在设计缺陷。然而,有一些方法可以实现这一目标。您可以创建有序的单元测试,它基本上是确保测试顺序的单个测试容器。
这是 MSDN 上的指南,或者您可以使用 播放列表
Right click on the test method -> Add to playlist -> New playlist
执行顺序将在您将它们添加到播放列表时执行,但如果您想更改它,您有文件
如果你需要在执行过程中保持测试data/objects,你可以使用一些全局变量。我正在使用这样的 TestDataStore:
private Dictionary<string, yourObjData> _dataStore = new Dictionary<string, yourObjData>();
因此您可以随时添加和检索您需要的内容(包括您的会话详细信息、网络驱动程序等)。
好的,这是我找到的解决方案。而不是:
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,如果您使用 "using Microsoft.VisualStudio.TestTools.UnitTesting;")打开一次,并且来自该夹具的所有 [Test]-s 将在同一浏览器实例中 运行 .完成所有测试后,浏览器将关闭。
希望这对以后的其他人有所帮助。问我是否需要其他帮助。
HI 如果您使用 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
}