zoukankan      html  css  js  c++  java
  • WebDriver中的Actions对象

    我们可以利用Actions对象来模拟鼠标的操作以及页面的拖拽

    1、模拟鼠标的双击操作:

      1)模拟双击一个div,验证点击之前的字体为14号
      2)点击后字体为20号
      Actions builder = new Actions(driver); //new 出Actions对象
      builder.doubleClick(message).build().perform(); //对象后可以跟一系列的操作,要通过跟着build().perform()才能执行
    package com.example.tests;
    import static org.junit.Assert.*;
    import org.junit.*;
    import org.openqa.selenium.*;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.interactions.Actions;
    public class Selenium2 {
    WebDriver driver = new FirefoxDriver();
    @Test
    public void tableTest() {
    driver.get("D:\demo\DoubleClickDemo.html");
    WebElement message = driver.findElement(By.id("message"));
    //  验证初始字体为14px
    assertEquals("14px", message.getCssValue("font-size"));
    Actions builder = new Actions(driver);
    builder.doubleClick(message).build().perform();
    //  验证点击后字体变为20px
    assertEquals("20px", message.getCssValue("font-size"));
    driver.close();
    }
    }

    2、模拟拖拽:

    dragAndDrop(source, target)方法
    package com.example.tests;
    import static org.junit.Assert.*;
    import org.junit.*;
    import org.openqa.selenium.*;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    import org.openqa.selenium.interactions.Actions;
    public class Selenium2 {
    @Test
    public void testDragDrop() {
    WebDriver driver = new InternetExplorerDriver();
    driver.get("D:\demo\DragAndDrop.html");
    WebElement source = driver.findElement(By.id("draggable"));
    WebElement target = driver.findElement(By.id("droppable"));
    Actions builder = new Actions(driver);
    builder.dragAndDrop(source, target).perform();
    try {
    assertEquals("Dropped!", target.getText());
    } catch (Error e) {
    e.printStackTrace();
    }finally{
    driver.close();
    }
    }
    }
  • 相关阅读:
    65 组件 静态文件相关 视图
    作者和书籍的增删改查 多对多
    64 装饰器函数: 母版 csrf防御机制 cookie
    61 书籍和出版社 的增删改查 几秒后跳转一个页面
    60 Django项目 单表(出版社)的增删改查 __str__方法及格式化输出 的两个方法
    模块 itertools
    59 Django基础三件套 , 模板{{}}语言 , 程序连mysql Django项目app Django中ORM的使用
    nginx 并发数
    设置tomcat最大内存
    goaccess安装
  • 原文地址:https://www.cnblogs.com/sylovezp/p/4329820.html
Copyright © 2011-2022 走看看