zoukankan      html  css  js  c++  java
  • Selenium HtmlUnitDriver 多浏览器的支持

    1、HtmlUnitDriver
    WebDriver包括一个基于HtmlUnit的无界面实现,称为HtmlUnitDriver,即使用HtmlUnit时并不会打开真实的浏览器,而是在内存中执行代码,因此运行速度很快,但是对JavaScript的支持不够好,当页面上有复杂的JavaScript元素时,经常捕捉不到。
    eclipse测试例子如下:
    WebDriver dr = new HtmlUnitDriver();
    dr.get(“http://www.baidu.com“);
    WebElement element = dr.findElement(By.name(“wd”));
    element.sendKeys(“webdriver”);
    element.submit();
    Thread.sleep(5000);
    System.out.println(“page title is:”+dr.getTitle());
    运行成功时控制台会打印百度搜索页面标题“page title is:webdriver_百度搜索”。
    2、Firefox
    WebDriver实现了FireFoxDriver,无需用户下载FireFoxDriver。
    优点:FireFoxDriver对页面的自动化测试支持得比较好,很直观地模拟页面的操作,对JavaScript的支持也非常完善,基本上页面上做的所有操作FireFox Driver都可以模拟。
    缺点:启动很慢,运行也比较慢,不过,启动之后Webdriver的操作速度虽然不快但还是可以接受的,建议不要频繁启动停止FireFoxDriver。
    使用Firefox浏览器只需要设置WebDriver driver = new FirefoxDriver(),前提是你的Firefox被安装在默认的位置。
    操作系统 Firefox默认安装位置
    Linux firefox (found using “which”)
    Mac /Applications/Firefox.app/Contents/MacOS/firefox
    Windows %PROGRAMFILES%Mozilla Firefoxfirefox.exe
    如果你的FireFox没有被安装在指定的位置,可以设置“webdriver.firefox.bin”来指定它的位置,java代码如下:
    System.setProperty(“webdriver.firefox.bin”,”thelocation of Firefox”);
    eclipse测试例子如下:
    System.setProperty(“webdriver.firefox.bin”,”D:Mozilla Firefoxfirefox.exe”);
    WebDriver dr = new FirefoxDriver();
    dr.get(“http://www.baidu.com“);
    WebElement element = dr.findElement(By.name(“wd”));
    element.sendKeys(“webdriver”);
    element.submit();
    Thread.sleep(5000);
    System.out.println(“page title is:”+dr.getTitle());
    3、Chrome
    webdriver没有实现chromedriver,要使用chrome浏览器需要自己下载chromedriver.exe(下载地址:http://code.google.com/p/chromedriver/downloads/list),这个程序是由Chrome团队提供的,你可以看做它是链接WebDriver和Chrome浏览器的桥梁。
    eclipse例子如下:
    System.setProperty(“webdriver.chrome.driver”,”D:chromedriverchromedriver.exe”); //指定chromedriver的路径
    System.setProperty(“webdriver.chrome.bin”,”C:Documents and SettingsgongjfLocal SettingsApplication DataGoogleChromeApplicationchrome.exe”); //chrome没有安装在默认路径时,指定chrome.exe的路径
    WebDriver driver = new ChromeDriver();
    driver.get(“http://www.baidu.com“);
    4、IE
    webdriver要使用IE浏览器需要下载InternetExplorerDriver.exe(下载地址:http://code.google.com/p/selenium/downloads/list),根据浏览器的版本下载32位或者64位的driver。
    注意:需要将IE浏览器各个区域的保护模式设置的一样,要么全勾选,要么全不勾选,工具–Internet选项–安全。还需要将页面的缩放比例设置为100%
    优点:直观地模拟用户的实际操作,对JavaScript提供完善的支持。
    缺点:是所有浏览器中运行速度最慢的,并且只能在Windows下运行,对CSS以及XPATH的支持也不够好。
    System.setProperty(“webdriver.ie.driver”,”D:iedriverIEDriverServer.exe”); //设置IEDriverService.exe的路径;如果IE没有安装在默认目录,同样需要设置webdriver.ie.bin
    WebDriver driver = new InternetExplorerDriver();
    driver.get(“http://www.baidu.com“);

    事在人为,功不唐捐
  • 相关阅读:
    Python 操作Excel之通过xlutils实现在保留原格式的情况下追加写入数据
    【转载】Python字符串操作之字符串分割与组合
    【转】Python判断字符串是否为字母或者数字
    Appium 在测试android混合应用时,关于webview页面切换的那些事儿
    使用pip install XX 命令时报错
    Appium笔记(二) 丶Appium的安装
    Android SDK的下载与安装
    KlayGE 4.4中渲染的改进(五):OpenGL 4.4和OpenGLES 3
    最先进的开源游戏引擎KlayGE 4.4发布
    KlayGE 4.4中渲染的改进(四):SSSSS
  • 原文地址:https://www.cnblogs.com/xinleishare/p/4372595.html
Copyright © 2011-2022 走看看