Storing a List as a collection variable?


I want to get text from web elements, store them as a List and then store the list to be used by another test case.

The purpose is to verify text on one page is the same as text on another page.

I can get the process working on a single test case. I grabbed the text from each line in the List and stored them as an array then used WS.verifyMatch to compare them. I now want to split the process in to multiple test cases to be called when needed.

I either need to be able to store the List and access it through another test case or store the array as I would a collection variable.

Here’s what I’ve used so far to get the web element text, then store them.

List<WebElement> fullAddress = driver.findElements(By.xpath('//*[@id="content"]/div[1]/p/span'))


length = fullAddress.size()

String[] addressArray = new String[length]

def i=0

for (WebElement line : fullAddress) {
	addressArray[i] = line.getText()

I repeated this process for the next page and was able to compare them using WS.verifyMatch

I’d either like to store List fullAddress to be accessed by another test cases, or store

String addressArray.

I guess, you want to avoid duplicating similar code fragments in multiple Test Cases, right?

Then you should create an Groovy class in the “Keywords” folder that encapsulates the code fragment as a method, and your Test Cases should share the class and call the method. See

Off topic, but your code could use some improvement.

Enter collect() method and callbacks (Closures):

final String[] addressArray = fullAddress.collect { WebElement line ->
    return line.getText();

…but I still have some questions…

  1. Why do you need Array ? How’s come you don’t just use List ?
  2. What are you tryna do by printing the same value twice? Why print it at all?
  3. fullAddress as a name for List<WebElement>? Really?

I would change it to addressElements, or something similar…

But as for your actual question, I would just create a page class, and store this as a public static member of that class. Though the smartest way would be to just create a public method for it…

Follow kazuyaram’s suggestion on creating custom keyword, for this.