3

I close my driver and reopen it between each negative test, for example - in my negative login test - if the test failed it means the login succeeded, then I close the driver and open it again so I start from a fresh browser in the login page again.

Is it best practice to do it that way? I thought about some alternatives but I can't decide which one is better -

  1. closing the driver only when I actually need to - for example when the test failed and it logged in.
  2. clear the browser cache and cookies between each test, so it'll simulate new driver.
bad_coder
  • 11,289
  • 20
  • 44
  • 72
amitlisha
  • 140
  • 1
  • 8

3 Answers3

1

Of course, a test has to start with a fresh session or you can get test bugs that will hard to reproduce. You can use Selenoid (https://aerokube.com/selenoid/latest/), you will get a new browser each time faster. To deploy it locally use https://docs.docker.com/docker-for-windows/install/

Ilya Tatsiy
  • 185
  • 2
  • 8
  • every test? for example I'm entering a negative value in a field, and test if it accepts it, let's say it didn't, is it necessary to restart the driver? – amitlisha Jan 17 '21 at 12:32
  • 1
    Yes, you could run your test set in parallel and it will take less time, but still will be reliable. – Ilya Tatsiy Jan 17 '21 at 13:05
1

I would recommend not to restart the browser each time unless it is required in your test to have a fresh instance .

This is because users won't be always using fresh browser instant for each buissnesss requirement flow (Or test case ).

So for end to end test you can use the same browser instance

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • yeah that's what I thought, but how do I decide when I need a fresh browser? – amitlisha Jan 17 '21 at 13:35
  • @Boomer sometimes IN most cases you don' t want . But in cases like you don't want session information like ntlm authentication which cannot be logged out you can use new browser session to validate user is prompted with authentication prompt again – PDHide Jan 17 '21 at 13:39
0

From a broader perspective, while performing or tests the pre-requites of a testcase remains the same. So there is no harm in continuing the tests in the same browsing context.

However, during negative testing it would be a better idea to start each testcase through a fresh browsing context so the tests fails for the proper reason else you may face a lot of False positives and false negatives due to unwanted cookies and other session attributes.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • But it takes so much time, negative testing a login form, let's say I have 10 negative values for each field, that means opening and closing the driver 20 times only for the negative login tests.. isn't that too much? – amitlisha Jan 19 '21 at 08:02
  • And another question, do you think instead of restarting the browser, is it equally good to clear the cache and cookies between tests? – amitlisha Jan 19 '21 at 08:21
  • @amitlisha As I mentioned, the cause of failure for **negative** testcases mustn't deviate from the expected reason. Hence to simulate the negative testcases it will be always advisable to start on a clean slate. The extra time incurred for reopening webdriver/browser sessions will save you from the _False positives_ and _False negatives_. – undetected Selenium Jan 19 '21 at 18:55
  • @amitlisha _...is it equally good to clear the cache and cookies between..._: It's **not** only about the cookies and cache only but also the [**`scoped_dir*`**](https://stackoverflow.com/questions/54151695/selenium-chromedriver-test-execution-leaves-scoped-dir-temp-files-which-makes-t) – undetected Selenium Jan 19 '21 at 19:03