Selenium Tips: Efficiently removing cookies

November 20th, 2009 by Santiago Suarez Ordoñez

We all know that when your tests run in firefox, things are easy. Even for sessions and cookies. Each time you run the start() method, Selenium will generate a clean Firefox profile and you can forget about previous tests or sessions.

Well, life is not always easy, and we don’t always have to test just Firefox… When IE comes to the game, Selenium can’t create clean profiles and cookies from previous tests will remain for future ones, causing unexpected issues.

For this, Selenium API’s has 2 methods you should use:

sel.delete_cookie(name, optionsString)

This method is useful when you only want to remove a specific cookie. The parameters it receives are sometimes hard to write and can take some time to actually make it work the way you need.

sel.delete_all_visible_cookies()

This one is my personal favorite. It just blows away any cookie that the current domain could have created in your browser.

But wait! I said “current domain”, not all domains. So, if your tests go though several domains, and you want to keep things clean, you’ll need something like the following:

def clean_history(sel, domains):
    temp = sel.get_location()
    for domain in domains:
        sel.open(domain)
        sel.delete_all_visible_cookies()
    sel.open(temp)

This is a python function you can add anywhere, and by calling it with the selenium browser as the first parameter and a list of all domains used in the second one, will make you session clean without too much hassle. Off course you can always make this part of your own version of the selenium client, for then being able to call:

sel.clean_history(domains)

or even better, make selenium keep a record of each domain and then you can call:

sel.clean_history()

But that will have to go in another TOTW.

  • Share/Bookmark

Selenium 2.0 and Beyond!

November 16th, 2009 by John Dunham

On Weds. 11-Nov-09, sponsored by Sauce Labs and Salesforce.com, Simon Stewart (Google) and Jason Huggins (Sauce Labs) reprised their Zurich GTAC 09 talk on this subject.  Fortunately, this time both microphones worked.  Check out their “and one more thing” item at the end.

Here’s the Q & A session as well:

  • Share/Bookmark

Selenium Tips: Upload files on browsers running over remote machines

November 13th, 2009 by Santiago Suarez Ordoñez

Having your tests upload files to the test application is something that becomes confusing when moving Selenium RC servers to different machines.

Browsers can only upload files from the local machine, but when the RC server (and consequently the browser) are on a different machine than the one running the test, this can be very hard to do.

Luckily, for this kind of situations, Selenium has the attach_file method which receives both a locator and a URL as parameters, then the RC Server takes care of downloading the file to the test machine at a temporary location and then attaching the file completing with this path in the desired field.
Here’s the reference from Selenium’s documentation:

In [5]: sel.attach_file?
Definition:    a.attach_file(self, fieldLocator, fileLocator)
Docstring:
Sets a file input (upload) field to the file listed in fileLocator
'fieldLocator' is an element locator
'fileLocator' is a URL pointing to the specified file.
Before the file  can be set in the input field (fieldLocator), Selenium RC may need to transfer the file to the local machine before attaching the file in a web page form.
This is common in selenium  grid configurations where the RC server driving the browser is not the same  machine that started the test.
Supported Browsers: Firefox ("*chrome") only.

The only drawback of using this method is that the files will have to be placed in a public URL. However, if you don’t want them to be 100% public, you can still keep them safe using basic HTTP Auth and get them using the auth information in the same URL.

Here’s an example usage of the command in python:

sel.attach_file("css=input[type=file]", "http://url.com/file.txt")

Another caveat in the implementation, is that the file must be placed on top level in the URL, so using the following URL will not work:

http://saucelabs.com/subdirectory/subdirectory2/file.txt

while the following will:

http://saucelabs.com/file.txt

Hope you find this useful, and if you need more info, let us know in the comments.

  • Share/Bookmark