Skip to content

Commit 64588c6

Browse files
committed
Merge branch 'master' of github.com:symfony/panthere
2 parents 9d3e889 + 0ef05d7 commit 64588c6

File tree

7 files changed

+223
-4
lines changed

7 files changed

+223
-4
lines changed

src/DomCrawler/Field/FormFieldTrait.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Symfony\Component\Panther\DomCrawler\Field;
1515

1616
use Facebook\WebDriver\WebDriverElement;
17+
use Facebook\WebDriver\WebDriverKeys;
1718
use Symfony\Component\Panther\ExceptionThrower;
1819

1920
/**
@@ -48,13 +49,17 @@ public function getValue()
4849
return $this->element->getAttribute('value');
4950
}
5051

51-
public function setValue($value)
52+
public function isDisabled()
5253
{
53-
\is_bool($value) ? $this->element->click() : $this->element->sendKeys($value);
54+
return $this->element->getAttribute('disabled') ?? false;
5455
}
5556

56-
public function isDisabled()
57+
private function setTextValue($value): void
5758
{
58-
return $this->element->getAttribute('disabled') ?? false;
59+
// Ensure to clean field before sending keys.
60+
// Unable to use $this->element->clear(); because it triggers a change event on it's own which is unexpected behavior.
61+
$existingValueLength = \strlen($this->getValue());
62+
$deleteKeys = \str_repeat(WebDriverKeys::BACKSPACE.WebDriverKeys::DELETE, $existingValueLength);
63+
$this->element->sendKeys($deleteKeys.$value);
5964
}
6065
}

src/DomCrawler/Field/InputFormField.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ final class InputFormField extends BaseInputFormField
2222
{
2323
use FormFieldTrait;
2424

25+
public function setValue($value)
26+
{
27+
if (\in_array($this->element->getAttribute('type'), ['text'], true)) {
28+
$this->setTextValue($value);
29+
} elseif (\is_bool($value)) {
30+
$this->element->click();
31+
} else {
32+
$this->element->sendKeys($value);
33+
}
34+
}
35+
2536
/**
2637
* Initializes the form field.
2738
*

src/DomCrawler/Field/TextareaFormField.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ final class TextareaFormField extends BaseTextareaFormField
2222
{
2323
use FormFieldTrait;
2424

25+
public function setValue($value)
26+
{
27+
$this->setTextValue($value);
28+
}
29+
2530
/**
2631
* Initializes the form field.
2732
*
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Panther project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Symfony\Component\Panther\Tests\DomCrawler\Field;
15+
16+
use Symfony\Component\DomCrawler\Field\InputFormField;
17+
use Symfony\Component\Panther\Tests\TestCase;
18+
19+
/**
20+
* @author Robert Freigang <[email protected]>
21+
*/
22+
class InputFormFieldTest extends TestCase
23+
{
24+
/**
25+
* @dataProvider clientFactoryProvider
26+
*/
27+
public function testGetValueWithSomeValueFromTextInput(callable $clientFactory)
28+
{
29+
$crawler = $this->request($clientFactory, '/input-form-field.html');
30+
$form = $crawler->filter('form')->form();
31+
32+
/** @var InputFormField */
33+
$field = $form['text_input_with_some_value'];
34+
$this->assertInstanceOf(InputFormField::class, $field);
35+
$this->assertSame('some_value', $field->getValue());
36+
}
37+
38+
/**
39+
* @dataProvider clientFactoryProvider
40+
*/
41+
public function testGetValueWithNoValueFromTextInput(callable $clientFactory)
42+
{
43+
$crawler = $this->request($clientFactory, '/input-form-field.html');
44+
$form = $crawler->filter('form')->form();
45+
46+
/** @var InputFormField */
47+
$field = $form['text_input_with_no_value'];
48+
$this->assertInstanceOf(InputFormField::class, $field);
49+
$this->assertSame('', $field->getValue());
50+
}
51+
52+
/**
53+
* @dataProvider clientFactoryProvider
54+
*/
55+
public function testSetValueMultipleTimesInTextInput(callable $clientFactory)
56+
{
57+
$crawler = $this->request($clientFactory, '/input-form-field.html');
58+
$form = $crawler->filter('form')->form();
59+
60+
/** @var InputFormField */
61+
$field = $form['text_input_with_no_value'];
62+
$this->assertInstanceOf(InputFormField::class, $field);
63+
64+
$field->setValue('first_value');
65+
$this->assertSame('first_value', $field->getValue());
66+
67+
$field->setValue('second_value');
68+
$this->assertSame('second_value', $field->getValue());
69+
}
70+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Panther project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Symfony\Component\Panther\Tests\DomCrawler\Field;
15+
16+
use Symfony\Component\DomCrawler\Field\TextareaFormField;
17+
use Symfony\Component\Panther\Tests\TestCase;
18+
19+
/**
20+
* @author Robert Freigang <[email protected]>
21+
*/
22+
class TextareaFormFieldTest extends TestCase
23+
{
24+
/**
25+
* @dataProvider clientFactoryProvider
26+
*/
27+
public function testGetValueWithSomeValue(callable $clientFactory)
28+
{
29+
$crawler = $this->request($clientFactory, '/textarea-form-field.html');
30+
$form = $crawler->filter('form')->form();
31+
32+
/** @var TextareaFormField */
33+
$field = $form['textarea_with_some_value'];
34+
$this->assertInstanceOf(TextareaFormField::class, $field);
35+
$this->assertSame('some_value', $field->getValue());
36+
}
37+
38+
/**
39+
* @dataProvider clientFactoryProvider
40+
*/
41+
public function testGetValueWithNoValue(callable $clientFactory)
42+
{
43+
$crawler = $this->request($clientFactory, '/textarea-form-field.html');
44+
$form = $crawler->filter('form')->form();
45+
46+
/** @var TextareaFormField */
47+
$field = $form['textarea_with_no_value'];
48+
$this->assertInstanceOf(TextareaFormField::class, $field);
49+
$this->assertSame('', $field->getValue());
50+
}
51+
52+
/**
53+
* @dataProvider clientFactoryProvider
54+
*/
55+
public function testSetValueMultipleTimes(callable $clientFactory)
56+
{
57+
$crawler = $this->request($clientFactory, '/textarea-form-field.html');
58+
$form = $crawler->filter('form')->form();
59+
60+
/** @var TextareaFormField */
61+
$field = $form['textarea_with_no_value'];
62+
$this->assertInstanceOf(TextareaFormField::class, $field);
63+
64+
$field->setValue('first_value');
65+
$this->assertSame('first_value', $field->getValue());
66+
67+
$field->setValue('second_value');
68+
$this->assertSame('second_value', $field->getValue());
69+
}
70+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Form with ChoiceFormField</title>
6+
<style>
7+
label {
8+
border: 1px solid lightgrey;
9+
display: block;
10+
}
11+
</style>
12+
</head>
13+
<body>
14+
<form>
15+
16+
<label class="field">
17+
text_input_with_some_value
18+
<input name="text_input_with_some_value" type="text" value="some_value" />
19+
</label>
20+
21+
<label class="field">
22+
text_input_with_no_value
23+
<input name="text_input_with_no_value" type="text" />
24+
</label>
25+
26+
<input type="submit" value="OK">
27+
</form>
28+
</body>
29+
</html>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Form with ChoiceFormField</title>
6+
<style>
7+
label {
8+
border: 1px solid lightgrey;
9+
display: block;
10+
}
11+
</style>
12+
</head>
13+
<body>
14+
<form>
15+
16+
<label class="field">
17+
textarea_with_some_value
18+
<textarea name="textarea_with_some_value">some_value</textarea>
19+
</label>
20+
21+
<label class="field">
22+
textarea_with_no_value
23+
<textarea name="textarea_with_no_value"></textarea>
24+
</label>
25+
26+
<input type="submit" value="OK">
27+
</form>
28+
</body>
29+
</html>

0 commit comments

Comments
 (0)