Skip to content

Commit 093747c

Browse files
authored
added type method (#76)
* added type method * fix test * fixed tests for type * fixed a test
1 parent 797b918 commit 093747c

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/Codeception/Module/WebDriver.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,6 +1826,37 @@ public function clearField($field)
18261826
$el->clear();
18271827
}
18281828

1829+
/**
1830+
* Type in characters on active element.
1831+
* With a second parameter you can specify delay between key presses.
1832+
*
1833+
* ```php
1834+
* <?php
1835+
* // activate input element
1836+
* $I->click('#input');
1837+
*
1838+
* // type text in active element
1839+
* $I->type('Hello world');
1840+
*
1841+
* // type text with a 1sec delay between chars
1842+
* $I->type('Hello World', 1);
1843+
* ```
1844+
*
1845+
* This might be useful when you an input reacts to typing and you need to slow it down to emulate human behavior.
1846+
* For instance, this is how Credit Card fields can be filled in.
1847+
*
1848+
* @param $text
1849+
* @param $delay [sec]
1850+
*/
1851+
public function type($text, $delay = 0) {
1852+
$keys = str_split($text);
1853+
foreach ($keys as $key) {
1854+
sleep($delay);
1855+
$this->webDriver->getKeyboard()->pressKey($key);
1856+
}
1857+
sleep($delay);
1858+
}
1859+
18291860
public function attachFile($field, $filename)
18301861
{
18311862
$el = $this->findField($field);

tests/unit/Codeception/Module/TestsForWeb.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@ public function testLinkWithDocRelativeURLFromDefaultPage()
12891289

12901290
/*
12911291
* @env chrome
1292-
* https://github.com/Codeception/Codeception/issues/1507
1292+
* @env headless
12931293
*/
12941294
public function testSubmitFormWithDefaultRadioAndCheckboxValues()
12951295
{

tests/web/WebDriverTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,16 @@ public function testAppendFieldTextarea()
365365
$this->assertEquals('eat code', $form['description']);
366366
}
367367

368+
public function testTypeOnTextarea()
369+
{
370+
$this->module->amOnPage('/form/textarea');
371+
$this->module->fillField('form #description', '');
372+
$this->module->type('Hello world');
373+
$this->module->click('Submit');
374+
$form = data::get('form');
375+
$this->assertEquals('Hello world', $form['description']);
376+
}
377+
368378
public function testAppendFieldTextareaFails()
369379
{
370380
$this->shouldFail();
@@ -381,6 +391,16 @@ public function testAppendFieldText()
381391
$this->assertEquals('OLD_VALUE code', $form['name']);
382392
}
383393

394+
public function testTypeOnTextField()
395+
{
396+
$this->module->amOnPage('/form/field');
397+
$this->module->fillField('form #name', '');
398+
$this->module->type('Hello world');
399+
$this->module->click('Submit');
400+
$form = data::get('form');
401+
$this->assertEquals('Hello world', $form['name']);
402+
}
403+
384404
public function testAppendFieldTextFails()
385405
{
386406
$this->shouldFail();

0 commit comments

Comments
 (0)