Selenium WebDriver Demo
2015-11-05 16:33
417 查看
1 实现的功能和环境介绍
使用Selenium WebDriver做了简单的Demo,功能如下。- 测试对象:基于Web的Foglight产品
- 测试步骤:使用Chrome登录Foglight,进入到CreateRule页面,创建新的一条Rule,然后退出浏览器。
- CreateRule的界面如下:
2 碰到的问题
运行测试程序时提示IllegalStateExceptionException 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/ STAFhttps://github.com/SeleniumHQ/selenium/wiki/ChromeDriver ChromeDriver
相关文章推荐
- 把Chrome OS安装在VMWare下及Chrome OS的关键特性和一些截图
- 小白观察:微软释放出其基于 Chromium 的 Edge 浏览器
- Chrome的hack写法以及CSS的支持程度图示
- Chrome Visual Studio 2005下的编译过程
- 在firefox和Chrome下关闭浏览器窗口无效的解决方法
- chrome下jq width()方法取值为0的解决方法
- chrome不支持form.submit的解决方案
- chrome扩展学习 右键菜单实现代码
- chrome浏览器不支持onmouseleave事件的解决技巧
- javascript判断chrome浏览器的方法
- Ubuntu、Linux Mint一键安装Chrome浏览器的Shell脚本分享
- 如何解决ajax在google chrome浏览器上失效
- Linux下的Chrome快捷键详细整理
- Chrome内核下由ashx输出的js代码不起作用的解决方法
- chrome调试javascript详解
- 使用Chrome浏览器调试Android App详解
- 针对Ruby的Selenium WebDriver安装指南
- Chrome Web App开发小结
- FCKeditor 在chrome中不显示问题
- chrome浏览器当表单自动填充时如何去除浏览器自动添加的默认样式