Behat/Mink — Issue #131: Encoding issue with Sahi and get option value with utf8 chars
Author: gabrielpillet
Created: 2011-11-23 13:42:59+00:00
State: open

Given I've got this HTML page :

```html

Foobar

There is a non breaking space

Some accentués characters

Gimme some accentués characters

```

And I run the following test :

```
Feature: Testing FR accents

@javascript
Scenario: accent and non breaking space
Given I am on "foobar.html"
Then I should see "Some accentués characters"
When I select "Gimme some accentués characters" from "foobar"
```

Here is the result :

```
Feature: Testing FR accents

@javascript
Scenario: accent and non breaking space
Given I am on "foobar.html"
Then I should see "Some accentués characters"
When I select "Gimme some accentués characters" from "foobar"
Select option with value|text "Gimme some accentués characters" not found

1 scenario (1 failed)
3 steps (2 passed, 1 failed)
```

Going deeper in this issue, I found that in SahiDriver::getContent()

```php
evaluateScript('document.getElementsByTagName("html")[0].innerHTML');

$html = preg_replace(array(
'/<\!--SAHI_INJECT_START--\>.*\<\!--SAHI_INJECT_END--\>/sU',
'/\<\!\[CDATA\[\*\/\/\*----\>\*\/__sahi.*\<\!--SAHI_INJECT_END--\>/sU'
), '', $html);
$html = html_entity_decode($html);

return "\n$html\n";
```

The $html var after `$html = html_entity_decode($html);` contains a non utf8 character � in place of the non breaking space.

You can see it using with this code :

```php

```

When $html is then given to a \DOMDocument object and then parsed with xpath, \DOMDocument does not detect the content as UTF8 and reencode it. When comparing with the given value "Gimme some accentués characters" is not found because compared to a reencoded DOM.

Providing the charset to `html_entity_decode` solves this issue.

```php

```

An empty string also work if you correctly set your default charset/locale.

```php

```

I don't know what is the standard regarding charset in Behat, so I did not want to make a PR, but I'll do it if you validate one option or the other (or something else working).

Looks like there is also an issue with PHP, some people seems to have the same problem (see http://php.net/manual/en/function.html-entity-decode.php)