Search code examples
phpseleniumlaravel-5behatmink

how to click a button under a nested div using xpath and class in behat3


I have a nested div structure with a button element in it. I want to check the click and redirect functionality on it via behat test cases, But I am not able to test it as error shows up.

My page HTML is something like this -

<div id="divSubmit1">
      <div id="subDivSubmit1">...</div>
      <div class="row  mt-20 ">...</div>
      <div class="row mt-20">
             <div class="col-xs-offset-7 col-xs-5 mt-20 mb-10 text-center">
                 <button class="btn btn-success btn-lg mt-20" type="submit" name="cmd_btn_submit" onclick="return check_input()">Submit
                     <span class="btn-icon"><i class="fas fa-chevron-right"></i></span></button>
             </div>
        </div>
</div>

Have tried the following

This is my step in feature file

And I click on button "cmd_btn_submit"

and my context file has

     * @And I click on button :arg1
     * @Then I click on button :arg1
     */
    public function iClickOnButton($arg1)
    {
       $this->getSession()->getPage()->find("xpath", "//button[div[@id='divSubmit1'][contains(text(),'".$arg1."')]]
")->click();

}

Getting this error,

And I click on button "cmd_btn_submit"                                                                                   # Shop\Features\Context\FeatureContext::iClickOnButton()
      Fatal error: Call to a member function click() on null (Behat\Testwork\Call\Exception\FatalThrowableError)

Solution

  • The XPath is wrong, plus your argument needs to be button text, not the Id of the button.

    Your xpath should be:

    "//div[@id='divSubmit1']//button[contains(text(), '$arg1')]"
    

    And your cucumber step should be:

    And I click on button "Submit"