每个测试方法都有新的 WebDriver 实例? [英] New WebDriver instance per test method?

查看:24
本文介绍了每个测试方法都有新的 WebDriver 实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Selenium-webdriver 中创建 webdriver 实例的最佳实践是什么?每个测试方法、每个测试类或每个测试运行一次?

What's the best practice fort creating webdriver instances in Selenium-webdriver? Once per test method, per test class or per test run?

它们启动起来似乎相当(非常!)昂贵,但在测试之间保持开放可能会在测试方法之间泄露信息.

They seem to be rather (very!) expensive to spin up, but keeping it open between tests risks leaking information between test methods.

或者是否有替代方案 - 单个 webdriver 实例是单个浏览器窗口(不包括弹出窗口),还是有从给定驱动程序实例启动新窗口/会话的方法?

Or is there an alternative - is a single webdriver instance a single browser window (excluding popups), or is there a method for starting a new window/session from a given driver instance?

谢谢马特

推荐答案

我发现在使用真实浏览器时,在测试方法之间重用浏览器实例可以节省大量时间,例如火狐.使用 HtmlUnitDriver 运行测试时,几乎没有什么好处.

I've found that reusing browser instances between test methods has been a huge time saver when using real browsers, e.g. Firefox. When running tests with HtmlUnitDriver, there is very little benefit.

关于不确定性测试的危险性,这是完全确定性测试和您的时间之间的权衡.集成测试通常涉及这样的权衡.如果您想要完全确定性的集成测试,您还应该担心在测试运行之间清除数据库/服务器状态.

Regarding the danger of indeterministic tests, it's a trade-off between totally deterministic tests and your time. Integration tests often involve trade-offs like these. If you want totally deterministic integration tests you should also be worrying about clearing the database/server state in between test runs.

如果您要重用浏览器实例,您绝对应该做的一件事是在运行之间清除/存储 cookie.

One thing that you definitely should do if you are going to reuse browser instances is to clear/store the cookies between runs.

driver.manage().deleteAllCookies();

我在一个 tearDown() 方法中做到了这一点.此外,如果您的应用程序在客户端存储任何数据,您需要清除它(可能通过 JavascriptExecutor).对于您正在测试的应用程序,执行此操作后它应该看起来像一个完全不相关的请求,这确实将不确定行为的风险降至最低.

I do that in a tearDown() method. Also if your application stores any data on the client side you'd need to clear that (maybe via JavascriptExecutor). To your application which is under test, it should look like a completely unrelated request after doing this, which really minimizes the risk of indeterministic behaviour.

这篇关于每个测试方法都有新的 WebDriver 实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