2016-10-03 42 views
0

後TestCleanup()退出測試的其餘部分我自動化我的GitHub的個人資料和以下是我的測試案例:硒UI測試:第一次測試加載

  • 加載瀏覽器(這是在testInitialize定義()
  • 載入URL
  • 進行登錄 下面的代碼片段:

命名空間GitAutomationTest { 我們Microsoft.VisualStudio.TestTools.UnitTesting;使用OpenQA.Selenium.IE的 ;使用OpenQA.Selenium.Remote的 ;使用系統的 ; [TestClass] public class GitTest { private string baseURL =「https://github.com/login」; 私人RemoteWebDriver驅動程序; public TestContext TestContext {get;組; }

 [TestMethod] 
    public void LoadURL() { 
      driver.Navigate().GoToUrl(baseURL); 
      Console.Write("Loaded URL is :" + baseURL); 
     } 
     [TestMethod] 
     public void PerformLogin() { 
      driver.FindElementById("login_field").SendKeys("USERNAME"); 
      driver.FindElementById("password").SendKeys("PASSWORD"); 
      Console.Write("password entered \n "); 
      driver.FindElementByClassName("btn-primary").Click(); 
      driver.GetScreenshot().SaveAsFile(@"screenshot.jpg", format: System.Drawing.Imaging.ImageFormat.Jpeg); 
      Console.Write("Screenshot Saved: screenshiot.jpg"); 
     } 
     [TestCleanup()] 
     public void MyTestCleanup() 
     { 
      driver.Quit(); 
     } 
     [TestInitialize()] 
     public void MyTestInitialize() 
     { 
      driver = new InternetExplorerDriver(); 
      driver.Manage().Window.Maximize(); 
      Console.Write("Maximises The window\n"); 
      driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(20)); 
     } 
    } 
} 

輸出
每次我運行所有測試: - 測試初始化​​:在Internet Explorer中被加載 - 基準url加載 - 之後,駕駛員與TestCleanUP()退出

下一次驅動程序運行testperformLogin() - 測試無法找到執行登錄的用戶名和密碼元素,因爲此時基礎URL未加載。

我們如何管理TestInitialize()類這樣: - 瀏覽器是用的BaseURL直到所有測試完成。 我們如何管理TestCleanup(): - 只有在所有測試完成後,瀏覽器纔會關閉。

+0

爲什麼不試試Nunit測試框架?請看看它。 http://nunit.org/index.php?p=docHome&r=2.6.4 – Aishu

回答

0

有一個AssemblyCleanup屬性在所有測試執行後運行。

你可以在這裏找到更多關於屬性的信息 - Unit Testing Framework

0

您需要將下面的代碼爲「PerformLogin」測試方法

driver.Navigate().GoToUrl(baseURL); 

或另一種方法是在「Mytestinitialize」方法,下面的代碼添加和刪除「使用loadURL」方法

driver.Navigate().GoToUrl(baseURL); 

由於[TestInitialize]在每個[TestMethod]之後每調用[TestMethod]並調用[TestCleanup]之前調用,您都面臨這個問題。

在你的情況下,「LoadURL」測試能夠獲取URL,但「PerformLogin」無法獲取該URL,因爲它未在「MyTestInitialize」中提及。