qileilove

          blog已經(jīng)轉(zhuǎn)移至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>
          相關(guān)文章:
          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

          導航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 霸州市| 疏附县| 察雅县| 新建县| 清镇市| 乐东| 屏南县| 龙江县| 元朗区| 德格县| 平谷区| 巍山| 塔城市| 湘潭县| 错那县| 巴楚县| 云南省| 泾源县| 珠海市| 九龙城区| 自治县| 平昌县| 左贡县| 阿坝县| 南宁市| 峨眉山市| 南昌县| 泊头市| 临潭县| 常熟市| 彰化县| 义乌市| 科技| 原平市| 汉川市| 那曲县| 通州区| 朝阳市| 满城县| 日照市| 舞阳县|