Search code examples
testingautomationbehatmink

How to access onClick attribute in behat+mink?


We have two button named “Inschrijven” in Tab1 section and Tab2 section as shown in below image enter image description here

Tab 1:
<span class="form-submit-wrapper"><input type="submit" id="edit-submit--3" name="op" value="Inschrijven" class="form-submit"></span>

Tab 2:
<span class="form-submit-wrapper"><input onclick="return validate_course_anywhere(this)" type="submit" id="edit-submit--22" name="op" value="Inschrijven" class="form-submit"></span>

When I click element with id "edit-submit--3" This behat code scenario is worked in Tab 1 section but not in tab 2 section ( When I click element with id "edit-submit--22")

                               /**
                               * @When /^I click element with id "([^"]*)"$/
                               */
                              public function iClickElementWithId($class) {
                                            $locator = "#$class";
                                            $element = $this->getSession()->getPage()->find('css', $locator);

                                            if (null === $element) {
                                                            throw new \InvalidArgumentException(sprintf('Could not evaluate CSS selector: "%s"', $locator));
                                            }

                                            $element->click();
                                            $this->getSession()->wait(1000);
                              }

But error message is displaying in Tab 2 section:

Exception thrown by (//html/descendant-or-self::*[@id = 'edit-submit--22'])[1]
Element is not clickable at point (813.5, 16.666671752929688). Other element would receive the click: <a href="/certificering" title="" class="menu__link"></a>

Solution

  • It seems that the selector is ok, the issue here is that is not clickable and some other element is in the front of it and receives the click.

    Some of the possible issues: 1. wait for the element to be visible 2. you have 2 elements with the same id and the first one is found but not clickable 3. wait for the page to be loaded if needed

    To use the onClick attribute you can use a css selector like: [onclick*='some_value_from_attribute']

    You should have a method to click by selector and not particular method for just one attribute.

    Another thing to avoid is using blind waits, you should try to use conditional waits.