Not able to get correct text of an element

Hello Team,

I have an element for which I am trying to verify Text but not able to do so.

The script is something like this.

WebUI.verifyElementPresent(findTestObject('Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title'), 0)

Size_Guide_Header_Text = WebUI.getText(findTestObject('Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title'), FailureHandling.STOP_ON_FAILURE)

CustomKeywords.'kms.turing.katalon.plugins.assertj.StringAssert.equals'('Size Guide', Size_Guide_Header_Text, true, 'To Test the Size Guide Header Text', 
    FailureHandling.STOP_ON_FAILURE)

The error I am getting is :

06-21-2019 10:43:10 AM Test Cases/QATB-182 - Size Guide Testing/QATB-182 - Automate Testing of Size Guide

Elapsed time: 16.465s

kms.turing.katalon.plugins.assertj.GenericAssert.handleError:71

kms.turing.katalon.plugins.assertj.GenericAssert.handleError:85

kms.turing.katalon.plugins.assertj.StringAssert.equals:103

kms.turing.katalon.plugins.assertj.GenericAssert.invokeMethod:0

Test Cases/QATB-182 - Size Guide Testing/QATB-182 - Automate Testing of Size Guide FAILED.
Reason:
com.kms.katalon.core.exception.StepFailedException: To Test the Size Guide Header Text.
Expected: equals to '' with case-sensitive: true.
Actual: Size Guide

From the error, it looks like the selector of the element is not able to get the text correctly

You’re expecting a different text, the actual text is 'Size Guide’s which is what you want. You should check the assertion to see if the expecting text is indeed ‘Size Guide’.

Cheers !

2 Likes

@ThanhTo I fixed that part sorry it was my mistake, Now the code is something like this

/* Clicking on Size Guide */
WebUI.click(findTestObject('Page_PDP/Size_Guide'))

WebUI.verifyElementPresent(findTestObject('Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title'), 0)

Size_Guide_Header_Text = WebUI.getText(findTestObject('Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title'), FailureHandling.STOP_ON_FAILURE)

CustomKeywords.'kms.turing.katalon.plugins.assertj.StringAssert.equals'(Size_Guide_Header_Text, 'Size Guide', true, 'To Test the Size Guide Header Text', 
    FailureHandling.STOP_ON_FAILURE)

I am still getting error :


Test Cases/QATB-182 - Size Guide Testing/QATB-182 - Automate Testing of Size Guide FAILED.
Reason:
com.kms.katalon.core.exception.StepFailedException: To Test the Size Guide Header Text.
Expected: equals to 'Size Guide' with case-sensitive: true.
Actual: 
	at com.kms.katalon.core.util.KeywordUtil.markFailedAndStop(KeywordUtil.java:28)
	at com.kms.katalon.core.util.KeywordUtil$markFailedAndStop$1.call(Unknown Source)
	at kms.turing.katalon.plugins.assertj.GenericAssert.handleError(GenericAssert.groovy:71)
	at kms.turing.katalon.plugins.assertj.GenericAssert.handleError(GenericAssert.groovy:85)
	at kms.turing.katalon.plugins.assertj.StringAssert.equals(StringAssert.groovy:103)
	at kms.turing.katalon.plugins.assertj.GenericAssert.invokeMethod(GenericAssert.groovy)
	at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:50)
	at QATB-182 - Automate Testing of Size Guide.run(QATB-182 - Automate Testing of Size Guide:89)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
	at TempTestCase1561143993249.run(TempTestCase1561143993249.groovy:21)

You probably need to trim the text you retrieve from the application:

Size_Guide_Header_Text = WebUI.getText(findTestObject('Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title'), FailureHandling.STOP_ON_FAILURE).trim()

This is because there’s a bunch of whitespace in the text node:

image

1 Like

@Brandon_Hein I tried but it is giving the same error.

<span class="fit-title" xpath="1">
                                Size Guide
                            </span> 

Code :

WebUI.verifyElementPresent(findTestObject('Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title'), 0)

Size_Guide_Header_Text = WebUI.getText(findTestObject('Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title'), FailureHandling.STOP_ON_FAILURE).trim()

CustomKeywords.'kms.turing.katalon.plugins.assertj.StringAssert.equals'(Size_Guide_Header_Text, 'Size Guide', true, 'To Test the Size Guide Header Text', 
    FailureHandling.STOP_ON_FAILURE)

