11.1使用JavaScripExecutor单击元素

被测试网页:http://www.sogou.com
Java代码

public class NewTest {
     WebDriver driver;
     String baseUrl;
     JavascriptExecutor js;
  @Test
  public void f() throws Exception {
      
     WebElement searchInputBox=driver.findElement(By.id("query"));
     WebElement searchButton=driver.findElement(By.id("stb"));
     searchInputBox.sendKeys("使用javascipt语句进行页面元素的单击");
     //调用等装好的JavaScipt方法来单击sogou首页的搜索按钮
     JavaScriptClick(searchButton);  
  }
  public void JavaScriptClick(WebElement element) throws Exception{
      try{
          //if条件判断函数参数传入的element元素是否处于可单击状态,以及是否显示在页面上
          if(element.isEnabled()&&element.isDisplayed()){
              System.out.println("使用JavaScipt进行页面元素的单击");
              //执行JavaScipt语句argument[0].click()
              ((JavascriptExecutor) driver).executeScript("arguments[0].click();",element);
          }else{
              System.out.println("页面上的元素无法进行单击操作");
          }
          //当出现异常的时候,catch语句会被执行,打印相关的异常信息和堆栈信息
      }catch(StaleElementReferenceException e){
          System.out.println("页面元素没有附加在网页中"+e.getStackTrace());
      }catch(NoSuchElementException e){
          System.out.println("页面中没有找到要操作的页面元素"+e.getStackTrace());
      }catch(Exception e){
          System.out.println("无法完成单击动作"+e.getStackTrace());
      }

}

代码解释:方法里面的代码实现就是一种封装,把常用的操作卸载一个函数里面,就可以很方便的重复调用,减少冗余代码的编写,提高测试代码的编写效率。
————————————————————————————————————————————————————
11.2在Ajax方式产生的浮动框中,单击选择包含某个关键字的选项
目的:有些被测试页面包含Ajax的局部刷新机制,并且会产生显示多条数据的浮动框,需要单击选择浮动框中包含某个关键的选项
 
被测试网页:http://www.sogou.com
Java代码
  @Test
  public void f() throws Exception {
      
     WebElement searchInputBox=driver.findElement(By.id("query"));
     //WebElement searchButton=driver.findElement(By.id("stb"));
     searchInputBox.click();
     //将浮动框中的所有选项存储到suggetionOptions的List容器中,直接复制的xpath是://*[@id="vl"]/div[1]/ul/li[3],这里的双引号需要转义一下
     //List需要进入包,刚开始引入的import java.awt.List;报错,引入import java.util.List;成功
     List <WebElement>suggetionOptions=driver.findElements(By.xpath("//*[@id=\"vl\"]/div[1]/ul/li[3]"));
     for(WebElement element:suggetionOptions){
         if(element.getText().contains("")){
             System.out.println(element.getText());
             element.click();
             break;
         }
     }

}

————————————————————————————————————————————————————————
11.3设置一个页面对象的属性值
目的:掌握设定页面对象的所有属性的方法,本节以设定文本框的可编辑状态和显示长度为目标。
被测试网页:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>设置文本框属性</title>
</head>
<body>
<input type="text" id="text" value="今年的西瓜相当甜" size="100">文本框
</body>
</html>
java代码
public class NewTest {
     WebDriver driver;
     String baseUrl; 
     JavascriptExecutor js;
  @Test
  public void f() throws Exception {
      
     WebElement textInputBox=driver.findElement(By.id("text"));
     //调用setAttribute方法修改文本框的value属性值,改变文本框中显示的文字
    setAttribute(driver,textInputBox,"value","文本框的文字和长度属性已经被修改了");
    //调用setAttribute方法修改文本框的size属性值,改变文本框中的长度
    setAttribute(driver,textInputBox,"size","10");
    //调用removeAttribute方法删除文本框中的size属性值
    //removeAttribute(driver,textInputBox,"size");
    //增加页面元素属性属性和修改页面元素属性的封装方法
  }
 
private void setAttribute(WebDriver driver2, WebElement textInputBox, String string, String string2) {
    // TODO Auto-generated method stub
     JavascriptExecutor js=(JavascriptExecutor) driver;
      Object element = null;
    Object attributeName = "aa";
    Object value = null;
    js.executeScript("arguments[0].setAttribute(arguments[1],arguments[2])",element,attributeName,value);
}
/*private void removeAttribute(WebDriver driver2, WebElement textInputBox, String string) {
    // TODO Auto-generated method stub
      JavascriptExecutor js=(JavascriptExecutor) driver;
      Object element = null;
    Object attributeName = null;
    Object value = null;
    js.executeScript("arguments[0].removeAttribute(arguments[1],arguments[2])",element,attributeName,value);
    
}*/
 
@BeforeMethod
  public void beforeMethod() {
      baseUrl="C:\\Users\\Administrator\\WebstormProjects\\untitled\\11.3.html";
    System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
     driver=new ChromeDriver(); 
     driver.get(baseUrl);

}

selenium自动化测试实例的更多相关文章

  1. Selenium自动化测试实例-基于python

    一.Selenium介绍 Selenium是一个Web开源自动化测试框架,具有页面级操作.模拟用户真实操作.API从系统层面触发事件等特点. 1.版本 Selenium 1.0  Sever/Clie ...

  2. Selenium自动化测试,接口自动化测试开发,性能测试从入门到精通

    Selenium自动化测试,接口自动化测试开发,性能测试从入门到精通Selenium接口性能自动化测试基础部分:分层自动化思想Slenium介绍Selenium1.0/2.0/3.0Slenium R ...

  3. Selenium自动化测试Python五:WebDriver设计模式

    WebDriver 设计模式 欢迎阅读WebDriver进阶讲义.本篇讲义将会重点介绍Selenium WebDriver 自动化框架的设计,着重使用Page Object设计模式,以及使用HTML测 ...

  4. Selenium自动化测试Python二:WebDriver基础

    WebDriver基础 欢迎阅读WebDriver基础讲义.本篇讲义将会重点介绍Selenium WebDriver的环境搭建和基本使用方法. WebDriver环境搭建 Selenium WebDr ...

  5. Java+selenium自动化测试基础

    Java+selenium maven配置 maven的配置,但还需要建立maven的本地库,修改apach-maven的setting.xml http://www.cnblogs.com/haoa ...

  6. selenium自动化测试框架之PO设计模式

    面向对象的特性:封装.继承.多态.在自动化中一样适用,Selenium自动化测试中有一个名字常常被提及PageObject(思想与面向对象的特性相同),通过PO模式可以大大提高测试用例的维护效率. 传 ...

  7. 记一个Selenium自动化测试网页

    今天想跟大家分享的是Selenium自动化测试网页,就是关于selenium的自动化测试一些基础的东西,如有不对的地方请多多指教. 一.安装环境 1.Python环境 安装完成后通过Windows命令 ...

  8. 《手把手教你》系列技巧篇(十五)-java+ selenium自动化测试-元素定位大法之By xpath中卷(详细教程)

    1.简介 按宏哥计划,本文继续介绍WebDriver关于元素定位大法,这篇介绍定位倒数二个方法:By xpath.xpath 的定位方法, 非常强大.  使用这种方法几乎可以定位到页面上的任意元素. ...

  9. 《手把手教你》系列技巧篇(十六)-java+ selenium自动化测试-元素定位大法之By xpath下卷(详细教程)

    1.简介 按宏哥计划,本文继续介绍WebDriver关于元素定位大法,这篇介绍定位倒数二个方法:By xpath.xpath 的定位方法, 非常强大.  使用这种方法几乎可以定位到页面上的任意元素. ...

随机推荐

  1. 让Spring Boot启动更快

    关注公众号:锅外的大佬, 每日推送国外技术好文,帮助每位开发者更好成长 原文链接:https://dev.to/bufferings/lets-make-springboot-app-start-fa ...

  2. 使用mysql导入数据时关掉binlog

    在my.cnf中注释掉 log-bin=bin-log参数然后重启数据库

  3. Array types are now written with the brackets around the element type问题的解决方法

    在xcode6.1中来编写swift空数组时.出现的的这个问题,依照官方 Swift 教程<The Swift Programming Language>来写 let emptyArray ...

  4. String池与iterator对集合的迭代

    一.静态导入 1.       导入类的静态属性 import static java.lang.System.out; out.println("haha"); 2.       ...

  5. Docker and Go: why did we decide to write Docker in Go?

    Docker and Go: why did we decide to write Docker in Go? | Hacker News https://news.ycombinator.com/i ...

  6. kill 挂起 Apache Web Server

    [root@hadoop1 ~]# kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8 ...

  7. Axure Base 09 带遮罩层的弹出框

    示例原型下载:小楼Axure原创元件-带遮罩层的弹出框 实现目标: 1.   点击按钮弹出带遮罩层的对话框: 2.   页面上下左右滚动时,弹出的对话框水平和垂直始终居中. 实现步骤如下: 1. 拖入 ...

  8. POJ3984 迷宫问题 —— BFS

    题目链接:http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  9. HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Other ...

  10. codeforces B. Balls Game 解题报告

    题目链接:http://codeforces.com/problemset/problem/430/B 题目意思:给出用不多于k种颜色对n个球的染色情况,以及手中的唯一一个球的颜色.初始时,连续的相同 ...