1

我一直在RemoteWebDriver使用 Selenium Grid 对 Selenium 2 进行大量阅读。目前我的测试是在Test()使用[Test]属性中生成的。

我有一个TestCriteria类,我填写了信息,然后使用 Selenium WebDriver '页面对象设计模式' 作为一种“控制”如何将这些数据输入到我的测试中的方式。

所以,我有一个简单的标准对象,例如:

public class Credentials
{ 
    public string Username { get; set; } 
    public string Password { get; set; }
}

然后在 LoginPage 对象中使用此信息。

public class LoginPage
{
    [FindsByAnnotation]
    public IWebElement UsernameField { get; set; }

    [FindsByAnnotation]
    public IWebelement PasswordField { get; set; }

    [FindsByAnnotation]
    public IWebelement SubmitButton { get; set; }

    public HomePage Login(Credentials cred)
    {
         UsernameField.SendKeys(cred.user);
         // code for login and return new HomePage object
    }
}

现在,有了这种结构,一旦我有了测试数据,我就可以在我的测试中创建一些好的方法链接,比如我的 Credentials 对象,需要在其他页面上填写的数据等。

[TestFixture]
public class TestFixture
{
     private IWebDriver driver;
     private TestCaseData data; // Which contains a Credentials object etc etc

     [SetUp]
     public void SetUp()
     {
         data = new TestCaseData() 
         { 
              Credentials = new Credentials() 
              { 
                   Username = "username", 
                   Password = "password"
              }
             // Other test case data objects can be populated here 
         };

         driver = new FireFoxDriver();
     }

     [Test]
     public void Test()
     {
          new LoginPage().Login(data.Credentials)
                         .NavigateToSearchPage()
                         .EnterSearchCriteria(data.SearchCritiera)
          // etc
     }

}

这一切都很好,但是......如果我想从可以从 XML 反序列化的序列化 TestData 对象中加载这个测试数据,该怎么办?

我也对使用 感兴趣RemoteWebDriver,我已经使用过,但是与使用相比仍然很脆弱IWebDriver driver = new FireFoxDriver();,但是忽略这些问题,我真的很想多次运行 TESTCASE ......同时。这让我想到了并行测试的问题。

我知道 MBUnit 可以处理这是 Gallio 的一部分,我还研究了 PUnit,它是 Nunit 的扩展,但它还有很长的路要走。所以我决定坚持使用 MbUnit。

这使我能够使用属性运行 TestFixtures [Parallelizable]。因此,如果我编译我的项目并将项目 dll 加载到 Gallio TestRunner GUI 中,我可以同时运行 4 个测试用例,这反过来又会打开 4 个浏览器同时运行每个测试,也可以给网站施加压力当所有 4 个测试都在运行时。(显然,这将增加为在多台机器上使用 selenium RemoteWebDriver 和 Selenium Grid 运行数百个浏览器。

这里有谁知道我可以加载 xml 序列化对象集合的方法,生成新的 TestFixture 对象,这些对象可以将[Parallelizable]属性添加到测试夹具的顶部,并在加载 1-10 后让它们同时运行.xml 文件,例如:C:\TestCase 目录?

我的想法是加载所有这些,然后让 Selenium Grid 处理浏览器会话,其中 1 - 5 个 Selenium 节点运行连接到主 selenium 网格集线器。

当我找不到允许我生成 a 的框架时,我很难真正利用 Selenium Grid Test Fixture,其中包括 a[SetUp] [TearDown] [Test]以及根据从 TestCase .xml 加载的内容为测试属性设置某些测试条件的能力文件。

例如,如果 TestCase .xml 文件有一个失败的元素,那么我如何加载这个 .xml 文件并为此在我的 TestFixture 的 [Test] 上设置一个属性,或者将 TestCase .xml 描述映射到[Test(Description = "")]属性运行。

我已经知道 selenium webdriver 的功能、页面对象设计模式、屏幕截图的 seleniumEventFiringWebDriver功能。

我如何利用加载几个 XML 序列化 TestCaseData 的能力,在加载这些对象后生成新的 TestFixture 对象?

理想情况下,我希望每个 TestFixture 的 [SetUp] 进行设置,IWebDriver因为某些 URL 会根据 TestCase.xml 文件包含的内容而有所不同,例如,此测试是在 UAT 环境、测试环境、预生产中运行环境,我需要在测试运行之前进行设置。

有没有人有一个基本示例,它将这些主要概念与 Selenium Grid/Selenium WebDriver 一起使用,并能够并行运行这些测试装置以利用运行多个浏览器的 Selenium Grid 功能。

所以我在伪代码中寻找一些东西。

public class Main()
{
   // Load Testfixtures
   List<TestCase> testCases = Deserialise("\\Some\\FolderLocation");

   foreach(test in testCases)
   { 
      // create NEW testFixture, not [Test]
      // ability to attach parallel TestFixture
   }  
}

[Testfixture]
[Parallelizable]
public class TestFixture
{

     public TestCase testCase { get; set; }
     public IWebDriver driver { get; set; }

     [SetUp]
     public void SetUp()
     {
         if(testCase.Environment == "UAT")
         {
             driver = new FireFoxDrive()
             driver.NavigateTo("http://localhost/uat/Login");
         }

         // What if the testCase.ShouldPass is false?

         // How can i generate my [Test] with ExpectedException?

     }

      [Test]
      public void Test()
      {
          // code here with page object method chaining passing in testCase data objects  
      }
}

也许我正在以错误的方式进行这个设计。请对并行化这些的最佳方法有任何建议吗?

4

2 回答 2

1

这个怎么样?

public class TestCase
{
    public string Name { get; set; }
}

public static class TestCaseFactory
{
    public static IEnumerable<TestCase> TestCases()
    {
        yield return new TestCase { Name = "one" };
        yield return new TestCase { Name = "two" };
    }
}

[Factory(typeof(TestCaseFactory), "TestCases"), Parallelizable]
public class DataDrivenFixture
{
    private readonly TestCase testCase;

    public DataDrivenFixture(TestCase testCase)
    {
        this.testCase = testCase;
    }

    [SetUp]
    public void SetUp()
    {
        Console.WriteLine("SetUp " + testCase.Name);
    }

    [Test]
    public void Test()
    {
        Console.WriteLine("Test " + testCase.Name);
    }
}
于 2012-02-24T11:49:46.520 回答
0

很好的解决方案。但是在并行的不同浏览器中运行测试呢?如果我在 [FixtureSetUp] 中初始化 IWebDriver,则浏览器打开并运行测试但出现错误(基本上是 NoSuchElement)。

public static IEnumerable<TestCase> TestCases()
        {
            yield return new TestCase { Name = "internet explorer" };
            yield return new TestCase { Name = "firefox" };
        }

[FixtureSetUp]
        public void SetUp()
        {
            Console.WriteLine("SetUp " + testCase.Name);
            switch (testCase.Name)
            {
                case "internet explorer":
                    driver = new InternetExplorerDriver();
                    break;
                case "chrome":
                    driver = new ChromeDriver();
                    break;
                case "firefox":
                    driver = new FirefoxDriver();
                    break;
            }
        }
于 2012-05-15T10:22:45.580 回答