Sending keys in Selenium in php

Below example shows how to press various keys in Selenium in PHP.
 
<?php
class MyTest extends PHPUnit_Framework_TestCase {

protected $webDriver;

public function setUp()
{
$capabilities = array(WebDriverCapabilityType::BROWSER_NAME => ‘chrome’);
$this->webDriver = RemoteWebDriver::create(‘https://localhost:4444/wd/hub’, $capabilities);
$this->webDriver->manage()->window()->maximize();
}

public function testKeys()
{
$this->webDriver->get(“https://www.softpost.org/selenium-test-page”);

$this->webDriver->findElement(WebDriverBy::id(‘fn’))->sendKeys(“Ponting”);
sleep(2);

//Sending combination of characters
$this->webDriver->findElement(WebDriverBy::id(‘fn’))->sendKeys(WebDriverKeys::CONTROL . “a”);

sleep(2);

//Similarly you can press any other key as mentioned below
//CANCEL, BACKSPACE, TAB, RETURN_KEY, ENTER, SHIFT, ALT, ESCAPE, SPACE
//PAGE_UP, PAGE_DOWN, END, HOME, ARROW_LEFT, UP, DOWN, ARROW_UP, INSERT, DELETE, NUMPAD0
//F1 to F12

}

public function tearDown()
{
//Quit the driver
$this->webDriver->quit();
}
}
?>

Web development and Automation testing

solutions delivered!!