kraul25
kraul25

Reputation: 137

Karate UI automation, is it possible to make locators dynamic

Thanks Peter for API,Perf karate framework. Now I am trying UI automation and it looks perfect as of now. I have below question: I have a menu bar with 15 items in it. they are named as following:

-Home
-Account
-groups
-settings
etc etc.

Locator for each one is like this "a[name=Home]" , "a[name=Account]", "a[name=groups]" . So ony the part after name= is something that is dynamic in nature. So my question is that is it possible to somehow make this dynamic locator?

I have written a called feature in which i have written steps to login and select a menu item. and I want to pass the menu item from calling feature in json like below: if I want to click on Account menu

* call read(menuItem.feature) {menuItem: Account}

menuItem.feature looks something like below:

Given url 'xyz'
And input (username, userID)
And input (password, password)
And click("button[name=login]")
And click("a[name=Home]")

Here I want to make Home as dynamic in last click step on menuItem.feature so that I can pass other menu items and reuse all above steps/feature file to login everytime with different menu items passed from calling feature file in json{. Again its just a query. I have currently written multiple scenarios to click on menu item and then in calling feature I call the called feature with tags like @Home,@account etc. and this works perfectly fine.

Also if there is any other better way to achieve this then please suggest

Upvotes: 1

Views: 744

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58088

Yes. Let me put it this way, the things you see such as input('foo', 'bar') is pure JS behind the scenes. So normal programming-language rules apply. For example:

* def dynamic = 'foo'
* def locator = 'a[name=' + dynamic + ']'
* input(locator, 'some value')

Upvotes: 1

Related Questions