Skip to content

added type method #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Codeception/Module/WebDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <?php
* // activate input element
* $I->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);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Codeception/Module/TestsForWeb.php
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ public function testLinkWithDocRelativeURLFromDefaultPage()

/*
* @env chrome
* https://github.com/Codeception/Codeception/issues/1507
* @env headless
*/
public function testSubmitFormWithDefaultRadioAndCheckboxValues()
{
Expand Down
20 changes: 20 additions & 0 deletions tests/web/WebDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down