热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

每个测试方法都有新的WebDriver实例?-NewWebDriverinstancepertestmethod?

WhatsthebestpracticefortcreatingwebdriverinstancesinSelenium-webdriver?Oncepertestmeth

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

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

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

它们似乎相当(非常)昂贵,但在测试之间保持开放可能会在测试方法之间泄漏信息。

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?

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

Thanks Matt

谢谢马特

2 个解决方案

#1


14  

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.

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

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.

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

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

如果要重用浏览器实例,那么你应该做的一件事就是在运行之间清除/存储COOKIE。

driver.manage().deleteAllCOOKIEs();

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.

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

#2


6  

If your goal of automated integration testing is to have reproducible tests, then I would recommend a new webdriver instance for every test execution.

如果您的自动化集成测试的目标是进行可重复的测试,那么我建议每个测试执行一个新的webdriver实例。

Each test should stand alone, independent from any other test or side-effects.

每项测试都应该独立,独立于任何其他测试或副作用。

Personally the only thing I find more frustrating than a hard to reproduce bug, is a non-deterministic test that you don't trust.

就个人而言,我唯一觉得比难以复制的bug更令人沮丧的是,你不信任的非确定性测试。

(This becomes even more crucial for managing the test data itself, particularly when you look at tests which can modify persistent application state, like CRUD operations.)

(这对于管理测试数据本身更为重要,特别是当您查看可以修改持久应用程序状态的测试时,例如CRUD操作。)

Yes, additional test execution time is costly, but it is better then spending the time debugging your tests.

是的,额外的测试执行时间是昂贵的,但最好是花时间调试测试。

Some possible solutions to help offset this penalty is to roll your testing directly into your build process, going beyond Continuous Build to Continuous Integration approach.

一些可能的解决方案可以帮助抵消这种损失,将测试直接推送到构建过程中,超越了持续构建到持续集成的方法。

Also try to limit the scope of your integration tests. If you have a lot of heavy integration tests, eating up execution time, try to refactor. Instead, increase the coverage of your more lightweight unit tests of the underlying service calls (where your business logic is).

还尝试限制集成测试的范围。如果你有很多繁重的集成测试,耗费执行时间,试着重构。相反,增加底层服务调用(业务逻辑所在的位置)的更轻量级单元测试的覆盖范围。


推荐阅读
author-avatar
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有