C# 一起运行时测试失败,单独运行时通过
当我使用TestFixture使用nunit实现跨浏览器测试时,我的测试在一起运行时失败,在单独运行时通过。调用SendKeys方法时抛出异常,因为参数为null,但这不是原因,因为当我再次运行此测试时,测试将通过。当然,我试图调试这个问题,但我没有找到解决办法。简单的OpenHomePage测试工作正常。这是我的密码:C# 一起运行时测试失败,单独运行时通过,c#,selenium,webdriver,nunit,ddt,C#,Selenium,Webdriver,Nunit,Ddt,当我使用TestFixture使用nunit实现跨浏览器测试时,我的测试在一起运行时失败,在单独运行时通过。调用SendKeys方法时抛出异常,因为参数为null,但这不是原因,因为当我再次运行此测试时,测试将通过。当然,我试图调试这个问题,但我没有找到解决办法。简单的OpenHomePage测试工作正常。这是我的密码: [TestFixture(typeof(ChromeDriver))] [TestFixture(typeof(FirefoxDriver))] public class Te
[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());
}
[TestFixture(typeof(ChromeDriver))]
[测试夹具(类型(FirefoxDriver))]
公共类TestClass,其中TWebDriver:IWebDriver,new()
{
[一次性设置]
public void CreateDriver()
{
尝试
{
PropertiesCollection.driver=new TWebDriver();
Console.WriteLine(“打开的浏览器”);
PropertiesCollection.driver.Url=”http://localhost:81/";
Console.WriteLine(“打开的URL”);
PropertiesCollection.driver.Manage().Window.Maximize();
//从excel工作表初始化测试数据
ExcelLib.PopulateInCollection(@“c:\users\bolec\documents\visual studio 2015\Projects\RowingSectionTests\RowingSectionTests\TestData.xlsx”);
}
捕获(异常消息)
{
Console.WriteLine(msg.ToString());
}
}
[onetimeeartown]
public void FixtureTearDown()
{
HomePageObjects homebj=新的HomePageObjects();
homeObj.Logoff();
如果(PropertiesCollection.driver!=null)PropertiesCollection.driver.Quit();
}
[撕裂]
公共无效拆卸()
{
//对失败进行筛选
if(TestContext.CurrentContext.Result.output.Status.Equals(TestStatus.Failed))
{
字符串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);
}
}
//永远都会过去
[测试]
公开网页(
{
HomePageObjects homebj=新的HomePageObjects();
}
//使用正确的凭据登录将登录到acc
[测试]
公共无效登录()
{
HomePageObjects homebj=新的HomePageObjects();
LoginPageObjects loginObj=homeObj.ToLoginPage();
loginObj.Login(ExcelLib.ReadData(1,“用户名”)、ExcelLib.ReadData(1,“密码”);
//登录后检查URL是否正确
Assert.AreEqual(“http://localhost:81/,PropertiesCollection.driver.Url.ToString());
//在导航栏上检查登录是否正确
Assert.AreEqual(homobj.GetUserLoginStringInButton().ToLower(),ExcelLib.ReadData(1,“用户名”).ToLower());
}
使用static PropertiesCollection的问题是,在一个测试中对静态类的任何更改都会反映在另一个测试中,这使得测试相关性非常高(正如您所发现的)
您有两个选择,首先,不要使用静态方法,而是创建实例。或者,确保在安装和拆卸方法中,将PropertiesCollection设置/重置回其所需状态
使用OneTimeSetUp属性也有风险,因为它只对夹具中的所有测试运行一次。no。因为他使用的是NUnit V3(否则它将无法编译)OneTimeSetUp和OneTimeEardown是正确的。TestFixtureXxxxxXx属性在V3中被弃用,但会做同样的事情。您启用了并行执行吗?如果启用了,这两个装置可能相互干扰。PropertiesCollection.driver是静态的吗?如果是,您有两个不同的装置使用相同的数据。肯定不会工作k并行。如果不是并行的,每个测试都需要在自身之后进行清理。是的,它是静态的。因此,也许你可以引导我一些解决方案?是的…不要使用静态的。:-)创建一个单独的集合,用于夹具每个实例的OneTimeSetUp方法。