Selenium WebDriver Demo

1 实现的功能和环境介绍

使用Selenium WebDriver做了简单的Demo,功能如下。
- 测试对象:基于Web的Foglight产品
- 测试步骤:使用Chrome登录Foglight,进入到CreateRule页面,创建新的一条Rule,然后退出浏览器。
- CreateRule的界面如下:
这里写图片描述

2 碰到的问题

  • 运行测试程序时提示IllegalStateException

Exception in thread “main” java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html

解决方案:
设置chromedriver的系统参数,告诉程序运行时,可以找到对应的ChromeDriver。
代码如下:

System.setProperty("webdriver.chrome.driver", "D:\\EclipseWorkspace\\SeleniumHQ\\selenium-lib\\chromedriver.exe");
  • 运行时,提示Chrome版本太低
    解决方案:升级Chrome版本。由于本机Browser可以翻墙,但是Chrome官网提供的是一个下载器,用于下载最新的Chrome版本。如果要想直接下载安装包,可以使用如下链接:
https://www.google.com/chrome/browser/desktop/index.html?standalone=1

注意:加了standalone参数。这样方式下载,需要浏览器可以翻墙。

  • 查找Create Rule页面的元素时,通过Firebug可以找到,但是无法通过程序找到
    解决方案:Create Rule使用了iFrame来实现,会导致使用程序查找时,默认在Top Window中查找。如果要在Create Rule中查找,需要先切换到iFrame元素中。代码如下:
         //由于Create Rule页面是通过iFrame框架实现的。所以需要找到iframe的节点后,切换到iframe的页面去操作
         WebElement frameNode = driver.findElement(By.xpath("//iframe"));       
         driver.switchTo().frame(frameNode);

3 Todo List

要完整的走一个自动化的流程,还需要提供如下功能:

  • 自动安装和启动Foglight:可以使用ibm的staf
  • 使用xUnit来管理test case:Junit3/Junit4/test ng
  • 生产可读性强的report报告

操作步骤代码如下:

public void testCreateRule(){
        //调整到Create Rule页面
         WebElement selectNode;
         selectNode = driver.findElement(By.xpath("//div[@nodeid='system:administration']/img"));
         selectNode.click();
         waitTime(1000);

         selectNode = driver.findElement(By.xpath("//div[@nodeid='system:administration_rulesnotifications']/img"));
         selectNode.click();
         waitTime(1000);

         selectNode = driver.findElement(By.xpath("//div[@nodeid='system:administration_rulesnotifications.1']/span"));
         selectNode.click();
         waitTime(5000);

         //由于Create Rule页面是通过iFrame框架实现的。所以需要找到iframe的节点后,切换到iframe的页面去操作
         WebElement frameNode = driver.findElement(By.xpath("//iframe"));       
         driver.switchTo().frame(frameNode);

         //填写数据      
         WebElement inputNode;
         inputNode = driver.findElement(By.xpath("//input[@id='myRuleName']"));
         inputNode.sendKeys("testRule");        
         inputNode = driver.findElement(By.id("nodomain"));
         inputNode.click();
         waitTime(2000);

         WebElement NextButton;
         NextButton = driver.findElement(By.id("nextButton"));
         if(NextButton.isEnabled()){
             NextButton.click();
         }else{
             waitTime(5000);
             NextButton.click();
         }

         WebElement condition = driver.findElement(By.id("titlebar_fire"));
         condition.click();

         condition = driver.findElement(By.xpath("//textarea[@id='condition1']"));
         condition.sendKeys("true");
         waitTime(1000);

         condition = driver.findElement(By.xpath("//span[@title='Validate Condition']/img"));
         condition.click();
         waitTime(1000);

         NextButton = driver.findElement(By.id("nextButton"));
         NextButton.click();
         waitTime(2000);

         WebElement Schedules = driver.findElement(By.xpath( "//optgroup[@label='--- Available Schedules ---']/option[18]"));
         Schedules.click();
         waitTime(1000);

         Schedules = driver.findElement(By.xpath("//input[@value='Add>>'][1]"));
         Schedules.click();
         waitTime(1000);

         WebElement finish = driver.findElement(By.id("finishButton"));
         if(finish.isEnabled()){
             finish.click();
         }
         //切换到父页面后,继续操作。否则可能会导致后面的操作找不到节点
         driver.switchTo().parentFrame();

    }

4 Reference

http://staf.sourceforge.net/ STAF
https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver ChromeDriver

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值