官术网_书友最值得收藏!

The sendKeys() method

ThesendKeys  action is applicable for textbox or textarea HTML elements. This is used to type text into the textbox. This will simulate the user keyboard and types text into WebElements exactly as a user would. The API syntax for the sendKeys() method is as follows:

void sendKeys(java.lang.CharSequence...keysToSend)

The input parameter for the preceding method is CharSequence of text that has to be entered into the element. This method doesn't return anything. Now, let's see a code example of how to type a search text into the Search box using the sendKeys() method:

@Test
public void elementSendKeysExample() {
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("Phones");
searchBox.submit();
assertThat(driver.getTitle())
.isEqualTo("Search results for: 'Phones'");
}

In the preceding code, the sendKeys() method is used to type the required text in the textbox element of the web page. This is how we deal with normal keys, but if you want to type in some special keys, such as Backspace, Enter, Tab, or Shift, we need to use a special enum class of WebDriver, named Keys. Using the Keys enumeration, you can simulate many special keys while typing into a WebElement.

Now let's see some code example, which uses the Shift key to type the text in uppercase in the Search Box:

@Test
public void elementSendKeysCompositeExample() {
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys(Keys.chord(Keys.SHIFT,"phones"));
searchBox.submit();
assertThat(driver.getTitle())
.isEqualTo("Search results for: 'PHONES'");
}

In the preceding code, the chord() method from the Keys enum is used to type the key, while the text specified is being given as an input to be the textbox. Try this in your environment to see all the text being typed in uppercase.

主站蜘蛛池模板: 遵义县| 鹿邑县| 福海县| 澎湖县| 南投县| 塘沽区| 夏津县| 湘潭县| 靖安县| 安化县| 卢湾区| 黎城县| 湟源县| 尼木县| 仙游县| 页游| 井研县| 湾仔区| 铁岭市| 伊宁市| 台州市| 漯河市| 旬邑县| 朝阳区| 彭泽县| 永康市| 鄄城县| 炎陵县| 金寨县| 延津县| 信丰县| 原阳县| 灵丘县| 泰安市| 枞阳县| 自贡市| 靖西县| 全椒县| 双柏县| 安新县| 华坪县|