Dynamically adding year to test object

Hello,

I am fairly new and I am looking for a way I can dynamically handle searching for specific test depending on the current date.

Currently I have an xpath on an object with the following:
//span[(text() = ‘2023 - 2024’ or . = ‘2023 - 2024’)]

which is called upon by:
WebUI.verifyElementPresent(findTestObject(‘Object Repository/01 1 Home Page test/span_2023 - 2024’), 0)

Is there a way I can make it so that the text()=‘2023 - 2024’ is handled with the following logic:
text()=‘(currentYear-1) - currentYear’

Any pointers would be much appreciated. Thank you.

1 Like

To dynamically handle the year in your XPath based on the current date, follow these steps:

1. Calculate the Dynamic Year Range

groovy

import java.time.Year

// Get current year
int currentYear = Year.now().value
int previousYear = currentYear - 1
String dynamicYear = "${previousYear} - ${currentYear}"

2. Create Dynamic Test Object

groovy

// Build XPath with dynamic year value
String dynamicXpath = "//span[(text() = '${dynamicYear}' or . = '${dynamicYear}')]"

// Create test object with dynamic XPath
TestObject dynamicYearObject = new TestObject('dynamicYearObject').addProperty('xpath', ConditionType.EQUALS, dynamicXpath)

// Use the dynamic test object
WebUI.verifyElementPresent(dynamicYearObject, 10)

Full Implementation:

groovy

import java.time.Year
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

// Calculate dynamic year range
int currentYear = Year.now().value
String dynamicYear = (currentYear - 1) + " - " + currentYear

// Create dynamic test object
TestObject yearObject = new TestObject('Dynamic Year').addProperty('xpath', ConditionType.EQUALS, 
    "//span[(text() = '${dynamicYear}' or . = '${dynamicYear}')]")

// Verify element with dynamic year
WebUI.verifyElementPresent(yearObject, 10)

Explanation:

  1. Year Calculation:
  • Year.now().value gets the current year (e.g., 2024)
  • currentYear - 1 gives the previous year (2023)
  • Format becomes “2023 - 2024”
  1. Dynamic XPath Construction:
  • Uses string interpolation to insert dynamicYear into the XPath
  • XPath becomes: //span[(text() = '2023 - 2024' or . = '2023 - 2024')]
  1. Dynamic Test Object:
  • Creates a test object at runtime with the dynamic XPath
  • No need to modify your Object Repository entries

Key Advantages:

  • Always current: Automatically updates each year
  • No hardcoding: Eliminates manual updates for new years
  • Reusable: Works across different environments/timezones

Notes:

  • If you need to use existing Object Repository items:

groovy

TestObject baseObject = findTestObject('Object Repository/01 1 Home Page test/span_2023 - 2024')
String newXpath = baseObject.getActiveProperties().get('xpath').value.replace('2023 - 2024', dynamicYear)
TestObject dynamicObject = new TestObject().addProperty('xpath', ConditionType.EQUALS, newXpath)

This approach ensures your tests automatically adapt to the current year without manual script updates

1 Like

Thank you! :slight_smile: This works great

1 Like

Just a note that your statement with a “timeOut” of zero will always fail as it takes a moment for KS to check if your statement is solidly true or false. Zero gives it no time to check, so it will always fail. You can note that @dineshh’s answer has a solid “timeOut”.
WebUI.verifyElementPresent(yearObject, 10)

2 Likes