zoukankan      html  css  js  c++  java
  • 章节十四、10-按键事件

    一、如何按键盘上的按键:

     1 package keypress;
     2 
     3 import static org.junit.jupiter.api.Assertions.*;
     4 
     5 import java.util.concurrent.TimeUnit;
     6 
     7 import org.junit.jupiter.api.AfterEach;
     8 import org.junit.jupiter.api.BeforeEach;
     9 import org.junit.jupiter.api.Test;
    10 import org.openqa.selenium.By;
    11 import org.openqa.selenium.Keys;
    12 import org.openqa.selenium.WebDriver;
    13 import org.openqa.selenium.chrome.ChromeDriver;
    14 
    15 class KeyPressDemo1 {
    16 
    17     WebDriver driver;
    18     String url;
    19     
    20     @BeforeEach
    21     void setUp() throws Exception {
    22         driver = new ChromeDriver();
    23         url = "https://login.yahoo.com/config/login?.src=fpctx&.intl=us&.lang=en-US&.done=https%3A%2F%2Fwww.yahoo.com";
    24         driver.manage().window().maximize();
    25         driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
    26         driver.get(url);
    27     }
    28 
    29     @Test
    30     void test() throws InterruptedException {
    31         driver.findElement(By.id("login-username")).sendKeys("test");
    32         Thread.sleep(2000);
    33 //        按键盘上的enter按钮,发送回车键
    34 //        Keys.按键:可以根据自己的需要进行相应的按键设置
    35         driver.findElement(By.id("login-signin")).sendKeys(Keys.ENTER);
    36     }
    37     
    38     @AfterEach
    39     void tearDown() throws Exception {
    40         Thread.sleep(2000);
    41         driver.quit();
    42     }
    43 }

    二、如何按键盘上的组合键:

     1 package keypress;
     2 
     3 import static org.junit.jupiter.api.Assertions.*;
     4 
     5 import java.util.concurrent.TimeUnit;
     6 
     7 import org.junit.jupiter.api.AfterEach;
     8 import org.junit.jupiter.api.BeforeEach;
     9 import org.junit.jupiter.api.Test;
    10 import org.openqa.selenium.By;
    11 import org.openqa.selenium.Keys;
    12 import org.openqa.selenium.WebDriver;
    13 import org.openqa.selenium.chrome.ChromeDriver;
    14 
    15 class KeyPressDemo2 {
    16     
    17     WebDriver driver;
    18     String url;
    19     
    20     @BeforeEach
    21     void setUp() throws Exception {
    22         driver = new ChromeDriver();
    23         url = "file:///C:/Users/acer/Desktop/%E5%85%B6%E5%AE%83/PracticePage2.html";
    24         driver.manage().window().maximize();
    25         driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
    26         driver.get(url);
    27     }
    28 
    29     @Test
    30     void test() throws InterruptedException {
    31 //        方法一:
    32 //        driver.findElement(By.id("openwindow")).sendKeys(Keys.CONTROL+"a");
    33         
    34 //        方法二:
    35 //        driver.findElement(By.id("openwindow")).sendKeys(Keys.chord(Keys.CONTROL,"a"));
    36 //        chord方法返回值是String类型的,为了便于理解,因此我们可以这样写:
    37         String seclect = Keys.chord(Keys.CONTROL,"a");
    38         driver.findElement(By.id("openwindow")).sendKeys(seclect);
    39         Thread.sleep(2000);
    40 
    41     }
    42     
    43     @AfterEach
    44     void tearDown() throws Exception {
    45         Thread.sleep(2000);
    46         driver.quit();
    47     }
    48 }

    三、用Actions类直接处理按键事件

     1 package keypress;
     2 
     3 import java.util.concurrent.TimeUnit;
     4 
     5 import org.junit.jupiter.api.AfterEach;
     6 import org.junit.jupiter.api.BeforeEach;
     7 import org.junit.jupiter.api.Test;
     8 import org.openqa.selenium.Keys;
     9 import org.openqa.selenium.WebDriver;
    10 import org.openqa.selenium.chrome.ChromeDriver;
    11 import org.openqa.selenium.interactions.Actions;
    12 
    13 class KeyPressDemo3 {
    14 
    15     WebDriver driver;
    16     String url;
    17     
    18     @BeforeEach
    19     void setUp() throws Exception {
    20         driver = new ChromeDriver();
    21         url = "file:///C:/Users/acer/Desktop/%E5%85%B6%E5%AE%83/PracticePage2.html";
    22         driver.manage().window().maximize();
    23         driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
    24         driver.get(url);
    25     }
    26 
    27     @Test
    28     void test() {
    29         Actions actions = new Actions(driver);
    30         actions.keyDown(Keys.CONTROL).sendKeys("a").keyDown(Keys.CONTROL).perform();
    31     }
    32     
    33     @AfterEach
    34     void tearDown() throws Exception {
    35         Thread.sleep(2000);
    36         driver.quit();
    37     }
    38 }

    注:

    1、actions操作按键时,有按下动作,也需要有松开动作。  

    keyDown():表示按下按键动作

    keyDown(Keys.CONTROL).sendKeys("a"):表示同时按下Ctrl+A

    keyDown():表示松开按键动作

    keyDown(Keys.CONTROL):表示松开Ctrl

    2、actions方法都需要使用.perform()执行,如果.perform()不能成功执行actions,那么还需要加上.bulid.perform()。

    3、使用actions可以直接操作按键,不需要先查找元素。

    如果有不明白的小伙伴可以加群“555191854”问我,群里都是软件行业的小伙伴相互一起学习。

    内容具有连惯性,未标注的地方可以看前面的博客,这是一整套关于ava+selenium自动化的内容,从java基础开始。

    欢迎关注,转载请注明来源。

  • 相关阅读:
    读取数据变JSON传值!
    YII2.0多条件查询升级版
    JS跳转页面方法
    yii的简单片段缓存
    我读过的最好的epoll讲解--转自”知乎“
    I/O多路复用详解
    ”open-close"prinple (OCP)
    获取本机的IPv4或者v6地址
    .Net 下未捕获异常的处理
    TCP断开连接的过程
  • 原文地址:https://www.cnblogs.com/luohuasheng/p/11115935.html
Copyright © 2011-2022 走看看