Cannot find elements when XPath expression is null

I have made a project on GitHub, where you can reproduce the insident.


OS: Mac OS 10.13.6 High Siearra

Katalon Studio: version 5.7.1

How to reporoduce the insident

  1. git clone this project or download ZIP form the relases page.
  2. open the project with Katalon Studio v5.7.0 or higher.
  3. open Test Caset/TC1 and execute it. The source is as follows:
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

WebUI.openBrowser('')
WebUI.navigateToUrl('http://www.angularjshub.com/examples/forms/textinputs/')
WebUI.verifyElementPresent(findTestObject('Object Repository/Page_AngularJS Hub  Text Inputs/iframe_Example_exampleIFrame'), 10)
WebUI.closeBrowser()

The test case will fail with the following message:

java.lang.IllegalArgumentException: Cannot find elements when the XPath expression is null

What is expected

The test case should pass.

What Actually occurs

The test case fails.

Unable to verify object 'Object Repository/Page_AngularJS Hub  Text Inputs/iframe_Example_exampleIFrame' is present
  (Root cause: java.lang.IllegalArgumentException: Cannot find elements when the XPath expression is null.)

TestCaseFailed

My investigation

The Test Object ‘Object Repository/Page_AngularJS Hub Text Inputs/iframe_Example_exampleIFrame’ was generated by the Spy Web. It has Selection Method: XPath , and it seems to have a valid Selector: //iframe[@id='exampleIFrame'] . TestObjectInitial

But the message is saying that *‘the XPath is null’. This is the point I am curious about.

I did 2 experiments.

manually turned to Selector: Attributes

I changed manually the Test Object to have Selection Method: Attributes and set Selector by tag and id: //iframe[@id = 'exampleIFrame'] .

TestObjectEdited

When I ran the Test Cases/TC1 , it PASSED.

manually turned back to Selector: XPath

After this fist experiment, I changed mannually the Test Object to have Selection Method: XPath . It got a valid selector: //iframe[@id='exampleIFrame'] . This is just the same as the one generated by Spy Web. When I ran the Test Cases/TC1 , it PASSED.

Once passed, the Test Cases/TC1 will PASS forever. The only way to make it FAIL again is to clone the project from the GitHub repository and start fron scratch.

Conclusion

I believe there is a small bug in Katalon Studion 5.7.

I believe that the message “Cannot find elements when XPath expression is null” started appearing since 5.7.0 which changed the specificaiton of Test Object: Selection method – XPath, Attributes, CSS

I think that the Test Object was generated by Spy Web in a internal state somewhat incomplete, which caused the message Cannot find elements when XPath expression is null comes up. I manually edited the Test Object — this operation seems to change the internal state, and hides the problem.

I guess that upgrading old version Katalon Studio to newer version (>=5.7.0) causes similar problem.

In the Katalon Forum I have seen a few posts reporting the same message.

4 Likes

I can verify that I am seeing this too with version 5.8.3 Build 2

I tried running my code using the above mentioned steps, but it still fails

Settings I changed:

the error I get:

image.png

error.PNG

1 Like

Hi, I am getting this same issue as well. I did not know this workaround, but what I did until now when the object was caught by the spy web returned the xpath null, I tried to capture the same object with the recorder and overwritte the old object captured with spy web and it works. The 2 objects are identical. So there is no dought this is a bug and a very serious one. Specially when the main objective of Katalon is to give users or manual testers which can not program yet the possibility to create automatic tests.

Does anyone knows if the development team of Katalon Studio is planning to fix this? if the issue started at version 5.7.0 and now we are at this date at version 5.8.6 it is quite many versions without fixing…

I am same question as well. We are new to Katalon and are not able to get pass this issue

One more case reported:

1 Like

One more case reported

1 Like

I am still seeing this issue in 5.9.1 as well

I recently installed Katalon Studio and experienced this same problem trying to create a few initial testcases using the “Record Web” function. Is the problem specifically with web pages that include an iframe? Does anyone have any idea when it might get fixed? We are currently evaluating the use of Katalon Studio and the initial page that is displayed includes an iframe.

Hi all,
This issue has been fixed with latest version(5.10.0), please grab it to double check.

To be precise, version 5.10.0

2 Likes

I believe the issue has been addressed.

Katalon was completely unable to locate an iframe during execution by either xpath or attributes using version 5.8.5 just a few minutes ago. The execution failed with the “xpath is null” error immediately after recording, but by manually editing the selector after the fact I could get it to fail with errors stating that it was unable to locate the element by the xpath instead. This was especially frustrating considering that the web spy would consistently locate the button I wanted to interact with using the same selector text.

After updating to 5.10.0 I started the test case over by recording my desired workflow and it played back what it recorded without need for modification from me. So far the update appears to have improved and perhaps rectified this issue, at least for me.

Thanks!

2 Likes

I checked the version v5.10.0 build1 on Windows7.
I cloned the problem-reproducing project
GitHub - kazurayam/Cannot_find_elements_when_XPath_expression_is_null and executed the test case TC1.

Unfortunately I encountered the problem (Root cause: java.lang.IllegalArgumentException: Cannot find elements when the XPath expression is null.)
log.txt (32.3 KB)

The problem still exists.

1 Like

@kazurayam hmm, something stranger here, It’s will be double check again by development team. Thanks u for the issue :smiley:

