TMH
TMH

Reputation: 6246

Mink/Selenium can't find a button

I'm trying to run this scenario

@javascript
Scenario: Greeting in an alert box
    Given I am on "/"
    And I get content
    When I press "say"
    And I wait for the greeting to appear
    Then I should see "Hello JavaScript!"

With my debug step

/**
 * @Given I get content
 */
public function iGetContent(){
    echo $this->getSession()->getPage()->getContent();
}

However, it reports it cannot find the button, when (I believe) it should be able to

@javascript
  Scenario: Greeting in an alert box      # features/home.feature:11
    Given I am on "/"                     # FeatureContext::visit()
    And I get content                     # FeatureContext::iGetContent()
      │ <html lang="en"><head></head><body>
      │         <h1>Home</h1>
      │         <span id="js-greeting"></span>
      │         <button type="button" id="say" value="say">say</button>
      │     
      │ 
      │     <script>
      │         function showGreeting(){
      │             document.getElementById("js-greeting").innerHTML = "Hello JavaScript!"
      │         }
      │ 
      │         function delayedGreeting(){
      │             window.setTimeout(showGreeting, 1000);
      │         }
      │     </script>
      │ 
      │ </body></html>
    When I press "say"                    # FeatureContext::pressButton()
      Button with id|name|title|alt|value "say" not found. (Behat\Mink\Exception\ElementNotFoundException)
    And I wait for the greeting to appear # FeatureContext::iWaitForTheGreetingToAppear()
    Then I should see "Hello JavaScript!" # FeatureContext::assertPageContainsText()

Selenium launches Firefox, and I can see that the page successfully loads, and I can physically see the button. Is there something I've missed? Here's my composer.json/behat .yml in case there's something here I've missed.

{
    "name": "",
    "description": "",
    "keywords": [],

    "repositories": [
        {
            "type": "vcs",
            "url": "[email protected]:xxx.git"
        }
    ],
    "require": {
        "php": "^5.6 || ^7.0",
        "judgeservice/mvc": "dev-master",
        "php-amqplib/php-amqplib": ">=2.6.1",
        "behat/behat": "3.0.6",
        "behat/mink": "1.6.*",
        "behat/mink-extension": "*",
        "behat/mink-selenium2-driver": "*",
        "behat/mink-goutte-driver": "*",
        "laravel/framework": "4.2.*",
        "behat/mink-zombie-driver": "*"
    },
    "autoload": {
        "psr-4": {
            "Application\\": "module/Application/src/"
        }
    },

    "require-dev": {
        "phpunit/phpunit": "^5.7"
    }
}
default:
    extensions:
        Behat\MinkExtension:
            base_url: "http://api.example.com"
            browser_name: 'firefox'
            goutte: ~
            selenium2: ~
public function pressButton($button)
{
    $button = $this->fixStepArgument($button);
    $this->getSession()->getPage()->pressButton($button);
}


protected function fixStepArgument($argument)
{
    return str_replace('\\"', '"', $argument);
}

public function pressButton($locator)
{
    $button = $this->findButton($locator);

    if (null === $button) {
        throw $this->elementNotFound('button', 'id|name|title|alt|value', $locator);
    }

    $button->press();
}



public function findButton($locator)
{
    return $this->find('named', array(
        'button', $this->getSelectorsHandler()->xpathLiteral($locator),
    ));
}
/**
 * @Given I hit say
 */
public function iHitSay(){
    $button = $this->getSession()
            ->getPage()
            ->find('css', '#say');

    $button->press();
}

Upvotes: 1

Views: 877

Answers (1)

lewis
lewis

Reputation: 33

These solutions helped me:

https://github.com/minkphp/MinkSelenium2Driver/issues/293#issuecomment-519920991

https://github.com/Behat/MinkExtension/issues/345#issuecomment-510712489

The problem is starting with Chrome 75 version and above.

Upvotes: 0

Related Questions