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

Error by using selenium #1

Closed
dullus opened this issue Jan 26, 2012 · 8 comments
Closed

Error by using selenium #1

dullus opened this issue Jan 26, 2012 · 8 comments

Comments

@dullus
Copy link

dullus commented Jan 26, 2012

When I test acceptance test with Selenium I got some bug. Maybe it is related to Xpath element positionoing ? All other test with selenium runs just fine, just this one throws error.

  • Selenium 2.15.0 + Java 1.6.0.21
  • Codecept version 1.0.1
  • PHP 5.3.8 (32b)
  • Win7 64b

searchAjaxCept.php:

<?php
$I = new WebGuy($scenario);
$I->wantToTest("EHMK/searchAjax");
$I->amOnPage("/");

/*for Selenium*/ 
$I->fillField("html body.lang-sk div#wrapper.container_12 div#main.container_12 div#box_search form#search_form fieldset div#search_bg input[name=search-input]", "mus");
//now ajax fires and finds "music" and "museums"
//JS parses ajax output creates <ul><li><a>music</></li><li><a>museums</a></li></ul>
//now I simulate click on <a> in second <li>
//XPath
$I->click("/html/body/ul/li[2]/a");
$I->see("museums");
?>

D:\codecept run acceptance --debug

There was 1 error:

Couldn't test ehmk/searchajax (searchAjaxCept.php)
  Exception thrown Symfony\Component\CssSelector\Exception\ParseException:
  Unexpected symbol: / at 0
  Stack trace:
   #1 tokenizeSymbol D:\devel\apache\php\PEAR\Symfony\Component\CssSelector\Tokenizer.php:99
   #2 tokenize D:\devel\apache\php\PEAR\Symfony\Component\CssSelector\CssSelector.php:93
   #3 parse D:\devel\apache\php\PEAR\Symfony\Component\CssSelector\CssSelector.php:61
   #4 toXPath D:\devel\apache\php\PEAR\mink\src\Behat\Mink\Selector\CssSelector.php:27
   #5 translateToXPath D:\devel\apache\php\PEAR\mink\src\Behat\Mink\Selector\SelectorsHandler.php:94
   #6 selectorToXpath D:\devel\apache\php\PEAR\mink\src\Behat\Mink\Element\Element.php:67
   #7 findAll D:\devel\apache\php\PEAR\mink\src\Behat\Mink\Element\Element.php:57
   #8 find D:\devel\apache\php\PEAR\Codeception\src\Codeception\Util\Mink.php:127
   #9 findEl D:\devel\apache\php\PEAR\Codeception\src\Codeception\Util\Mink.php:109

I cannot use CSS selector unless some changes are made in web page source, so then I get:

There was 1 failure:

Couldn't test ehmk/searchajax (searchAjaxCept.php)
 Exception thrown PHPUnit_Framework_AssertionFailedError:
 Link or Button or CSS for 'html body.lang-sk ul.ui-autocomplete li.ui-menu-item a.ui-corner-all' not found'
 Stack trace:
   #1 fail D:\devel\apache\php\PEAR\Codeception\src\Codeception\Util\Mink.php:128
   #2 findEl D:\devel\apache\php\PEAR\Codeception\src\Codeception\Util\Mink.php:109
@DavertMik
Copy link
Member

You can't use XPath as a selector, Thus, click("/html/body/ul/li[2]/a") won't work.
Better use link names in 'click' action.

Also, from my perspective, it's good to avoid that complex paths. As designs and markups may change, and that shouldn't break tests.

Don't use comments in your scenario. Better use methods amGoingTo and expect, for commenting. Writing your comments with them makes scenarios more readable.

@dullus
Copy link
Author

dullus commented Jan 26, 2012

Using complex paths is intentional, our acceptance tests should fail if somebody changes CSS or UI elements. Comments were added only to this issue ticket as explanation.

Thanks anyway, any plans to support XPath in future versions ?

I have also one question when running test with phpbrowser:

$I->sendAjaxGetRequest("/url/", array('term' => "mus"));

how can I access ajax result within test ?

@DavertMik
Copy link
Member

I will think on adding XPath. As for me it looks much harder to read XPath then CSS selectors and names.

As for ajax request... If your request returns HTML, your see commands will be executed for that response.

@dullus
Copy link
Author

dullus commented Jan 26, 2012

I mean, in debug mode I see json response, can I get that string somewhere from $I object ?

Suite acceptance started
Trying to  test ehmk/searchajax (searchAjaxCept.php)
Scenario:
* I am on page "/"
* I send ajax get request ["/url/",{"term":"mus"}]
=> Request (GET): /url/?term=mus {"term":"mus"}
=> Response code: 200
=> {"results":[{"name":"lmusic","url":"/lst?cat=1050","desc":""},
{"name":"museums","url":"/lst?cat=1031","desc":""}],"code":200,"status":"OK"}

@DavertMik
Copy link
Member

I assume you want to check your JSON response? The $I can't return anything, it's a basic limilitation. All actions and assertions can be performed as a part of $I instance. I recommend you to write helpers for your custom assertions. Check out class 'tests/_helpers/WebHelper' and Modules section of documentation. From your helper you can access current Mink session instance and perform any actions on it.

@dullus
Copy link
Author

dullus commented Jan 27, 2012

I don't want that custom function in WebHelper makes assertion. I want that it returns variable that can be used in next fillField test. For example:

$I->sendAjaxGetRequest("/url/", array('term' => "mus"));
$name = $I->getAjaxResponse("name");
//getAjaxResponse is defined in WebHelper, does its magic with mink session,
//retrieves "name" from response JSON and will return it
$I->fillField("input#search_input", $name);

And here is another problem, I had no luck to connect to that mink session from WebHelper::getAjaxResponse. I need something like in module/PhpBrowser.php function call() :

$this->session->getStatusCode()

so I can parse it in WebHelper::getAjaxResponse and return value (or if it is not possible that $I methods return value, then I will do at least assertion in helper). Can you write me example how to access current mink session from WebHelper class ?

@DavertMik
Copy link
Member

Sure. That principle is covered in Modules part of Guides.

In helper:

<?php
$session = $this->getModule('Selenium')->session;
?>

Every module has it's public API. You can execute all public methods and properties from other modules and helpers.

@dullus
Copy link
Author

dullus commented Jan 30, 2012

Thank you, works now as I want :-) Now I can do all needed ajax asserts in webhelper.

$session = $this->getModule('PhpBrowser')->session;
$result = $session->getStatusCode();
$result2 = $session->getResponseHeaders();
$result3 = $session->getCurrentUrl();
$result4 = json_decode($session->getPage()->getContent(), true);

DavertMik pushed a commit that referenced this issue Apr 3, 2013
DavertMik pushed a commit that referenced this issue Mar 14, 2014
Naktibalda added a commit that referenced this issue Jun 22, 2015
Naktibalda pushed a commit that referenced this issue Sep 1, 2015
DavertMik pushed a commit that referenced this issue Apr 29, 2019
* [Laravel5] Fix issue #5391

* [Laravel5] Fix issue #4897

* Update Laravel5.php (#1)
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

2 participants