Problem when get object property value

I write a updateAttribute function. Purpose is to get the attribute value of object a, add prefix of suffix on that value of a to interact with object B. Problem here is I use the method findPropertyValue(attribute) to get the object A attribute value, but the value I got not like I have expected.

def static TestObject updateAttribute(
			TestObject objectFocus,
			String attribute,
			type = null,
			String data) {
		if (objectFocus != null || attribute != null || data != null) {
			String originalValue = objectFocus.findPropertyValue(attribute)
			println("Get: "+originalValue)
			String updatedValue = null

			if (type == null || type == '' || type == 1) {
				updatedValue = attribute + data
			}
			else if (type == 2) {
				updatedValue = data + attribute
			} else if (type == 3) {
				updatedValue = data
			}
			objectFocus.getProperties().clear()
			TestObject objectNew = objectFocus.addProperty(attribute, ConditionType.EQUALS, updatedValue)
			String infoNew = objectNew.findPropertyValue(attribute)
			println("Origin value from object ${objectFocus}: ${attribute}: ${originalValue} \n- Concate with: ${data} \n=> Concatenated value is: ${infoNew}")
			KeywordUtil.markPassed("Origin value from object ${objectFocus}: ${attribute}: ${originalValue} \n- Concate with: ${data} \n=> Concatenated value is: ${infoNew}")
			return objectNew
		} else {
			println("Error, variable has empty data")
			KeywordUtil.markFailed("Error, variable has empty data")
		}
	}

When I running on script test first (script under). The findPropertyValue give me the xpath, but running in function it is null, so I have to use attribute in the function.

object1 = findTestObject('Object Repository/Draft/test1')
originalValue = object1.findPropertyValue('xpath')
suffix = '/div/span[@title = \'Clear all\' and (text() = \'×\' or . = \'×\')]'
println('Origin value: ' + originalValue)
objectNew = objectUtils.updateAttribute(object1, originalValue, '1', suffix)
println('New value after update: ' + objectNew)
recheckObject = object1.findPropertyValue('xpath')
println('Recheck origin after update: ' + recheckObject)
2 Likes

hi @qaat.hanh
you can try this corrected function:

def static TestObject updateAttribute(
        TestObject objectFocus,
        String attributeName,  // Changed parameter name for clarity
        type = null,
        String data) {
    
    if (objectFocus != null && attributeName != null && data != null) {
        // Get the current value of the attribute
        String originalValue = objectFocus.findPropertyValue(attributeName)
        println("Get: " + originalValue)
        
        String updatedValue = null
        
        // Fix the concatenation logic to use originalValue instead of attributeName
        if (type == null || type == '' || type == 1) {
            updatedValue = originalValue + data  // Append suffix
        }
        else if (type == 2) {
            updatedValue = data + originalValue  // Prepend prefix
        } else if (type == 3) {
            updatedValue = data  // Replace entirely
        }
        
        // Clear properties and add the updated one
        objectFocus.getProperties().clear()
        TestObject objectNew = objectFocus.addProperty(attributeName, ConditionType.EQUALS, updatedValue)
        
        String infoNew = objectNew.findPropertyValue(attributeName)
        println("Origin value from object ${objectFocus}: ${attributeName}: ${originalValue} \n- Concatenate with: ${data} \n=> Concatenated value is: ${infoNew}")
        KeywordUtil.markPassed("Origin value from object ${objectFocus}: ${attributeName}: ${originalValue} \n- Concatenate with: ${data} \n=> Concatenated value is: ${infoNew}")
        
        return objectNew
    } else {
        println("Error, variable has empty data")
        KeywordUtil.markFailed("Error, variable has empty data")
        return null
    }
}

and here’s how to call it correctly:

// Correct usage
object1 = findTestObject('url')
suffix = '/div/span[@title = \'Clear all\' and (text() = \'×\' or . = \'×\')]'

// Pass the attribute name 'xpath', not the value
objectNew = objectUtils.updateAttribute(object1, 'xpath', 1, suffix)

println('New value after update: ' + objectNew.findPropertyValue('xpath'))

// Check the original object
recheckObject = object1.findPropertyValue('xpath')
println('Recheck origin after update: ' + recheckObject)















Alternative approach if you want to pass the value directly:

def static TestObject updateAttributeByValue(
        TestObject objectFocus,
        String attributeName,
        String currentValue,  // Pass the value explicitly
        type = null,
        String data) {
    
    if (objectFocus != null && attributeName != null && currentValue != null && data != null) {
        String updatedValue = null
        
        if (type == null || type == '' || type == 1) {
            updatedValue = currentValue + data  // Append suffix
        }
        else if (type == 2) {
            updatedValue = data + currentValue  // Prepend prefix
        } else if (type == 3) {
            updatedValue = data  // Replace entirely
        }
        
        objectFocus.getProperties().clear()
        TestObject objectNew = objectFocus.addProperty(attributeName, ConditionType.EQUALS, updatedValue)
        
        return objectNew
    }
    return null
}

// Usage:
originalValue = object1.findPropertyValue('xpath')
objectNew = objectUtils.updateAttributeByValue(object1, 'xpath', originalValue, 1, suffix)

2 Likes

Ah, my bad, setting wrong data input, thanks a lot man