Having TestObject update itself in the test-script while using Custom Keyword

Hello team,

My app has same work-flow on both iOS & Android, so I don’t want to spend effort to split the test (one for Android, another one for iOS), it’s a kind of duplication and hard for us to do maintain later.

I create the custom keyword to return to correct TestObject base on test device (iOS Objects and Android Objects have same structure).

But if we’re using the Custom Keyword then TestObject doesn’t update itself in the test-script. I think this one need some knowledge inside Katalon development team.

I’d be great if you guys can shed a light OR help to add one more keyword to fit above purpose.

@Keyword
    	def findObject(String objectPath) {
    		String objectRepo = Mobile.getDeviceOS().equals('iOS') ? 'iOS Objects': 'Android Objects'
    		def actualObjectPath = objectRepo + '/' + objectPath
    		println actualObjectPath
    		return findTestObject(actualObjectPath);
    	}

// I tag some Katalon Developers which I see on the forum here because this one needs the knowledge inside of Katalon development team. I’m sorry if it causes any inconvenience.
@ThanhTo @Zarashima @Brian_Ducson

Thanks a ton.
Tam

Does that line print the correct path? Maybe I’m missing something, but I don’t see anything wrong with what you are doing here.

However, I would make the keyword a static and import the class statically everywhere you wish to use it:

package com.mypackage

public class MyClass {

 // @Keyword <== You don't need this
  static TestObject findObject(String objectPath) {
    String objectRepo = Mobile.getDeviceOS().equals('iOS') ? 'iOS Objects': 'Android Objects'
    String actualObjectPath = objectRepo + '/' + objectPath
    println actualObjectPath
    return findTestObject(actualObjectPath)
  }
}

In your Test Case:

import static com.mypackage.MyClass.* // import all static methods from MyClass

 // get the button for the correct platform, iOS/Android...
 TestObject button = findObject("my_button")

 WebUI.click(button)

Hope that helps.

1 Like

@Russ_Thomas thank you for your quick response. Yes it’s fine to use.

But the concern is when I update Test Object (e.g. change name or move to another folder)

  • If I use Custom Keyword need to manually modify it in my test-cases.

  • If I use Built-in Keyword findTestObject, Katalon will help to do that part.

The changing of folder/path can be managed by having a static variable which you keep updated to the correct path. Then you only have to change it in one place.

Then use the variable in the method:

public class MyClass {
  static String iOSPath = 'iOS Objects'
  static String androidPath = 'Android Objects'

  static TestObject findObject(String objectPath) {
    String objectRepo = Mobile.getDeviceOS().equals('iOS') ? iOSPath: androidPath
    String actualObjectPath = objectRepo + '/' + objectPath
    println actualObjectPath    
    ...
  }
}

I’m not sure why you would change the name but you could use the same approach for the name, too.

1 Like

I’m not sure why you would change the name but you could use the same approach for the name, too.

I just think before I apply this for all projects. I did same approach as you, but it only can change parent/root folder.

Update Test Object doesn’t happen frequently, but assume that we have a lot Test Objects and a lot of Test Cases, one TestObject is used in some-test-cases then it’d be very convenient if we update something on TestObject and Katalon helps to update/refactor that in related TestCases (as what Katalon has now for Built-in Keyword findTestObject).

Here is the video that feature of Katalon (if we use Built-in Keyword findTestObject) katalon_auto_refactor.mp4 - Google Drive

But that is the reason why I am suggesting you put the things that can change (variables) in ONE place (in a keyword class) and make sure your Test Cases ALWAYS refer to the variables. Then you only have ONE THING to change in ONE PLACE.

Understand?

Sorry, I don’t have time to watch videos.

even that way still needs to do 2 actions

  • first: update TestObject (change name or move to another folder, etc…)
  • second: update variable in the Class
    → do I understand it correct ?

If yes, then because we have many screens/pages, then we will have many Classes like this, unfortunately we have many projects under main project, a lot of thing need to add there and maintain later.

The goal of Katalon is to help non-tech QA can write automation-test, that’s why I’m asking if there is anyway to have a same feature (which already exists for built-in keyword) for a custom keyword.

it’s fine, I was afraid that my word was not clear enough, so just create video to make it more clear.

Have a nice day.