2014-11-14 59 views
1

我有一個問題,運行多個C#硒單元測試與一個驅動程序實例的測試。如何使用一個驅動程序實例運行c#多個Selenium單元測試?

請在下面找到我的課程。

文件夾:Com.Main.Web.Selenium

SeleniumTestInitialize.cs是包含驅動程序的主類。

[DeploymentItem(@"Resources\IEDriverServer.exe")] 
public class SeleniumTestInitialize 
{ 
    public TestContext TestContext 
    { 
     get { return testContextInstance; } 
     set { testContextInstance = value; } 
    } 
    private TestContext testContextInstance; 

    public bool SeleniumExecutionTerminateFlag=false; 

    public SeleniumTestInitialize seleniumTestInitalize; 
    public FindWebDriverElement findWebDriverElement; 
    public JavaScriptCalls javaScriptCalls; 
    public OperateOnWebDriverElement operateOnWebDriverElement; 
    **public RemoteWebDriver driver;** 
    // how to use this driver object across multiple unit test classes 

    public string baseURL; 

    public void SeleniumSetup() 
    { 
     try 
     { 
      Console.WriteLine("Starting Driver..........."); 
      seleniumTestInitalize = new SeleniumTestInitialize(); 
      var options = new InternetExplorerOptions 
      { 
       IntroduceInstabilityByIgnoringProtectedModeSettings = true, 
       //ForceCreateProcessApi=true 
       EnableNativeEvents = false, 
       RequireWindowFocus = false, 
       IgnoreZoomLevel = true 
      }; 
      driver = new InternetExplorerDriver(TestContext.DeploymentDirectory, options); 
      javaScriptCalls = new JavaScriptCalls(driver); 
      findWebDriverElement = new FindWebDriverElement(javaScriptCalls); 
      operateOnWebDriverElement = new OperateOnWebDriverElement(findWebDriverElement); 
      GoToSite(ConfigParameters.WEB_APPLICATION_URL); 
      driver.Manage().Window.Maximize(); 
     } 
     catch (Exception e) 
     { 
      log.Debug("Error Starting Web Driver..........."); 
      Console.WriteLine(e.StackTrace); 
     } 

    } 

    public bool SeleniumInitalizeCheck() 
    { 
     if (seleniumTestInitalize != null) 
      return true; 
     else 
      return false; 
    } 

    public void SeleniumQuit() 
    { 
     Console.WriteLine("Quitting Driver..........."); 
     try 
     { 
      if (driver != null) 
      { 
       driver.Quit(); 
      } 

      log.Info("Closing Web Driver..........."); 
      ProcessMgn.killProcessByNames("IEDriverServer");//Make sure the process is killed 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.StackTrace); 
     } 
    } 

    public void GoToSite(string urlToOpen) 
    { 
     driver.Navigate().GoToUrl(urlToOpen); 
    } 
} 

文件夾com.main.tests

Test01.cs

[TestClass] 
public class Test01 : SeleniumTestInitialize 
{ 

    [TestInitialize] 
    public void Setup() 
    { 
     SeleniumExecutionTerminateFlag = false; 

     if (!SeleniumInitalizeCheck()) 
     { 
      SeleniumSetup(); 
     } 
    } 

    [TestCleanup] 
    public void TearDown() 
    { 
     if (SeleniumExecutionTerminateFlag) 
     { 
      SeleniumQuit(); 
     } 
    } 

    [TestMethod] 
    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.TestCase", "http://tfsserver:8080/tfs/PoL;project", "1320", DataAccessMethod.Sequential)] 
    public void UCP002_M1() 
    { 
     var userName = this.TestContext.DataRow["UserName"].ToString(); 
     var passWord = this.TestContext.DataRow["PassWord"].ToString(); 
     //use the local host adress for your project here-> 
     baseURL = this.TestContext.DataRow["URL"].ToString(); 



     driver.Navigate().GoToUrl(baseURL); 

     //driver.FindElement(By.XPath("//html/body/div[2]/div/a/p/desc")).Click(); 
     //driver.FindElement(By.Id("registerLink")).Click(); 
     driver.FindElement(By.Id("ctl00_LoginTextBox")).Clear(); 
     driver.FindElement(By.Id("ctl00_LoginTextBox")).SendKeys(userName); 
     driver.FindElement(By.Id("ctl00_PasswordTextbox")).Clear(); 
     driver.FindElement(By.Id("ctl00_PasswordTextbox")).SendKeys(passWord); 
     driver.FindElement(By.Id("ctl00_LogInButton")).Click(); 
    } 


} 

