簡體   English   中英

如何在不關閉它的情況下在同一個瀏覽器實例中運行多個測試方法(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();
    }

看起來你需要測試鏈,但依賴於其他測試的測試指向設計缺陷。 然而,有辦法實現這一目標。 您可以創建有序單元測試,這基本上是一個確保測試順序的單個測試容器。

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

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

執行順序將在您將其添加到播放列表時,但如果您想要更改它,則您擁有該文件

播放列表

如果您需要在執行期間保留測試數據/對象,您可以使用一些全局變量。 我正在使用這樣的TestDataStore:

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

因此,您可以隨時添加和檢索所需內容(包括會話詳細信息,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打開一次(如果您使用“使用Microsoft.VisualStudio.TestTools.UnitTesting;”則為TestClass),並且該夾具中的所有[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
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM