diff --git a/src/Codeception/Module/WebDriver.php b/src/Codeception/Module/WebDriver.php index 70c8a4b..533a2bd 100644 --- a/src/Codeception/Module/WebDriver.php +++ b/src/Codeception/Module/WebDriver.php @@ -1826,6 +1826,37 @@ public function clearField($field) $el->clear(); } + /** + * Type in characters on active element. + * With a second parameter you can specify delay between key presses. + * + * ```php + * click('#input'); + * + * // type text in active element + * $I->type('Hello world'); + * + * // type text with a 1sec delay between chars + * $I->type('Hello World', 1); + * ``` + * + * This might be useful when you an input reacts to typing and you need to slow it down to emulate human behavior. + * For instance, this is how Credit Card fields can be filled in. + * + * @param $text + * @param $delay [sec] + */ + public function type($text, $delay = 0) { + $keys = str_split($text); + foreach ($keys as $key) { + sleep($delay); + $this->webDriver->getKeyboard()->pressKey($key); + } + sleep($delay); + } + public function attachFile($field, $filename) { $el = $this->findField($field); diff --git a/tests/unit/Codeception/Module/TestsForWeb.php b/tests/unit/Codeception/Module/TestsForWeb.php index ad238de..a4ca673 100644 --- a/tests/unit/Codeception/Module/TestsForWeb.php +++ b/tests/unit/Codeception/Module/TestsForWeb.php @@ -1289,7 +1289,7 @@ public function testLinkWithDocRelativeURLFromDefaultPage() /* * @env chrome - * https://github.com/Codeception/Codeception/issues/1507 + * @env headless */ public function testSubmitFormWithDefaultRadioAndCheckboxValues() { diff --git a/tests/web/WebDriverTest.php b/tests/web/WebDriverTest.php index c141be1..93a00a1 100644 --- a/tests/web/WebDriverTest.php +++ b/tests/web/WebDriverTest.php @@ -365,6 +365,16 @@ public function testAppendFieldTextarea() $this->assertEquals('eat code', $form['description']); } + public function testTypeOnTextarea() + { + $this->module->amOnPage('/form/textarea'); + $this->module->fillField('form #description', ''); + $this->module->type('Hello world'); + $this->module->click('Submit'); + $form = data::get('form'); + $this->assertEquals('Hello world', $form['description']); + } + public function testAppendFieldTextareaFails() { $this->shouldFail(); @@ -381,6 +391,16 @@ public function testAppendFieldText() $this->assertEquals('OLD_VALUE code', $form['name']); } + public function testTypeOnTextField() + { + $this->module->amOnPage('/form/field'); + $this->module->fillField('form #name', ''); + $this->module->type('Hello world'); + $this->module->click('Submit'); + $form = data::get('form'); + $this->assertEquals('Hello world', $form['name']); + } + public function testAppendFieldTextFails() { $this->shouldFail();