Re: Pre-load a cookie for a domain using Selenium webdriver

4,455 views
Skip to first unread message

Divya Maheshwari

unread,
Jun 10, 2013, 5:33:50 AM6/10/13
to seleniu...@googlegroups.com
Try to use some code like

Set-Cookie:flash=LoremIpsum; path=/; domain=.example.com

On Friday, June 7, 2013 10:56:36 PM UTC+5:30, peafunk wrote:
Hi,

I need to create a test that has a pre-loaded cookie in the request before hitting a URL.  I know in the documentation it says you cannot add a cookie to any other domain than the one you are on.  Has anyone figured out a way around this?  I have to treat this as a request from a user with a permanent cookie, which will drive behavior.  Thanks. 

robbiewinston

unread,
Jun 10, 2013, 6:01:14 AM6/10/13
to seleniu...@googlegroups.com
I have not found away to drop a cookie before the url, but I have done the following which may not be suitable in your scenario;

1. Create a factory class to create webdriver instances
2. Before returning the webdriver instance, navigating to any page on the domain under test, and drop the cookie, then navigate browser back.
3. Your test can now begin, unaware that the navigation and cookie dropping has taken place

The webpage does not have to be a valid page, I believe a 404 Not Found page will still allow you to drop cookies.

My problem was that the home page displayed a pop up if there was no historical cookie, so this approach allowed me to hide that functionality

David

unread,
Jun 10, 2013, 2:59:15 PM6/10/13
to seleniu...@googlegroups.com
+1 on robbiewinston's suggestion. Some variations of it:

* you could call the relevant code under test setup() to execute like a precondition before executing test
* you need not use factory class or do stuff before returning webdriver instance. just need to use webdriver to set up the environment (with or without cookie, etc.) before you do the real testing, how you do that is up to you.

I ran into similar issue before, but with cookie existing in browser/system when we expect not to (the reverse case). You may get this with IE and Safari, when not using their clean session capability option since they don't start with clean profiles unlike FF and Chrome. My workaround was to add extra logic to the test to clear out the cookie if found before proceeding to test the correct logic. For us, the way the flow worked, it made more sense to make part of test rather than some setup() type step before executing the test.

Rodrigo Martin

unread,
Jun 12, 2013, 2:40:57 PM6/12/13
to seleniu...@googlegroups.com
But with robbiewinston's suggestion, what happens with setting domain cookies from another domain? or maybe by just visitting about:blank?

Regards,
Rodrigo.

robbiewinston

unread,
Jun 13, 2013, 3:37:02 AM6/13/13
to seleniu...@googlegroups.com
You can drop a cookie on another domain as long as you navigate to that domain first.

Rodrigo Martín

unread,
Jun 13, 2013, 9:48:34 AM6/13/13
to seleniu...@googlegroups.com
Correct. I though your method included the ability to not navigate to the domain first. 
currently this add's a lot of time to UI tests, since I need to wait for the first navigation to end, drop the cookie and then start the test. I think there is no way to avoid this limitation at the moment :(.

On Thu, Jun 13, 2013 at 4:37 AM, robbiewinston <wareham...@gmail.com> wrote:
You can drop a cookie on another domain as long as you navigate to that domain first.

--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/ODJ4_KZOBpI/unsubscribe?hl=en-US.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/6f97362f-867d-404d-bad8-44170f1b0164%40googlegroups.com?hl=en-US.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Robbie Wareham

unread,
Jun 13, 2013, 9:52:38 AM6/13/13
to seleniu...@googlegroups.com
No, I always go to the domain first, apologies if that wasn't clear.



Reply all
Reply to author
Forward
0 new messages