如何在不关闭它的情况下在同一浏览器实例中运行多个测试方法(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(); }
看起来你需要测试链,但依赖于其他测试的测试指向设计缺陷。 然而,有办法实现这一目标。 您可以创建有序unit testing,这基本上是一个确保测试顺序的单个测试容器。
这是MSDN上的指南,或者您可以使用播放列表
Right click on the test method -> Add to playlist -> New playlist
执行顺序将在您将它们添加到播放列表时,但如果您想要更改它,则您拥有该文件
如果您需要在执行期间保留测试数据/对象,您可以使用一些全局变量。 我正在使用这样的TestDataStore:
private Dictionary _dataStore = new Dictionary();
因此,您可以随时添加和检索所需内容(包括会话详细信息,Web驱动程序等)。
好的,所以这里是我找到的解决方案。 代替:
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将在同一个浏览器实例中运行。 完成所有测试后,浏览器将关闭。
希望这将有助于其他人。 问我是否需要其他帮助。
你可以将selenium属性restart.browser.each.scenario
设置为false
,也许这对你来说已经很有用了。 在Java中,您可以使用设置此属性
System.setProperty("restart.browser.each.scenario", "false");
我认为在C#中会有类似的东西
System.Environment.SetEnvironmentVariable("restart.browser.each.scenario","false");
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 }