Dynamic elements in locators?
gerrymcgovern1 opened this issue · 2 comments
gerrymcgovern1 commented
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!
boakley commented
You would do it almost exactly how you specified, just move the
substitution to where you actually use the locator
...
"category": "//a[contains(text(),'[%s]')]"
...
def click_category(self, category):
self.se2lib.click_element(self.locator.category % category)
when I do this in my own code, I usually name the locator with a suffix
such as "template" or "pattern" (eg: "category_template")
…On Mon, Feb 19, 2018 at 3:14 PM, Gerry McGovern ***@***.***> wrote:
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!
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#12>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABEmYvxybgRQwYhLGLO5hVoKTWAc7yotks5tWeQcgaJpZM4SLHJH>
.
gerrymcgovern1 commented
Thanks boakley! That worked, although I just had to use %s rather than [%s]