CaBocuk
CaBocuk

Reputation: 21

How to start the firefox the second time with previous cookies using Selenium (Java)

  1. In my userstory I have to verify in firefox that when I login to particular site, some cookies appear.
  2. Then I need to close the browser and open it again, then open mentioned site and verify that the cookies which appeared in previous session are saved and have the same values.

The problems are with statement#2. I can't get the profile that is created by FirefoxDriver in the first session to create a new one based on that.

I've tried following things:

First browser was launched using this constructor:

public AchieveDriver(/*some parameters*/) {
    super(new FirefoxBinary(new File("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe")), this.profile=new FirefoxProfile());
    /*some code*/
}

and then create the second one using this one:

public AchieveDriver(/*some other parameters*/) throws Exception {
    super(new FirefoxBinary(new File("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe")), new FirefoxProfile(profile.layoutOnDisk()));
    /*some code*/
}

but that did not work

Are there any solves to get the cookies from first profile? (Except of saving them as Cookie objects and adding to second profile?)

Upvotes: 2

Views: 626

Answers (2)

Mo H.
Mo H.

Reputation: 1818

I'm going to put this in another answer since its technically a different solution. Although they are using python, this is more or less the exact same issue you are having.

Although they didn't find an exact solution, they "found that we need to store our session at some place and once we close the browser then we need to pass existing session while opening new instance."

I'm not sure if this will be of any help, but I figured it was worth adding in as well.

Upvotes: 0

Mo H.
Mo H.

Reputation: 1818

I've never had to handle cookies using Selenium. But if I had to venture a guess i'd say you need to get all the cookies from your first instance, and then add them back into you second instance. The methods to do this are found here

Use:

driver.manage().getCookies();

to get all your cookies and then iterate through them and add them back in using

otherdriver.manage().addCookie(aCookie);

Alternatively, this may help you solve your issue

Upvotes: 0

Related Questions