5

Selenium has the ability to temporarily store data items and then later retrieve them in subsequent tests, e.g.

storeText | @id='ctl00_ContentPlaceHolder1_FormView1' | someValue

This works well within a single test and also between tests in the same Test Suite when a value needs to be carried forward across test boundaries. Unfortunately it doesn't work between Test Suites (which is a requirement for our application that includes a number of workflows referring to the same object). How can Selenium be used to store values across Test Suite boundaries?

David Clarke
  • 12,888
  • 9
  • 86
  • 116

2 Answers2

3

It's possible to store values from a Selenium Test into the browser's Local Storage using javascript, e.g. if previously a value had been stored to someValue:

getEval | this.browserbot.getUserWindow().localStorage.setItem("someValue",storedVars['someValue'])
assertEval | this.browserbot.getUserWindow().localStorage.getItem("someValue") | ${someValue}
storeEval | this.browserbot.getUserWindow().localStorage.getItem("assetLabel") | someValue

In this case, this.browserbot.getUserWindow() returns the window of the application. This will store someValue into Local Storage from where it can subsequently be retrieved back into the Selenium stored variables.

David Clarke
  • 12,888
  • 9
  • 86
  • 116
  • Will this work if the browser profile gets reset between test suites? Or would you have to set Selenium to use a static profile? – artbristol Oct 03 '11 at 07:03
  • @artbristol: It depends on what you mean by "reset". If you tell Firefox to clear your personal data, then it will be lost. But Selenium IDE never does that, so in general, it will still be there. – Ross Patterson Oct 03 '11 at 12:33
  • @Ross But doesn't selenium start up with a fresh Firefox profile each time you run a test suite? I would have thought Local Storage wouldn't survive that. – artbristol Oct 03 '11 at 12:34
  • @artbristol: You're thinking of Selenium RC, which starts Firefox with a clean profile. With Selenium IDE, **you** start Firefox and launch the test suite by hand. – Ross Patterson Oct 03 '11 at 12:35
  • Just to clarify, the data stored to Local Storage is associated with the site that is being tested and not with Selenium. So for example if you stored a value in Local Storage when testing against Test.com, that value is not available when testing against Staging.com. – David Clarke Oct 03 '11 at 19:24
  • Does anyone know or can anyone confirm whether the localStorage works with Selenium RC instead of IDE? This example here is for IDE only. I was just testing the feature and it works fine under WebDriver using window.localStorage, but with RC, using same browserbot method as example above, I get Unexpected call to method or property access. I'm testing with IE using Python bindings for Selenium RC by the way. IE works fine using WebDriver. – David Jul 06 '12 at 21:25
0

You could also implement the persistence in the code that's running your Selenium tests. If you're using RC, this would be fairly trivial. (ie, just straight database queries to insert/update and then retrieve).

If you're using Selenese and don't have access to an API for persistence, you could also whip up a quick and dirty little webpage to store the data in a database and then read it back for subsequent test runs. Obviously this isn't ideal, but it should work if you can't access a persistence store directly from your tests..

Peter Bernier
  • 8,038
  • 6
  • 38
  • 53
  • Thanks and yes I'm sure there are alternatives but this is specifically Selenium IDE, i.e. it's running in Firefox and the users are not developers. Using Local Storage is a simple solution for the problem they have. – David Clarke Oct 03 '11 at 05:22