2016-04-14 138 views
1

当我使用TestFixture实现与nunit的跨浏览器测试时,我的测试一起运行时失败,单独运行时通过。当SendKeys方法被调用时抛出异常,因为参数为null,但这不是原因,因为当我再次运行此测试时,测试会通过。当然我试图调试这个问题,但我没有找到解决办法。简单的OpenHomePage测试工作正常。这里是我的代码:测试运行时失败,单独运行时通过

[TestFixture(typeof(ChromeDriver))] 
[TestFixture(typeof(FirefoxDriver))] 
public class TestClass<TWebDriver> where TWebDriver : IWebDriver, new() 
{ 
    [OneTimeSetUp] 
    public void CreateDriver() 
    { 
     try 
     { 
      PropertiesCollection.driver = new TWebDriver(); 
      Console.WriteLine("Opened browser"); 
      PropertiesCollection.driver.Url = "http://localhost:81/"; 
      Console.WriteLine("Opened URL"); 
      PropertiesCollection.driver.Manage().Window.Maximize(); 
      //initialize test data from excel sheet 
      ExcelLib.PopulateInCollection(@"c:\users\bolec\documents\visual studio 2015\Projects\RowingSectionTests\RowingSectionTests\TestData.xlsx"); 
     } 
     catch (Exception msg) 
     { 
      Console.WriteLine(msg.ToString()); 
     }         
    } 

    [OneTimeTearDown] 
    public void FixtureTearDown() 
    { 
     HomePageObjects homeObj = new HomePageObjects(); 
     homeObj.Logoff(); 
     if (PropertiesCollection.driver != null) PropertiesCollection.driver.Quit(); 
    } 

    [TearDown] 
    public void TearDown() 
    { 
     //Take screen on failure 
     if (TestContext.CurrentContext.Result.Outcome.Status.Equals(TestStatus.Failed)) 
     { 
      string fileName = Regex.Replace(TestContext.CurrentContext.Test.FullName + "_" + DateTime.Now.ToString(), "[^a-z0-9\\-_]+", "_", RegexOptions.IgnoreCase); 
      ((ITakesScreenshot)PropertiesCollection.driver).GetScreenshot().SaveAsFile(@"c:\users\bolec\documents\visual studio 2015\Projects\RowingSectionTests\RowingSectionTests\Screenshots\" + fileName + ".png", System.Drawing.Imaging.ImageFormat.Png); 
     } 
    } 

    //will always passed 
    [Test] 
    public void OpenHomePage() 
    {   
     HomePageObjects homeObj = new HomePageObjects(); 
    } 

    //login with correct credentials will login to acc 
    [Test] 
    public void Login() 
    {    
     HomePageObjects homeObj = new HomePageObjects(); 
     LoginPageObjects loginObj = homeObj.ToLoginPage(); 
     loginObj.Login(ExcelLib.ReadData(1, "UserName"), ExcelLib.ReadData(1, "Password")); 

     //checking is URL correct after loggin 
     Assert.AreEqual("http://localhost:81/", PropertiesCollection.driver.Url.ToString()); 
     //checking is login is correct on navbar 
     Assert.AreEqual(homeObj.GetUserLoginStringInButton().ToLower(), ExcelLib.ReadData(1, "UserName").ToLower()); 
    } 
+0

没有。由于他使用的是NUnit V3(否则它不会编译)OneTimeSetUp和OneTimeTearDown是正确的。 V3中的TestFixtureXxxxxXx属性已被弃用,但会做同样的事情。 – Charlie

+0

您是否启用了并行执行?如果是这样,这两个固定装置可能会相互干扰。 – Charlie

+0

是PropertiesCollection.driver一个静态?如果是这样,你有两个不同的灯具使用相同的数据。绝对不会并行工作。如果不是平行的,每个测试需要自行清理。 – Charlie

回答

1

利用静态PropertiesCollection的问题是,在一个测试,以静态类的任何更改将反映在其他测试,使得机会或创建一个测试的依赖非常高(如你已经发现)。

你有两种选择,首先不要使用静态代替创建实例。或者,确保在设置和拆卸方法中,您将PropertiesCollection设置为/重置为所需状态。

使用OneTimeSetUp属性也是有风险的,因为它只对夹具中的所有测试运行一次。

相关问题