Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any way to press a <button> instead of <input type=submit> #96

Closed
sameerpanjwani opened this issue Jun 9, 2015 · 6 comments
Closed

Comments

@sameerpanjwani
Copy link

Any way to "press" a button , doesn't seem to work.

use Laracasts\Integrated\Extensions\Selenium;

use Laracasts\Integrated\Services\Laravel\Application as Laravel;

class ValidationTest extends Selenium
{

    use Laravel;

function test_basic_validation()
    {
        $this->visitTestPage()
            ->see('Login')
            ->type('abc@def.com', 'email')
            ->type(' ', 'secret_password')
            ->press('Submit This')
            ->wait(2000)
            ->see("Success")
  }

Html

<button type="submit">Submit This</button>

Error:

Curl error thrown for http POST to http://localhost:4444/wd/hub/session/e2d34247-e11f-4a2a-8bed-1f6e79d67d99/element with params: {"using":"css selector","value":"input[value='Submit This']"}

@joedawson
Copy link

Could try popping an ID onto the button and using the click() method instead?

$this->click('#buttonID')

@alfredbez
Copy link
Contributor

Seems like this is a duplicate of #85

@joedawson click works only for links.

@joedawson
Copy link

@alfredbez that's not correct, as per the docs - I've also been using it this week :)

While it's easiest if you pass the text content of the desired anchor tag to the click method (like "Sign Up"), you may also use the anchor tag's name or id attributes if you wish.

@alfredbez
Copy link
Contributor

@joedawson
From here:

To find a link by name (or a clickable image by its alt attribute), use the selectLink method on an existing crawler.

Or like you already said:

[...] of the desired anchor tag [...]

This is not a link or an anchor tag:

<button type="submit">Submit This</button>

and from here:

Behind the scenes, this package will determine that destination of the link (the "href"), and make a new "GET" request, accordingly.

From the docs

Not to be confused with click, the press method is used to submit a form with a submit button that has the given text.

Please take also a look at the click method (source code)

@alfredbez
Copy link
Contributor

Looks like the click method from the Selenium-Extension should work also for other elements, like button.

I tried it, but I see the following error:

WebDriver\Exception\CurlExec: Curl error thrown for http POST to http://192.168.56.1:4444/wd/hub/session/684d95dc-d3a3-4129-ab75-7ff928c65996/element with params: {"using":"link text","value":"#buttonId"}

The requested URL returned error: 500

/var/www/html/my_project/vendor/instaclick/php-webdriver/lib/WebDriver/Exception.php:155
/var/www/html/my_project/vendor/instaclick/php-webdriver/lib/WebDriver/Service/CurlService.php:104
/var/www/html/my_project/vendor/instaclick/php-webdriver/lib/WebDriver/AbstractWebDriver.php:123
/var/www/html/my_project/vendor/instaclick/php-webdriver/lib/WebDriver/Container.php:67
/var/www/html/my_project/vendor/laracasts/integrated/src/Extensions/Selenium.php:116
/var/www/html/my_project/vendor/laracasts/integrated/src/Extensions/Selenium.php:91
/var/www/html/my_project/tests/MyTest.php:63

FAILURES!
Tests: 1, Assertions: 0, Errors: 1.

Selenium Error:

20:06:01.755 WARN - Exception: Unable to locate element: {"method":"link text","selector":"#buttonId"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.45.0', revision: '5017cb8', time: '2015-02-26 23:59:50'
System info: host: 'HOST', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-37-generic', java.version: '1.7.0_79'

PHP-Code:

$this->visit('/siteWithButton')->click('#buttonId');

It works as expected when I swap the lines 91 and 93 or when I use the following code in my test:

$this->visit('/siteWithButton')->findByNameOrId('#buttonId')->click();

@alfredbez
Copy link
Contributor

I came up with the following solution:

I created an abstract Test Class which extends the Selenium Extension Class.

It looks like this:

<?php

use Laracasts\Integrated\Extensions\Selenium as IntegrationTest;

abstract class AbstractSeleniumTest extends IntegrationTest {

  protected function findByCssSelector($selector)
  {
      try {
          return $this->session->element('css selector', $selector);
      } catch (NoSuchElement $e) {
          throw new InvalidArgumentException(
              "Couldn't find an element, matching the follwing css selector: '{$seletor}'."
          );
      }
  }

  public function clickCss($selector)
  {
    $this->findByCssSelector($selector)->click();

    return $this;
  }

}

It gives me a more flexible way to click on elements, e.g.

$this->clickCss("#someElement + button");
<div id="someElement">an Element</div>
<button>I have no ID and name!</button>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants