当运行起来我的测试失败,但单独通过 [英] My test fail when run together, but pass individually
问题描述
快速的问题......当我写在Visual Studio的测试,我检查它的工作原理是节能,建筑,然后在NUnit的运行测试(右键单击测试,然后运行)。
Quick question for you... 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).
测试工作耶...
,所以我继续......
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的呈现这样的:
Sse.Bec.Web.Tests.Fixtures.ManageSitesAndUsers.ChangeOfPremises.AdminNavigateToChangeOfPremises.AdminNavigateToPageChangeOfPremisesViaMenu:
OpenQA.Selenium.NoSuchElementException:元素无法找到
Sse.Bec.Web.Tests.Fixtures.ManageSitesAndUsers.ChangeOfPremises.AdminNavigateToChangeOfPremises.AdminNavigateToPageChangeOfPremisesViaMenu: OpenQA.Selenium.NoSuchElementException : The element could not be found
这行被突出显示:
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!
推荐答案
两件事可以尝试
- 把下面的两条线之间的断点。而看到这页你当第二线打在
-
通过介绍Thread.sleep代码
- 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
Driver.FindElement(By.Id(submenuitem4))点击();
VAR headerelement = Driver.FindElement(By.ClassName(头));
Driver.FindElement(By.Id("submenuitem4")).Click(); var headerelement = Driver.FindElement(By.ClassName("header"));
这篇关于当运行起来我的测试失败,但单独通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!