Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Merge pull request minkphp#555 from stof/select_tests
Browse files Browse the repository at this point in the history
Tests for select boxes
  • Loading branch information
stof committed May 17, 2014
2 parents f6ca55d + eb2d9f2 commit 1afdb9c
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 104 deletions.
83 changes: 0 additions & 83 deletions driver-testsuite/tests/Form/GeneralTest.php
Expand Up @@ -6,20 +6,6 @@

class GeneralTest extends TestCase
{
/**
* test accentuated option
* @group issue131
*/
public function testIssue131()
{
$this->getSession()->visit($this->pathTo('/issue131.html'));
$page = $this->getSession()->getPage();

$page->selectFieldOption('foobar', 'Gimme some accentués characters');

$this->assertEquals('1', $page->findField('foobar')->getValue());
}

// test multiple submit buttons
public function testIssue212()
{
Expand Down Expand Up @@ -150,75 +136,6 @@ public function testBasicGetForm()
$this->assertEquals('some#query', $div->getText());
}

public function testMultiselect()
{
$this->getSession()->visit($this->pathTo('/multiselect_form.html'));
$page = $this->getSession()->getPage();
$this->assertEquals('Multiselect Test', $page->find('css', 'h1')->getText());

$select = $page->findField('select_number');
$multiSelect = $page->findField('select_multiple_numbers[]');

$this->assertNotNull($select);
$this->assertNotNull($multiSelect);

$this->assertEquals('20', $select->getValue());
$this->assertSame(array(), $multiSelect->getValue());

$select->selectOption('thirty');
$this->assertEquals('30', $select->getValue());

$multiSelect->selectOption('one', true);

$this->assertSame(array('1'), $multiSelect->getValue());

$multiSelect->selectOption('three', true);

$this->assertEquals(array('1', '3'), $multiSelect->getValue());

$button = $page->findButton('Register');
$this->assertNotNull($button);
$button->press();

$space = ' ';
$out = <<<OUT
'agreement' = 'off',
'select_multiple_numbers' =$space
array (
0 = '1',
1 = '3',
),
'select_number' = '30',
OUT;
$this->assertContains($out, $page->getContent());
}

/**
* @dataProvider testElementSelectedStateCheckDataProvider
*/
public function testElementSelectedStateCheck($selectName, $optionValue, $optionText)
{
$session = $this->getSession();
$session->visit($this->pathTo('/multiselect_form.html'));
$select = $session->getPage()->findField($selectName);

$optionValueEscaped = $session->getSelectorsHandler()->xpathLiteral($optionValue);
$option = $select->find('named', array('option', $optionValueEscaped));
$this->assertNotNull($option);

$this->assertFalse($option->isSelected());
$select->selectOption($optionText);
$this->assertTrue($option->isSelected());
}

public function testElementSelectedStateCheckDataProvider()
{
return array(
array('select_number', '30', 'thirty'),
array('select_multiple_numbers[]', '2', 'two'),
);
}

public function testAdvancedForm()
{
$this->getSession()->visit($this->pathTo('/advanced_form.html'));
Expand Down
141 changes: 141 additions & 0 deletions driver-testsuite/tests/Form/SelectTest.php
@@ -0,0 +1,141 @@
<?php

namespace Behat\Mink\Tests\Driver\Form;

use Behat\Mink\Tests\Driver\TestCase;

class SelectTest extends TestCase
{
public function testMultiselect()
{
$this->getSession()->visit($this->pathTo('/multiselect_form.html'));
$page = $this->getSession()->getPage();
$this->assertEquals('Multiselect Test', $page->find('css', 'h1')->getText());

$select = $page->findField('select_number');
$multiSelect = $page->findField('select_multiple_numbers[]');
$secondMultiSelect = $page->findField('select_multiple_values[]');

$this->assertNotNull($select);
$this->assertNotNull($multiSelect);
$this->assertNotNull($secondMultiSelect);

$this->assertEquals('20', $select->getValue());
$this->assertSame(array(), $multiSelect->getValue());
$this->assertSame(array('2', '3'), $secondMultiSelect->getValue());

$select->selectOption('thirty');
$this->assertEquals('30', $select->getValue());

$multiSelect->selectOption('one', true);

$this->assertSame(array('1'), $multiSelect->getValue());

$multiSelect->selectOption('three', true);

$this->assertEquals(array('1', '3'), $multiSelect->getValue());

$secondMultiSelect->selectOption('two');
$this->assertSame(array('2'), $secondMultiSelect->getValue());

$button = $page->findButton('Register');
$this->assertNotNull($button);
$button->press();

$space = ' ';
$out = <<<OUT
'agreement' = 'off',
'select_multiple_numbers' =$space
array (
0 = '1',
1 = '3',
),
'select_multiple_values' =$space
array (
0 = '2',
),
'select_number' = '30',
OUT;
$this->assertContains($out, $page->getContent());
}

/**
* @dataProvider testElementSelectedStateCheckDataProvider
*/
public function testElementSelectedStateCheck($selectName, $optionValue, $optionText)
{
$session = $this->getSession();
$session->visit($this->pathTo('/multiselect_form.html'));
$select = $session->getPage()->findField($selectName);
$this->assertNotNull($select);

$optionValueEscaped = $session->getSelectorsHandler()->xpathLiteral($optionValue);
$option = $select->find('named', array('option', $optionValueEscaped));
$this->assertNotNull($option);

$this->assertFalse($option->isSelected());
$select->selectOption($optionText);
$this->assertTrue($option->isSelected());
}

public function testElementSelectedStateCheckDataProvider()
{
return array(
array('select_number', '30', 'thirty'),
array('select_multiple_numbers[]', '2', 'two'),
);
}

public function testSetValueSingleSelect()
{
$session = $this->getSession();
$session->visit($this->pathTo('/multiselect_form.html'));
$select = $session->getPage()->findField('select_number');
$this->assertNotNull($select);

$select->setValue('10');
$this->assertEquals('10', $select->getValue());
}

public function testSetValueMultiSelect()
{
$session = $this->getSession();
$session->visit($this->pathTo('/multiselect_form.html'));
$select = $session->getPage()->findField('select_multiple_values[]');
$this->assertNotNull($select);

$select->setValue(array('1', '2'));
$this->assertEquals(array('1', '2'), $select->getValue());
}

/**
* @see https://github.com/Behat/Mink/issues/193
*/
public function testOptionWithoutValue()
{
$session = $this->getSession();
$session->visit($this->pathTo('/issue193.html'));

$session->getPage()->selectFieldOption('options-without-values', 'Two');
$this->assertEquals('Two', $session->getPage()->findById('options-without-values')->getValue());

$this->assertTrue($session->getPage()->findById('two')->isSelected());
$this->assertFalse($session->getPage()->findById('one')->isSelected());

$session->getPage()->selectFieldOption('options-with-values', 'two');
$this->assertEquals('two', $session->getPage()->findById('options-with-values')->getValue());
}

/**
* @see https://github.com/Behat/Mink/issues/131
*/
public function testAccentuatedOption()
{
$this->getSession()->visit($this->pathTo('/issue131.html'));
$page = $this->getSession()->getPage();

$page->selectFieldOption('foobar', 'Gimme some accentués characters');

$this->assertEquals('1', $page->findField('foobar')->getValue());
}
}
14 changes: 0 additions & 14 deletions driver-testsuite/tests/Js/JavascriptTest.php
Expand Up @@ -32,20 +32,6 @@ public function testDragDrop()
$this->assertEquals('Dropped!', $droppable->find('css', 'p')->getText());
}

// TODO reevaluate this test as it is passing only for Selenium2Driver
// test select option without value
public function testIssue193()
{
$session = $this->getSession();
$session->visit($this->pathTo('/issue193.html'));

$session->getPage()->selectFieldOption('options-without-values', 'Two');
$this->assertEquals('Two', $session->getPage()->findById('options-without-values')->getValue());

$session->getPage()->selectFieldOption('options-with-values', 'two');
$this->assertEquals('two', $session->getPage()->findById('options-with-values')->getValue());
}

// test accentuated char in button
public function testIssue225()
{
Expand Down
8 changes: 4 additions & 4 deletions driver-testsuite/web-fixtures/issue193.html
Expand Up @@ -7,14 +7,14 @@
<body>

<form action="#">
<select id="options-without-values">
<select id="options-without-values" name="without">
<option>none selected</option>
<option>One</option>
<option>Two</option>
<option id="one">One</option>
<option id="two">Two</option>
<option>Three</option>
</select>

<select id="options-with-values">
<select id="options-with-values" name="with">
<option value="none">none selected</option>
<option value="one">One</option>
<option value="two">Two</option>
Expand Down
6 changes: 6 additions & 0 deletions driver-testsuite/web-fixtures/multiselect_form.html
Expand Up @@ -20,6 +20,12 @@ <h1>Multiselect Test</h1>
<option value="3">three</option>
</select>

<select name="select_multiple_values[]" multiple="multiple">
<option value="1">one</option>
<option value="2" selected="selected">two</option>
<option value="3" selected="selected">three</option>
</select>

<input type="submit" name="submit" value="Register" />
</form>
</body>
Expand Down
8 changes: 5 additions & 3 deletions src/Behat/Mink/Element/NodeElement.php
Expand Up @@ -68,7 +68,7 @@ public function getTagName()
}

/**
* Returns the value of the form field.
* Returns the value of the form field or option element.
*
* For checkbox fields, the value is a boolean indicating whether the checkbox is checked.
* For radio buttons, the value is the value of the selected button in the radio group
Expand All @@ -77,10 +77,12 @@ public function getTagName()
* For multiple select boxes, the value is an array of selected option values.
* for file inputs, the return value is undefined given that browsers don't allow accessing
* the value of file inputs for security reasons. Some drivers may allow accessing the
* path of the file set in the field, but this is not required if it cannot be implemened.
* path of the file set in the field, but this is not required if it cannot be implemented.
* For textarea elements and all textual fields, the value is the content of the field.
* Form option elements, the value is the value of the option (the value attribute or the text
* content if the attribute is not set).
*
* Calling this method on other elements than form fields is not allowed.
* Calling this method on other elements than form fields or option elements is not allowed.
*
* @return mixed
*/
Expand Down

0 comments on commit 1afdb9c

Please sign in to comment.