I have found out the cause of the problem in https://github.com/kazurayam/Cannot_find_elements_when_XPath_expression_is_null

Let me explain it.

I changed the test case TC1 . I inserted a few lines as follows:

import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature

TestObject to = findTestObject('Object Repository/Page_AngularJS Hub  Text Inputs/iframe_Example_exampleIFrame')

ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT)

println mapper.writeValueAsString(to)

When I run the TC1, the TestObject instance is printed as a JSON format string.

I ran TC1 twice. Once the error occurred. Then I did my “workaround”. Then one more run — One more time when no error occurred. I saved the JSON string into

I made a diff of a.json and b.json: diff.txt

64c64,67
<   "selectorCollection" : { },
---
>   "selectorCollection" : {
>     "BASIC" : "//iframe[@id = 'exampleIFrame' and @src = '/code/examples/forms/01_TextInputs/index.demo.php']",
>     "XPATH" : "//iframe[@id='exampleIFrame']"
>   },

Let me tell you what happened. The original Test Oject, as checked out of the repository, is printed like:

...
  "objectId" : "Object Repository/Page_AngularJS Hub  Text Inputs/iframe_Example_exampleIFrame",
  "imagePath" : null,
  "useRelativeImagePath" : false,
  "selectorMethod" : "XPATH",
  "selectorCollection" : { },
...

The Test Object in this status caused the error “Cannot find elements when XPath expression is null”.

After I did the workaround, the Test Object was changed to be:

"objectId" : "Object Repository/Page_AngularJS Hub  Text Inputs/iframe_Example_exampleIFrame",
  "imagePath" : null,
  "useRelativeImagePath" : false,
  "selectorMethod" : "XPATH",
  "selectorCollection" : {
    "BASIC" : "//iframe[@id = 'exampleIFrame' and @src = '/code/examples/forms/01_TextInputs/index.demo.php']",
    "XPATH" : "//iframe[@id='exampleIFrame']"
  },

I think this difference of selectorCollection element must be the cause of the problem.

I suppose that the selectorCollection element was introduced at v5.7.

It seems that the current Katalon Studio naively expects selectorCollection element to be NonNull. When it encounters empty selectorCollection, it throws IllegalArgumentException.

However, in the Katalon projects created before v5.7, possibly there are many Test Object instances without selectorCollection element. There seems to be some other cases in ver5.7 and later where a Test Object instance is left with empty selectorCollection element.

I want Katalon Studio to be more careful for and robust against those Test Object instance in older format.

2 Likes

Additional comment:

When I ran the Test Case TC1 of https://github.com/kazurayam/Cannot_find_elements_when_XPath_expression_is_null project, I got the following message in the Console:

2018-12-22 20:28:17.159 ERROR c.k.katalon.core.main.TestCaseExecutor   - ❌ Test Cases/TC1 FAILED.
Reason:
com.kms.katalon.core.exception.StepFailedException: Unable to verify object 'Object Repository/Page_AngularJS Hub  Text Inputs/iframe_Example_exampleIFrame' is present (Root cause: java.lang.IllegalArgumentException: Cannot find elements when the XPath expression is null.)
	at com.kms.katalon.core.keyword.internal.KeywordMain.stepFailed(KeywordMain.groovy:36)
	at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:65)
	at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:27)
...

This message does not tell me where the IllegalArgumentException was raised, by which class, by which method in which line. I wanted to see the StackTrace of the IllegalArgumentException. I wondered why the Stack Trace is not printed.

I read the source of VerifyElementPresentKeyword and other related classes.

I would like to propose to Katalon Team to modify the com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain class.

public class WebUIKeywordMain {

    private static final String TIMED_OUT_WAITING_FOR_PAGE_LOAD = "Timed out waiting for page load."

    @CompileStatic
    public static runKeyword(Closure closure, FailureHandling flowControl, boolean takeScreenShot, String errorMessage) {
        try {
            return closure.call();
        } catch (Throwable e) {
            if (isPageLoadTimeoutException(e) && DriverFactory.isEnablePageLoadTimeout() && DriverFactory.isIgnorePageLoadTimeoutException()) {
                stepFailed(errorMessage, FailureHandling.OPTIONAL, ExceptionsUtil.getMessageForThrowable(e), takeScreenShot);
                return;
            }

            // kazurayam wants to insert a line here
            e.printStackTrace()

            stepFailed(errorMessage, flowControl, ExceptionsUtil.getMessageForThrowable(e), takeScreenShot);
        }
    }
2 Likes

I wanted to fix the problem by setting my TestObject with selectorCollection content. I tried to find the way. To my surprise, com.kms.katalon.core.testobject.TestObject class seemd to have no public method which allows me to set selectorCollection content, for example setSelectorCollection(Map m). Therefore it seems impossible for me to fix (or workaround) the problem for myself.

I am wondering how the selectorCollection content can be set by which method of which class.

1 Like

I have created another post

In that post, the problem “Cannot find elements with XPath expression is null” blocked me to implement a solution for “Transforming Test Object on the fly”.

1 Like

Thank you for the suggestion. We will have an improvement in the next version that allows the original stack trace to be printed out.

I could get the stacktrace of the IllegalArgumentException, which is the root cause of the problem.

See

2 Likes