简体   繁体   English

为每个测试用例打开一个新的浏览器是一种好习惯吗? (硒,JUnit)

[英]Is it good practice to open a new browser for every test case? (Selenium, JUnit)

I am using Selenium and JUnit for testing. 我正在使用Selenium和JUnit进行测试。 First time doing testing, so I'm not sure what the best practices are. 第一次进行测试,所以我不确定最佳实践是什么。

For every single test case, do I need to open a new browser or do I set it up so that one test case can pick up where the previous test case left off, and use the browser state of the previous test case? 对于每个单独的测试用例,我是否需要打开一个新的浏览器或进行设置,以便一个测试用例可以从上一个测试用例停止的地方开始,并使用先前测试用例的浏览器状态? Are there some general rules of thumb for this? 是否有一些一般的经验法则?

Initially I was thinking that opening a new browser for every test case would be too inefficient so I was trying to have the test cases run one after the other, but run order cannot be assigned in JUnit.😭 So now I am thinking that I need to make every test case completely independent and have some "set up" lines of code at the beginning of each test case and if applicable use @Before . 最初,我认为为每个测试用例打开一个新的浏览器的效率太低,因此我试图让测试用例一个接一个地运行,但是无法在JUnit中分配运行顺序。😭现在我想我需要使每个测试用例完全独立,并在每个测试用例的开头添加一些“设置”代码行,并在适用时使用@Before

Is a good way to do it? 这是个好方法吗?

  1. In setUpBeforeClass(){} create the browser setUpBeforeClass(){}创建浏览器
  2. Use driver.get("specific url for test case"); 使用driver.get("specific url for test case"); at beginning of every test case. 在每个测试用例的开始。 But this would be bad when changing between environments and changing urls..... 但这在环境之间更改和更改URL时会很不好。

您可以使用以下方法删除测试之间的Cookie,从而节省时间:

driver.manage().deleteAllCookies();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM