Browser and Cookies related doubts

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

Browser and Cookies related doubts

mohit.khatri
Hi,

I want to know suggestion for below scenario using webdriver

1. How to close browser without deleting cookies?
2. After closing the browser in above case, how to open new browser instance?

Thanks,
Mohit
Reply | Threaded
Open this post in threaded view
|

Re: Browser and Cookies related doubts

softwaretestingforum
Administrator
In case of Firefox, you could ask it to use your default profile (which in turn would have cookies saved from past browsing sessions)

FirefoxProfile firefoxProfile = new ProfilesIni().getProfile("default");
WebDriver driver = new FirefoxDriver(firefoxProfile);

Now closing the browser is not going to delete the cookies your browser would store when executing tests.
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Browser and Cookies related doubts

mohit.khatri
Thanks Tarun for your reply.

After creating FF driver as mentioned by you, i've used below code

driver.navigate().to("http://www.google.com");
System.out.println("cookies: "+ driver.manage().getCookies().toString());
driver.close();
driver.navigate().to("http://www.google.com");
System.out.println("cookies: "+ driver.manage().getCookies().toString());

But system is giving below exception:
org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Build info: version: '2.21.0', revision: '16552', time: '2012-04-11 19:09:00'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.7.0_04'
Driver info: driver.version: RemoteWebDriver

Also, please do let me know how to open a new browser instance using webdriver without passing any URL.
Reply | Threaded
Open this post in threaded view
|

Re: Browser and Cookies related doubts

softwaretestingforum
Administrator
Since you close browser using -

driver.close(); 

you can not navigate again until unless you launch browser again. That is invoking new FirefoxDriver() again. This launches a blank browser and then you can launch URL using -

driver.get("your URL here");
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Browser and Cookies related doubts

mohit.khatri
What I'm trying to do that, I need cookies value at first time and after closing the browser instance i.e. same cookie should present before and after.

But after using driver.get(""); system is not giving any cookie value after reopening the browser.

Example is as below. First time system is giving cookie value and if i close the browser and again open it then cookie value is empty. It means, after using

FirefoxProfile firefoxProfile = new ProfilesIni().getProfile("default");
driver = new FirefoxDriver(firefoxProfile);

system is not persisting the cookie after closing the browser.

cookies: [NID=60=PfVGuRJBmpU6J47ahl3H2Hk5NiZddC13dZiUsfiGLVmPXkzecLqc3UULbWQno8xibfOPJcNhEr16bPmr4glUc-ylyk4fObRrEwFili4livqyafYviVvqscCNeqgt7Tpu; expires=Tue, 27 Nov 2012 03:04:49 IST; path=/; domain=.google.co.in, PREF=ID=f6bd9ade9994c3d6:U=2d1ba825648b08fb:FF=0:TM=1337915064:LM=1337915066:S=iu8aPAyc-KK5mUqM; expires=Sun, 25 May 2014 08:34:26 IST; path=/; domain=.google.co.in]
cookies: []
Reply | Threaded
Open this post in threaded view
|

Re: Browser and Cookies related doubts

mohit.khatri
Hi All,

If anybody has any idea regarding persisting browser cookie then please let me know. My query is mentioned in above post.

Thanks,
Reply | Threaded
Open this post in threaded view
|

Re: Browser and Cookies related doubts

softwaretestingforum
Administrator
I am not sure if it wrong. It is only that you get a new cookie. Did you try to check on FF setting if two cookies are stored with two different sessions of opening browser? Do it manually first and see what you encounter.
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Browser and Cookies related doubts

mohit.khatri
I followed below steps for verifying cookies
1. Opened FF browser and accessed two different sites
2. Verified cookies of both the sites
3. Closed the FF browser without deleting any of the cookies
4. Again opened the FF browser and verified the previous cookies

All cookies were present in the browser. Manually it is working fine but using webdriver it is not working. It is always deleting the cookies whenever a new instance opened up.

Same concept is working perfectly fine in one of my QTP scripts i.e. in between we are closing the browser without deleting the cookie and again opening the browser for verifying existing cookie.

One thing I found using webdriver that if i open FF instance first time and access some of the pages then cookies get created and after closing the FF browser, still cookies are present.

It means, before opening another FF instance, I was able to get the cookie but once the new instance is opened then nothing is present.

Reply | Threaded
Open this post in threaded view
|

Re: Browser and Cookies related doubts

softwaretestingforum
Administrator
It's little difficult to assume that WebDriver deleted cookies even when you use the default profile. Could you post your entire code here that is setup, test code which saves cookies and tear down methods.
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Browser and Cookies related doubts

mohit.khatri
public class SampleTest extends SeleneseTestBase {

        public WebDriver driver;
        private Selenium selenium;
       
        @BeforeClass
        public void setup(){
                FirefoxProfile firefoxProfile = new ProfilesIni().getProfile("default");
                driver = new FirefoxDriver(firefoxProfile);
        }
       
        @AfterClass
        public void teardown(){
// driver.quit();
        }
       
        @Test
        public void testGetTitle(){
       
                driver.navigate().to("http://www.google.com");
                driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

                System.out.println("cookies first time: "+ driver.manage().getCookies().toString());
                driver.close();
                System.out.println("cookies after closing the browser: "+ driver.manage().getCookies().toString());  
                driver.get("");
                driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

                System.out.println("cookies after reopening browser: "+ driver.manage().getCookies().toString());

        }
}
Reply | Threaded
Open this post in threaded view
|

Re: Browser and Cookies related doubts

softwaretestingforum
Administrator
mohit.khatri wrote
                driver.navigate().to("http://www.google.com");
                driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

                System.out.println("cookies first time: "+ driver.manage().getCookies().toString());
                driver.close();
                System.out.println("cookies after closing the browser: "+ driver.manage().getCookies().toString());  
        }
}
Here both first and second print statement get me the same values.  And I assume for you too

Now after this you do a get again which is wrong as you have already closed the browser. Moreover you don't pass any argument to get, which is wrong again -

 
mohit.khatri wrote
                driver.get("");
                driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
                System.out.println("cookies after reopening browser: "+ driver.manage().getCookies().toString());
        }
}
Try - driver.get("www.google.com"); and you would encounter error.
Now as I mentioned earlier, if you want to launch browser again after closing it then you should create the driver instance again. You can not use driver object after having closed it. Now even after this you may see new value of cookie as Google might be setting new cookie, each time you launch the browser.
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Browser and Cookies related doubts

mohit.khatri
I agree whatever you are saying but my test case is that, I need to reverify specific cookie value after  opening the browser. But in this case, if i reopen the browser then new cookie gets created and previous value doesn't persist.

For eg. I've one scenario that
1. first of all access any site and verify its cookie value (say specific value count=1)
2. Now close the browser without deleting the cookie
3. Now again open the browser and access same site and recheck existing cookie value (i.e. in this case, count should get increased by 1 i.e. it should be 2)

But in my case, again new cookie is getting generated.

Also, If i want to open blank browser without passing any url then how can i open? That's the reason I used driver.get(""). If there is any alternate then please do let me know.

In QTP, its pretty simple to open new instance without passing url but I'm not sure about the same thing in selenium.

Reply | Threaded
Open this post in threaded view
|

Re: Browser and Cookies related doubts

softwaretestingforum
Administrator
I can answer only one question at this point. Following code opens a blank browser -

FirefoxProfile firefoxProfile = new ProfilesIni().getProfile("default"); 
FirefoxDriver driver = new FirefoxDriver(firefoxProfile); 
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Browser and Cookies related doubts

mohit.khatri
Thanks Tarun.

I read in this site http://grokbase.com/p/gg/selenium-developer-activity/125j0f3t4k/issue-3927-in-selenium-selenium-webdriver-reuse-existing-browser-session-instead-of-opening-new-windows and 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 pretty sure about this, as i didn't check this. I'll try this part. Hopefully, it will solve problem.
Reply | Threaded
Open this post in threaded view
|

Re: Browser and Cookies related doubts

softwaretestingforum
Administrator
Please do keep us posted if you find a solution.
~ seleniumtests.com