qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Selenium2.0功能測試之你所不知道的sendKeys

           其實光看字面上的理解sendKeys這個api感覺只有輸入字符這一個功能,其實這樣理解是錯誤的其實這個api的真正作用是模擬鍵盤的操作(包含了輸入字符),不過輸入字符是其主要的功能,下面介紹一下其他的用法:
            這里就直接上代碼了
          package org.coderinfo.demo;
          import org.openqa.selenium.By;
          import org.openqa.selenium.Keys;
          import org.openqa.selenium.WebDriver;
          import org.openqa.selenium.chrome.ChromeDriver;
          public class ApiSendKeys {
          private static final String URL = "file:///C:/user/Desktop/Selenium/operate.html";
          public static void main(String[] args) {
          WebDriver driver = new ChromeDriver(); // create a chrome driver
          driver.manage().window().maximize(); // max size the chrome window
          driver.get(URL); // open URL with the chrome browser
          try {
          Thread.sleep(2000);  // wait for page loading
          } catch (InterruptedException e) {
          e.printStackTrace();
          }
          driver.findElement(By.id("UserName")).sendKeys("coderinfo");  // Get input element and input some words
          try {
          Thread.sleep(3000);
          } catch (InterruptedException e) {
          e.printStackTrace();
          }
          driver.findElement(By.id("UserName")).sendKeys(Keys.chord(Keys.CONTROL + "a")); // Use sendKeys to select all the content
          try {
          Thread.sleep(3000);
          } catch (InterruptedException e) {
          e.printStackTrace();
          }
          driver.findElement(By.id("UserName")).sendKeys(Keys.chord(Keys.CONTROL + "x")); // Use sendKeys to cut the content
          try {
          Thread.sleep(3000);
          } catch (InterruptedException e) {
          e.printStackTrace();
          }
          driver.findElement(By.id("UserEmail")).sendKeys(Keys.chord(Keys.CONTROL + "v"));   // Use sendKeys to copy the content
          try {
          Thread.sleep(5000);
          } catch (InterruptedException e) {
          e.printStackTrace();
          }
          driver.quit();  // close webdriver
          }
          }



            相信如何模擬其他的快捷鍵就不用我一一列舉了,我也沒有試過太多,因為我就用過一次。
            這里是測試的頁面operate.html的源碼:
          <!DOCTYPE html>
          <html>
          <head>
          <title>Operate Element</title>
          </head>
          <body>
          <h3>Operate Element</h3>
          <form class="form-h">
          <input type="text" class="in" id="UserName" /><br />
          <input type="text" class="in" id="UserEmail" /><br />
          <input type="submit" class="in" />
          <input type="reset" class="in" />
          </form>
          </body>
          </html>
          相關文章:
          Selenium2.0功能測試之Alert/Confirm/Prompt的處理

          posted on 2013-10-21 10:36 順其自然EVO 閱讀(2202) 評論(0)  編輯  收藏 所屬分類: selenium and watir webdrivers 自動化測試學習

          <2013年10月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 双柏县| 黑水县| 宁蒗| 兴安盟| 祁连县| 肥东县| 两当县| 玉环县| 甘孜县| 刚察县| 涿鹿县| 安国市| 南汇区| 普洱| 日喀则市| 华宁县| 栖霞市| 瑞安市| 墨江| 监利县| 金寨县| 凌源市| 临夏县| 汨罗市| 原平市| 隆昌县| 文成县| 望城县| 鸡东县| 璧山县| 鸡泽县| 泰宁县| 信宜市| 霍林郭勒市| 会同县| 旬邑县| 嘉禾县| 岳阳市| 钟山县| 淅川县| 惠州市|