001
001

Reputation: 65155

Selenium phantomjs webdriver, how do I use sessions?

I start phantomjs:

IWebDriver driver = new PhantomJSDriver(driverpath);

I perform a series of selenium commands to login website, submit firms, verify Page etc.

Once logged in, can I start the webdriver again and reuse the same browser user logged session again so that I don't need to login again? How would I use sessions for phantomjs in selenium?

Upvotes: 2

Views: 2105

Answers (1)

Artjom B.
Artjom B.

Reputation: 61902

In plain phantomjs there is the command line flag --cookies-file=/path/to/cookies.txt which can be used to persist the session cookie and use it in the next invocation.

The .net API provides the same functionality through the PhantomJSDriverService class. This is taken from this answer.

DriverService service = PhantomJSDriverService.CreateDefaultService(driverpath);
service.CookiesFile = "path/to/cookies.txt";
IWebDriver driver = new PhantomJSDriver(service);

The cookies will be automatically saved into this file.

If you want to have a little more control, then you should save the session cookie from window.document.cookie using driver.ExecuteScript into a file and retrieve it later.

Upvotes: 3

Related Questions