Search code examples
xpathcucumberselenide

Handling <br> tag in xpath through cucumber


I'm using cucumber with Selenide frameworks, I have the following step in cucumber's regression.feature file:

Then Check if has a section labelled "Foo bar foo bar"

In Steps.java file I have:

@Then("^Check if has a section labelled \"([^\"]*)\"$")
public void checkThatHomePageHassectionLabelled(String arg0) throws Throwable {
    servicesSectionCheck(arg0);
}

And I would like to check if:

public static void servicesSectionCheck(String name) {
    $(byXpath("//h1[(text()='" + name + "')]")).shouldBe(visible);
}

The problem is sometimes when the browser is smaller (RWD issue), the HTML is looking like

<div id="services" class="page_section row">
<input type="hidden" id="Count1" value="4">
<input type="hidden" id="Count2" value="2">
<input type="hidden" id="Count3" value="10">
<input type="hidden" id="loadMoreServicesURL" value="anURLhere">
<input type="hidden" id="portletNamespace" value="_serviceslist_INSTANCE_t8TJHYdgnwCu_"> 
<div class="col-12 desktop-padding"> 
    <div class="d-none d-sm-block margin-top-50"></div> 
    <h1 class="text-center d-none d-sm-block">Foo bar <br> foo bar</h1> 
<div class="d-none d-sm-block margin-bottom-40"></div>(...)

And sometimes

<h1 class="text-center d-none d-sm-block">Foo bar foo bar</h1>

How can I set an Xpath in servicesSectionCheck function to pass despite if <br> tag is presented or not?


Solution

  • Instead of evaluating child text node (text()) you can evaluate string representation of header:

    //h1[normalize-space()='" + name + "']