boakley/robotframework-pageobjectlibrary

Dynamic elements in locators?

gerrymcgovern1 opened this issue · 2 comments

I am testing an application where I have to select different items in a navigation bar. So in my locators I have:

"ShoesCategory": "//a[contains(text(),'Shoes')]",
"CoatsCategory": "//a[contains(text(),'Coats')]",

and then I have 2 separate methods like this to click

def click_shoes_category(self):
    self.se2lib.click_element(self.locator.ShoesCategory)

def click_coats_category(self):
   self.se2lib.click_element(self.locator.CoatsCategory)

I have quite a number of different categories to select and verify, is there a way to DRY this up so that I can pass a variable into the locator? So something like this:

"category": "//a[contains(text(),'[%s]')] % category"

And then have a method like this:

 def click_category(self, category):
     self.se2lib.click_element(self.locator.category)

Any help would be great, thanks!

Thanks boakley! That worked, although I just had to use %s rather than [%s]