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. openwrt procd

    接着前面写过的一篇关于 procd 的笔记. procd 在 STATE_INIT 时会运行 /etc/inittab 中描述的几个级别指定程序. procd_inittab_run("re ...

  2. MYSQL强制使用索引和禁止使用索引

    mysql强制索引和禁止某个索引 1.mysql强制使用索引:force index(索引名或者主键PRI) 例如: select * from table force index(PRI) limi ...

  3. angularjs学习之八(angularjs中isolate scope的使用)

    angular js中指令directive有个特别实用的东西,那就是 isolate scope (被隔离的scope) 关于详细他和全局的scope 有什么差别.能够參考以下这篇博文: Angul ...

  4. NAND flash坏区

    计算容量 厂家所说的4G指的是4 000 000 000字节,是按1000进制计算的,而电脑是按照1024进制计算的,所以标称为4G的NAND Flash理论容量是4 000 000 000 / 10 ...

  5. npm WARN uninstall not installed in /Users/hrt0kmt/node_modules: "xxx"

    You may meet this error on home directory. % npm uninstall appium npm WARN uninstall not installed i ...

  6. 诺基亚(Microsoft Devices Group)2014暑期实习生笔试题知识点

    总结一下Microsoft Devices Group的软件类笔试题,全部笔试题分两份试卷,逻辑题一份和软件測试题一份,仅仅总结技术题喽~题目全英文,仅仅包括选择题和填空题.选择题居多.分单选和多选. ...

  7. HDU 4085 Peach Blossom Spring 斯坦纳树 状态压缩DP+SPFA

    状态压缩dp+spfa解斯坦纳树 枚举子树的形态 dp[i][j] = min(dp[i][j], dp[i][k]+dp[i][l]) 当中k和l是对j的一个划分 依照边进行松弛 dp[i][j]  ...

  8. Statelessness Provide credentials with the request. Each request MUST stand alone and should not be affected from previous conversation happened from same client in past.

    The server never relies on information from previous requests. Statelessness As per the REST (REpres ...

  9. Smoke testing (software)

    Smoke testing (software) - Wikipedia https://en.wikipedia.org/wiki/Smoke_testing_(software) In compu ...

  10. 执行sql语句异常...需要的参数与提供的值个数不匹配

    执行mysql语句时,出现以下错误时. 看错误提示,提示说你的sql语句只需要5个参数,而你提供了8个值value,你确定你确实需要8个参数,而你的sql语句却提示说只需要5个参数 这时,请仔细检查一 ...