Search code examples
seleniumgeb

Call methods with sililar names through a loop in geb


I'm new to Geb and fairly new in Java. I ask my self if its possible to call multiple methods through a loop. For example this part:

homePage.file1 = Content.Upload()
isDisplayed(homePage.clear1, true)
homePage.file2 = Content.Upload()
isDisplayed(homePage.clear2, true)
homePage.file3 = Content.Upload()
isDisplayed(homePage.clear3, true)

I had the idea to call this through a loop cause the names are very similar to each other. Only the numbers are different. So I thought about something like this:

String[] elements = { "file1", "file2","file3"}
for( int i = 0; i <= elements.length - 1; i++){
    homePage.elements[i] = Generator.fileUpload()
}

But this won't work. Is there any other way to get this to work?

Greetings


Solution

  • Think this is what you're trying to achieve?:

    def elements = ["file1", "file2","file3"]
    elements.each {     
        homePage."${it}" = Generator.fileUpload()
    }