[英]Why do my tests fail when run together, but pass individually?
When I write a test in Visual Studio, I check that it works by saving, building and then running the test it in Nunit (right click on the test then run).当我在 Visual Studio 中编写测试时,我通过保存、构建然后在 Nunit 中运行测试来检查它是否有效(右键单击测试然后运行)。
The test works yay... so I Move on...测试有效...所以我继续...
Now I have written another test and it works as I have saved and tested it like above.现在我已经编写了另一个测试,它可以像上面一样保存和测试它。 But, they dont work when they are run together.
但是,当它们一起运行时,它们不起作用。
Here are my two tests that work when run as individuals but fail when run together:这是我的两个测试,它们在单独运行时有效,但在一起运行时失败:
using System;
using NUnit.Framework;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium;
namespace Fixtures.Users.Page1
{
[TestFixture]
public class AdminNavigateToPage1 : SeleniumTestBase
{
[Test]
public void AdminNavigateToPage1()
{
NavigateTo<LogonPage>().LogonAsCustomerAdministrator();
NavigateTo<Page1>();
var headerelement = Driver.FindElement(By.ClassName("header"));
Assert.That(headerelement.Text, Is.EqualTo("Page Title"));
Assert.That(Driver.Url, Is.EqualTo("http://localhost/Page Title"));
}
[Test]
public void AdminNavigateToPage1ViaMenu()
{
NavigateTo<LogonPage>().LogonAsCustomerAdministrator();
Driver.FindElement(By.Id("menuitem1")).Click();
Driver.FindElement(By.Id("submenuitem4")).Click();
var headerelement = Driver.FindElement(By.ClassName("header"));
Assert.That(headerelement.Text, Is.EqualTo("Page Title"));
Assert.That(Driver.Url, Is.EqualTo("http://localhost/Page Title"));
}
}
}
When the second test fails because they have been run together当第二个测试因为它们一起运行而失败时
Nunit presents this: Nunit 介绍了这一点:
Sse.Bec.Web.Tests.Fixtures.ManageSitesAndUsers.ChangeOfPremises.AdminNavigateToChangeOfPremises.AdminNavigateToPageChangeOfPremisesViaMenu: OpenQA.Selenium.NoSuchElementException : The element could not be found
Sse.Bec.Web.Tests.Fixtures.ManageSitesAndUsers.ChangeOfPremises.AdminNavigateToChangeOfPremises.AdminNavigateToPageChangeOfPremisesViaMenu:OpenQA.Selenium.NoSuchElementException:找不到元素
And this line is highlighted:此行突出显示:
var headerelement = Driver.FindElement(By.ClassName("header"));
Does anyone know why my code fails when run together, but passes when run alone?有谁知道为什么我的代码一起运行时失败,但单独运行时通过?
Any answer would be greatly appreciated!任何答案将不胜感激!
Such a situation normally occurs when the unit tests are using shared resources/data in some way.当单元测试以某种方式使用共享资源/数据时,通常会发生这种情况。
If none of the answers above worked for you, i solved this issue by adding Thread.Sleep(1)
before the assertion in the failing test...如果以上答案都不适合您,我通过在失败测试中的断言之前添加
Thread.Sleep(1)
解决了这个问题......
Looks like tests synchronization is missed somewhere... Please note that my tests were not order dependant, that i haven't any static member nor external dependency.看起来测试同步在某处丢失了......请注意,我的测试不依赖于顺序,我没有任何静态成员或外部依赖。
Two things you can try你可以尝试两件事
put the break point between the following two lines.将断点放在以下两行之间。 And see which page are you in when the second line is hit
并在第二行被击中时查看您在哪个页面
Introduce a slight delay between these two lines via Thread.Sleep通过 Thread.Sleep 在这两行之间引入一点延迟
Driver.FindElement(By.Id("submenuitem4")).Click(); var headerelement = Driver.FindElement(By.ClassName("header"));
Without knowing how Selenium works, my bet is on Driver
which seems to be a static class so the 2 tests are sharing state.在不知道 Selenium 是如何工作的情况下,我的赌注是
Driver
,它似乎是一个静态类,所以 2 个测试共享状态。 One example of shared state is Driver.Url
.共享状态的一个示例是
Driver.Url
。 Because the tests are run in parallel, there is a race condition to set the state of this object.因为测试是并行运行的,所以有一个竞争条件来设置这个对象的状态。
That said, I do not have a solution for you :)也就是说,我没有适合你的解决方案:)
look into the TestFixtureSetup , Setup , TestFixtureTearDown and TearDown .查看TestFixtureSetup 、 Setup 、 TestFixtureTearDown和TearDown 。
These attributes allow you to setup the testenvironment once, instead of once per test.这些属性允许您设置一次测试环境,而不是每次测试一次。
I think you need to ensure, that you can log on for the second test, this might fail, because you are logged on already?我认为您需要确保您可以登录进行第二次测试,这可能会失败,因为您已经登录了?
-> putting the logon in a set up method or (because it seems you are using the same user for both tests) even up to the fixture setup -> the logoff (if needed) might be put in the tear down method -> 将登录放在设置方法中,或者(因为看起来你在两个测试中使用相同的用户)甚至到夹具设置-> 注销(如果需要)可能会放在拆卸方法中
[SetUp]
public void LaunchTest()
{
NavigateTo<LogonPage>().LogonAsCustomerAdministrator();
}
[TearDown]
public void StopTest()
{
// logoff
}
[Test]
public void Test1()
{...}
[Test]
public void Test2()
{...}
If there are delays in the DOM instead of a thread.sleep I recommend to use webdriver.wait in combination with conditions.如果 DOM 有延迟而不是 thread.sleep,我建议结合条件使用 webdriver.wait。 The sleep might work in 80% and in others not.
睡眠可能在 80% 中起作用,而在其他情况下则不起作用。 The wait polls until a timeout is reached which is more reliable and also readable.
等待轮询直到达到更可靠且可读的超时。 Here an example how I usually approach this:
这是一个我通常如何处理的示例:
var webDriverWait = new WebDriverWait(webDriver, ..);
webDriverWait.Until(d => d.FindElement(By.CssSelector(".."))
.Displayed))
I realize this is an extremely old question but I just ran into it today and none of the answers addressed my particular case.我意识到这是一个非常古老的问题,但我今天才遇到它,没有一个答案能解决我的特殊情况。
Using Selenium with NUnit for front end automation tests.使用 Selenium 和 NUnit 进行前端自动化测试。
For my case I was using in my startup [OneTimeSetUp]
and [OneTimeTearDown]
trying to be more efficient.对于我的情况,我在启动时使用
[OneTimeSetUp]
和[OneTimeTearDown]
试图提高效率。
This however has the problem of using shared resources, in my case the driver itself and the helper I use to validate/get elements.然而,这存在使用共享资源的问题,在我的情况下是驱动程序本身和我用来验证/获取元素的助手。
Maybe a strange edge case - but took me a few hours to figure it out.也许是一个奇怪的边缘案例——但我花了几个小时才弄清楚。
Are you sure that after running one of the tests the method您确定在运行其中一项测试后该方法
NavigateTo<LogonPage>().LogonAsCustomerAdministrator();
is taking you back to where you should be?是带你回到你应该去的地方吗? It'd seem that the failure is due to improper navigation handler (supposing that the header element is present and found in both tests).
失败似乎是由于导航处理程序不当造成的(假设在两个测试中都存在并找到了 header 元素)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.