Clear cookies for each test in the PhantomJS + GhostDriver + WebDriver client configuration

How to clear cookies for each test in my PhantomJS + GhostDriver + Selenium WebDriver + WebDriver client system?

My test process is as follows:

  • Run selenium-web-driver-standalone as the host.
  • Run phantomjs in webdriver mode and attach it to WebDriver selenium.
  • Run a shell script that iterates over the test suites and runs each.
  • In each test, the webdriver client is used and the connection with the browser is connected to the selenium web driver.

When I use Firefox browser instead of phantomjs, all tests passed fine. But when I switch to using phantomjs as a browser, all the tests that checked the registration were unsuccessful, because the cookies were already set after the first test run. Can I delete all cookies every time I run a test? Or should I restart the phantomjs process on each individual test (how is this done with firefox and selenium webdriver as a non-hub)?

+6
source share
3 answers

Perhaps some kind of offtopic, the reason why the author noted that he uses php to run the test.

But if you came from Google and are interested in a C # solution to clear all cookies, look below:

// driver is fully initialized PhantomJS instance driver.Manage().Cookies.DeleteAllCookies(); 

NuGetPackages required:

PhantomJS 2.0.0

Selenium.Support 2.48.2

Selenium.WebDriver 2.48.2

Tested with .NETFramework v4.5.2

+2
source

You can delete a specific cookie using:

 webDriver.manage().deleteCookieNamed("smSession"); 

And delete all cookies with:

 webDriver.manage().deleteAllCookies(); 
+1
source

I had a similar problem with the fact that GhostDriver was not able to clear localStorage. Try the following guidelines:

After you provide driver.get (URL) to your page, you can execute javascript on this from webdriver, like this

 function clearCookie(name, domain, path){ var domain = domain || document.domain; var path = path || "/"; document.cookie = name + "=; expires=" + +new Date + "; domain=" + domain + "; path=" + path; }; 

There is no 100% solution to delete browser cookies.

The problem is that the cookie uniquely identifies not only their key "name", but also their "domain" and "path".

Without knowing the "domain" and the "path" to the cookie, you cannot reliably delete it. This information is not available through JavaScript document.cookie. It is not available through the HTTP Cookie header either!

However, if you know the name, path and domain of the cookie, then you can clear it by setting an empty cookie with an expiration date in the past

Resources

+1
source

Source: https://habr.com/ru/post/954611/


All Articles