Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear cookies per each test in PhantomJS+GhostDriver+WebDriver client configuration

How can I clear cookies for each tests in my PhantomJS + GhostDriver + Selenium WebDriver + WebDriver Client system?

My test process looks like this:

  1. Start selenium-web-driver-standalone in hub role.
  2. Start phantomjs in webdriver mode and attach it to selenium webdriver.
  3. Start shell script that iterates over tests suites and start each.
  4. Each test uses webdriver client and communicate with browser connected to selenium web driver.

When I use firefox browser instead of phantomjs all tests passed ok. But when I switch to using phantomjs as browser all tests that checked registration failed because cookies already set after first test execution. Can I clear all cookies on every test start up? Or I should to restart phantomjs process on every separate test (as this is with firefox and selenium webdriver in not hub role)?

like image 346
Ivan Velichko Avatar asked Sep 25 '13 14:09

Ivan Velichko


2 Answers

Maybe some offtopic, cause author marked that he uses php to run test.

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

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

Requires NuGetPackages:

PhantomJS 2.0.0

Selenium.Support 2.48.2

Selenium.WebDriver 2.48.2

Tested with .NETFramework v4.5.2

like image 68
userlond Avatar answered Oct 02 '22 00:10

userlond


You can delete specific cookie using:

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

And to delete all the cookies using:

webDriver.manage().deleteAllCookies();
like image 45
Johnny Avatar answered Oct 02 '22 01:10

Johnny