When reading visible XML text from a parsed document element, nested child elements can split the text across separate DOM nodes. The useful value is the concatenated text content (i.e., the single string formed by reading descendant text nodes in document order), because later XML handling often compares or reports the element's readable text.
textContent returns that concatenated text content for a DOM node, and it returns an empty string when the node is missing.[1] The XML setup for this scenario uses parseXml only to produce a document element under XML parsing rules.
Below is a test scenario of the baseline successful case of textContent: nested elements contribute their descendant text in document order.
The scenario
Given the XML <root><a>Hello </a><b>World</b></root> is parsed into a document element,
When textContent is called with that document element,
Then the result is Hello World.
The test fixture
The fixture builds a small XML document, calls textContent on the root element, and checks the returned string.
Below is the test fixture code.
test.openspec('textContent returns concatenated text of nested elements')('Scenario: textContent returns concatenated text of nested elements', async ({ when, then, attachPrettyJson }: AllureBddContext) => {
const xml = '<root><a>Hello </a><b>World</b></root>';
const doc = parseXml(xml);
let result!: string;
await when('textContent is called', async () => {
result = textContent(doc.documentElement);
await attachPrettyJson('Result', { xml, textContent: result });
});
await then('the result SHALL be the concatenated text content', () => {
expect(result).toBe('Hello World');
});
});
The expected result shape
The scenario asserts directly on the string returned by textContent, so the expected result is the literal scalar value from the assertion.[2]
Below is the result that textContent is expected to return for this scenario.
'Hello World'
The returned string is expected to be Hello World, because the root element's descendant text nodes contain Hello followed by World.