且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Selenium Webdriver:如何在同一个窗口中一个接一个地运行多个测试?

更新时间:2023-02-01 20:26:23

如果你的测试是依赖的,你可以把它们放在同一个类中,并在method2上定义dependsOnMethod={method1},这样可以保证顺序.如果它在不同的类之间,您可以考虑从 LoginScript 类扩展您的 AddNormalEE 类.

If your tests are dependent, you can put them in the same class with dependsOnMethod={method1} defined on method2 so that the order is gauranteed. If it is between different classes, you might consider extending your AddNormalEE class from the LoginScript class.

要在同一个浏览器中运行测试,您的驱动程序实例需要在您的类之间共享,或者它必须在您的所有@Tests 中都相同.要么将其设为静态,要么考虑使用 threadlocal webdriver 变量,以防您计划有一天并行运行.在上述情况下,您还可以在 loginScript 中使用 getDriver() 方法,如果需要避免静态,则该方法将驱动程序对象返回给 AddNormalEE 类.

To run tests in the same browser, your driver instance needs to be shared between your classes or it has to be the same in all your @Tests. Either make it static or consider having a threadlocal webdriver variable in case you plan to run parallely some day. In the above case, you can also have a method getDriver() in your loginScript which returns the driver object to the AddNormalEE class if static needs to be avoided.

作为一般做法,***进行独立测试.您可以利用并行运行来克服独立测试的时间问题.将登录作为一种方法而不是测试,因为我们没有根据上面的代码断言任何行为.如果我正在测试登录,我将进行单独的测试来仅测试登录功能.

As a general practice, it is good to have independent tests. You can make use of parallel runs to overcome the time issue with independent tests. Making login as a method and not a test since we are not asserting any behavior as per your code above. If I am testing login, I would have separate tests that test login functionality only.