During automated testing of a typical SPA page should the browser be closed after each scenario run by selenium e2e tests or should it remain open?
The pro I see in closing and opening the browser after each larger scenario is that the context is fresh and there should be no problems like caching issues or other dependencies etc. However maybe it's enough to do a hard refresh instead of closing the browser?
The pro in leaving browser window open is the speed of e2e tests in overall - closing browser and starting fresh session always takes time, in our case the difference between both is 20% faster when leaving browser open.
My question is: are there any other potential problems when leaving the browser open and running all tests in one browser session?
-
2I highly recommend starting a new scenario in a new browser.. As far as speed is concern..have you thought of parallelizing your tests to achieve a much faster result?Mrunal Gosar– Mrunal Gosar2016-06-16 09:19:24 +00:00Commented Jun 16, 2016 at 9:19
-
Thanks, can you elaborate why do you highly recommend this? Do you have any other pros than ones suggested by RemcoW?Arek– Arek2016-06-16 11:54:35 +00:00Commented Jun 16, 2016 at 11:54
-
Long running scenarios may occupy some of the resources which may not be able to clear via refresh..also it is highly recommended that scenarios should have their own pre-conditions rather than combining two or more scenarios to create one..Automated tests should always favor independency..also if u r able to clear browser then that says u don't have dependencies in ur test..which suggests u can run ur tests independent and support parallelismMrunal Gosar– Mrunal Gosar2016-06-16 14:11:19 +00:00Commented Jun 16, 2016 at 14:11
2 Answers
I am doing it myself by closing the browser and starting a fresh one each run. Like you said it will provide you with a fresh context. Another reason for this is that I can set custom settings for each testcase. In my case I am performing a lot of downloads. I'm making sure each testcase does the download to its own folder by changing the firefox/chrome settings before each testcase.
Another major reason why I'm closing and opening the browser after each run is that it allows you to run your tests in parallel. This is not possible when using a single browser for all your testcases. Your tests will be even faster than the 20% when you don't close the browser if you run then im parallel.
1 Comment
I hope below options may help you.
- rather than closing entire driver, you can close just the window.
driver.close(). - you can add
driver.manage().deleteAllCookies(); - you can also try incognito
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
capabilities.setCapability("chrome.switches", Arrays.asList("--incognito"));