Using a class to list all objects

I recently discovered keywords are packages really and that I can do a lot of normal java programming there thanks to Russ Thomas. I’m playing with some ideas to make as much reusable as possible. One idea I had was a class that houses all the objects for a page. Declaring them with an object name and utilizing this in my testing. It works, but I could have sworn I had it working the other day without declaring the class name in front of the object name…

I am curious, what do you do? Does this make sense to you? How would you get this to work?

This is what I have:
package in keywords with name
class with name of page
then I have

public class areaExample {
public static Object example1 = findTestObject(‘pathToObject’)
public static Object example2 = findTestObject(‘pathToObject’)
public static Object example3 = findTestObject(‘pathToObject’)
}

in my test case I’ll use the
import keywordname.areaExample

Then I can use objects like this:

WebUI.click(areaExample.example1)

I could have sworn I had it working like this though:
WebUI.click(example1)

When I do that however it will underline the example1 and tell me it’s not defined. Thoughts? ideas?

2 Likes

I was thinking the same. Will you make it a package then import in your test case?

Sorry for the late resonse @Karlo_Victor_Abapo I created packages for type and keywords for pages.

So I have a package called (this is generic and not exact) programname_objects. Then I have packages:
LoginPageObjects
FirstPageObjects
BillPageObjects

I then import these into the test case
import programname_objects.LoginPageObjects

then I can reference them in the test cases like:
WebUI.setText(LoginPageObjects.usernameInput, ‘username’, FailureHandling.Continue_On_Failure)
WebUI.setText(LoginPageObjects.passwordInput, ‘password’, FailureHandling.Continue_On_Failure)
WebUI.click(LoginPageObjects.submitButton)

Hey @B_L

If you make the objects you want available elsewhere static then import your stuff static you can remove the class at the point of invocation.

Class file…

public class Stuff {

  static TestObject ex1 = findTestObject(...)

  static String stuff = "Russ wrote this highly dubious code"
  
  static void method_thing(...) { /* do something wild and crazy */ }
}

Test Case…

import static programname_objects.Stuff.*

method_thing()
WebUI.setText(ex1, stuff)

UPDATE:

Be aware though, the static TestObject will only be valid while the life of the element itself remains current – if the element is refreshed/rebuilt asynchronously, then ex1 is likely to become “stale”. You might be better writing methods to always return a fresh copy.

2 Likes

@Russ_Thomas I’m very new to java and pretty much self taught since I started playing with katalon. is there and advantage to not making each object public? or to use “TestObject” over “Object”? I’m trying to understand that it does? I have not tried the static import before and if I didn’t make them public static they never use to show up. For example would this safe on memory?

Me too. Well, I did a little bit back when it was “new” – it had just gone through the name change from Oak. It had interfaces! Cool! Suck it up Stroustrup! More ways to do stuff! Even more ways to break things! Oh shit.

Anyway. Public…

(Almost) Everything in groovy is public unless you say otherwise.

TestObject: It is a TO so why not declare it as one?

I don’t know the answer to the memory question.

1 Like

Hey @B_L

Read my update on the post above.

And thanks for marking it a solution :sunglasses:

1 Like