一起运行时测试失败,单独运行时传递

时间:2016-04-14 23:12:04

标签: c# selenium webdriver nunit ddt

当我使用TestFixture使用nunit实现跨浏览器测试时,我的测试在一起运行时失败,在单独运行时传递。调用SendKeys方法时抛出异常,因为参数为null,但这不是原因,因为当我再次运行此测试时,测试将通过。 Ofcourse我试图调试这个问题,但我没有找到解决方案。简单的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());
    }

1 个答案:

答案 0 :(得分:1)

使用静态PropertiesCollection的问题是,在一次测试中对静态类的任何更改都将反映在另一个测试中,从而使得机会或创建测试依赖性非常高(正如您所发现的那样)。

您有两个选择,首先不要使用静态而是创建实例。或者,确保在设置和拆解方法中将PropertiesCollection设置/重置为其所需状态。

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