Test02.cs

[TestClass] 
public class Test02 : SeleniumTestInitialize 
{ 

    [TestInitialize] 
    public void Setup() 
    { 
     SeleniumExecutionTerminateFlag = false; 

     if (!SeleniumInitalizeCheck()) 
     { 
      SeleniumSetup(); 
     } 
    } 

    [TestCleanup] 
    public void TearDown() 
    { 
     if (SeleniumExecutionTerminateFlag) 
     { 
      SeleniumQuit(); 
     } 
    } 

    [TestMethod] 
    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.TestCase", "http://tfsserver:8080/tfs/PoL;project", "1320", DataAccessMethod.Sequential)] 
    public void Test02() 
    { 
     //some test script 
    } 


} 

我已經創建d 有序測試並按執行順序對測試進行優先級排序。但它調用驅動程序的兩個實例,意味着兩倍的瀏覽器。

我的問題是如何在所有硒單元測試中共享單個驅動程序對象?在開始時創建並在最後關閉驅動程序。 謝謝。

+0

下面是我在JUnit4中如何做到這一點,也許它可能會給你一個想法。 https://sourceforge.net/p/sebase/code/HEAD/tree/sebase-groovy/src/test/groovy/net/sourceforge/sebase/demos/OneBrowserTest.groovy – SiKing 2014-11-14 16:28:08

回答

1

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

您可以在此線程,在那裏我回答我是如何做到了一看:How to run multiple test methods in same browser instance without closing it (C#, SeleniumWebDriverz NUnit)?

基本上,我用:

using Microsoft.VisualStudio.TestTools.UnitTesting; 

相反的:

using NUnit.Framework; 

所以現在我有下一個等級:

[TestFixture] 
[TestFixtureSetup] // this is where I initialize my WebDriver " new FirefoxDriver(); " 
    [Test] //first test 
    [Test] //second test 
    [Test] //third test 
[TestFixtureTearDown] // this is where I close my driver 

通過此更改,我的瀏覽器將只打開一次TestFixture(或TestClass,如果您使用「使用Microsoft.VisualStudio.TestTools.UnitTesting;」)並且該夾具中的所有[Test] -s將在同一個瀏覽器實例中運行。所有測試完成後,瀏覽器將關閉。

希望這將有助於未來的其他人。詢問我是否需要其他幫助。

0

我用NUnit的框架:

using NUnit.Framework; 

然後我建立了我的webdriver的初始化,測試和拆除這樣的:

[TestFixture()] 
class NUnitSeleniumTests 
{ 
    [OneTimeSetUp] 
    public void Init() 
    { 
     driverIE = new InternetExplorerDriver(ConfigurationManager.AppSettings["IEDriver"]); 
     driverIE.Manage().Window.Maximize(); 

     // other setup logic 
    } 

    [Test] 
    public void TestMethod1() 
    { 
     // Test logic 
    } 

    [Test] 
    public void TestMethod2() 
    { 
     // Test logic 
    } 

    ... 
    ... 
    ... 

    [Test] 
    public void TestMethodN() 
    { 
     // Test logic 
    } 


    [OneTimeTearDown] 
    public void Close() 
    { 
     driverIE.Close(); 
    } 
} 

當我運行所有測試,WebDriver驅動程序IE都被初始化。然後,在WebDriver實例在測試運行結束時關閉之前,所有測試都將在該WebDriver實例中執行。

默認情況下,測試按字母順序執行;每個測試也可以單獨執行。

相關問題