Error :

06-25-2019 09:44:06 AM kms.turing.katalon.plugins.assertj.StringAssert.equals(Size_Guide_Header_Text, "Size Guide", true, "To Test the Size Guide Header Text", STOP_ON_FAILURE)

Elapsed time: 0.012s

To Test the Size Guide Header Text.
Expected: equals to 'Size Guide' with case-sensitive: true.
Actual: 

Ok we need to see what is being returned by the getText() call. Please print the value after it is retrieved:

Size_Guide_Header_Text = WebUI.getText(findTestObject('Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title'), FailureHandling.STOP_ON_FAILURE).trim()
System.out.println(Size_Guide_Header_Text)

Also, the

CustomKeywords.'kms.turing.katalon.plugins.assertj.StringAssert.equals'()

is your custom implementation, so it may not be working as you expect. Can you share the code for this keyword?

I just realized that the

CustomKeywords.'kms.turing.katalon.plugins.assertj.StringAssert.equals'()

probably isn’t a keyword that you wrote, but rather a keyword from a plugin. As such, try just doing a simple assertion like this, and see if it makes any difference:

assert Size_Guide_Header_Text.equals("Size Guide")

@Brandon_Hein It looks like there is a line break in the element.

The console log looks like this when I printed the text from getText() call

2019-06-25 10:03:58.387 DEBUG ATB-182 - Automate Testing of Size Guide - 18: Size_Guide_Header_Text = getText(findTestObject("Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title"), STOP_ON_FAILURE).trim()
2019-06-25 10:03:58.426 DEBUG ATB-182 - Automate Testing of Size Guide - 19: out.println(Size_Guide_Header_Text)

2019-06-25 10:03:58.435 DEBUG ATB-182 - Automate Testing of Size Guide - 20: assert Size_Guide_Header_Text.equals("Size Guide")
2019-06-25 10:03:58.440 ERROR c.k.katalon.core.main.TestCaseExecutor   - ❌ Test Cases/QATB-184 - Size Guide Testing/QATB-182 - Automate Testing of Size Guide FAILED.
Reason:
Assertion failed: 

assert Size_Guide_Header_Text.equals("Size Guide")
       |                      |
       ""                     false

	at QATB-182 - Automate Testing of Size Guide.run(QATB-182 - Automate Testing of Size Guide:105)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
	at TempTestCase1561482213510.run(TempTestCase1561482213510.groovy:21)

Hmmm it’s not even a line break, it’s an empty string, as shown in the assertion:

assert Size_Guide_Header_Text.equals("Size Guide")
       |                      |
       ""                     false

Are you sure you are targeting the right element?

@Brandon_Hein I think so,

I have xpath set as

That xpath may be returning multiple elements. Test it in your browser console by hitting F12, going to the “Console” tab, enter the following, and see what you get back:

$x("//*[@class='fit-title']")

You are right, it is returning 3 elements.

Yea, but in this case that doesn’t matter, the one you want is the first one that is being returned anyway (you should definitely be careful of this in general though). Try this instead and see what you get:

Size_Guide_Header_Text = WebUI.getAttribute(findTestObject('Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title'), "textContent", FailureHandling.STOP_ON_FAILURE).trim()
System.out.println(Size_Guide_Header_Text)
1 Like

@Brandon_Hein I tried this approach with the same selector and it worked.

2019-06-25 11:01:35.991 DEBUG ATB-182 - Automate Testing of Size Guide - 17: Size_Guide_Header_Text = getAttribute(findTestObject("Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title"), "textContent", STOP_ON_FAILURE).trim()
2019-06-25 11:01:36.035 DEBUG ATB-182 - Automate Testing of Size Guide - 18: out.println(Size_Guide_Header_Text)
Size Guide
2019-06-25 11:01:36.045 DEBUG ATB-182 - Automate Testing of Size Guide - 19: assert Size_Guide_Header_Text.equals("Size Guide")

Nice, so your assertion should work now. Not sure why WebUI.getText() wasn’t doing what you expected (because I never use the WebUI API). I find that getting the textContent attribute value is often more reliable than using any kind of getText() method, but that’s just me :smile:

2 Likes

@Brandon_Hein Thanks for your help again. It is always something new to learn from you. I do have some other areas with the same issue and I think I can use this approach.

2 Likes