• selenium-webdriver


    ruby环境下selenium/webdriver可以通过selenium-webdriver.gem包进行安装

    gem install selenium-webdriver
     
    支持语言及版本有ruby 1.8.7~1.9.2,jrbuy和rubinius
     
    selenium-webdriver包含了selenium-client,在阅读的时候,要注意它们两个命名空间是在两不同的API里:
    1.Selenium::WebDriver - WebDrver API
    2.Selenium::Client - Selenium RC API
     
    WebDrver API是继承自Selenium RC API,所以没有必要在Selenium RC API花大量的时间,我们可以直接从Selenium::WebDriver开始,并围绕两个大类:Seleniu::WebDriver:Driver和Selenium::WebDriver::Element,这是整个WebDriver API的入口。
     
    API 例子:
    一个简单的例子:
    require "selenium-webdriver"
     
    driver = Selenium::WebDriver.for :firefox
    driver.navigate.to "http://www.google.com.hk"
     
    element = driver.find_element(:name,'q')
    element.send_keys "Hello WebDriver"
    element.submit
     
    puts driver.title
     
    driver.quit
     
    Driver 例子:
    # 应用javascript
    puts driver.execute_script("return window.location.pathname")
     
    # 利用ruby和js获取元素
    element = driver.execut_script("return document.body")
    driver.execut_script("return arguments[0].tagname", element) #=> "BODY"
     
    # 等待一些特殊元素的出现
    wait = Selenium::WebDriver::Wait.new(:timeout=>10) # seconds
    wait.until{driver.find_element(:id,"foo")}
    # 注:wait在new时,可以设置三个值,分别为:timeout(默认5秒),:message(默认nil),:interval(默认0.5)
     
    # 选择frame
    driver.switch_to.frame "some-frame" # name或id
    driver.switch_to.frame driver.find_element(:id, 'some-frame') # frame
    # 注:switch_to方法不仅可以选择frame,还可以处理window,alert,comfirmation等窗口
     
    # 选择回主窗口
    driver.swith_to.default_content
     
    Element 例子:
    # 获取元素属性
    class_name = element.attr
     
    ibute("class")
    # 判断元素是否显示
    element.displayed?
     
    # 获取元素在页面上的相对坐标位置
    element.location
    element.location.x
    element.location.y
     
    # 将元素滚动到视频可以显示的位置,再返回元素的相对坐标
    element.location_once_scrolled_into_view
     
    # 获取元素的宽和高
    element.size
     
    # 在元素里输入空,参看Selenium::WebDriver::Keys输入值
    element.send_keys :space
    element.send_keys "tet", :arrow_left, "s" #=> "test", 先输入 tet, 再输入一次左方向键,再输入s
    element.send_keys [:control, 'a'], "1" #=> "1", 先输入crtl+a,再输入1
     
     
    # 获取元素文本
    element.text
     
     
    更高级的用法(见 ActionBuilder)
    driver.action.key_down(:shift).click(element).double_click(second_element).key_up(:shift).drag_and_drop(element,third_element).perform
     
     
    启动chrome浏览器的方法 
    1.下载ChromeDriver并运行,如图
     
    2.启动chrome
    driver = Selenium::WebDriver.for :remote, :url=>"http://localhost:9515"
    driver.get "http://www.google.com.hk"
    其它操作一样。
     
    如果这样感觉比较麻烦,可以将下载的chromedriver.exe路径加载到环境变量中,就可直接用了
    driver = Selenium::WebDriver.for :chrome
     
     
    chrome个性化启动
    profile = Selenium::WebDriver::Chrome::Profile.new
    profile['download.prompt_for_download'] = false
    profile['download.default_directory'] = "d:/download"
     
    driver = Selenium::WebDriver.for :chrome, :profile=>profile
     
    Remote应用
    RomteWebDriver可以控制浏览器在不同的机器上运行,下载selenium-server-standlone-x.xx.x.jar
    java -jar selenium-server-standalone.jar '启动服务
     
    driver = Selenium::WebDriver.for :remote
     
    默认情况下,可以启动服务运行在localhost:4444,并打开firefox。如果想连接其它机器上的服务,可以用:url选项
    driver = Selenium::WebDriver.for :remote, :url=>"http://remoteserver:44444/wd/hub"
     
    本机不用加/wd/hub,但远程一定要加
     
    启动其它的浏览器,用:desired_capabilities选项
    driver = Selenium::WebDriver.for :remote, :desired_capabilities=>:chrome
     
     
    Selenium::WebDriver::Remote::Capabilities的例子
     
    require "selenium-webdriver"
    include Selenium
     
    caps = WebDriver::Remote::Capabilities.htmlunit :javascript_enabled=>true
    driver = WebDriver.for :remote, :desired_capalibities=>caps
     
     
     
    修改remote服务的端口号,目前只支持firefox
     
    include Selenium
     
    caps = WebDriver::Remote::Capabilities.firefox (:proxy=>WebDriver::Proxy.new(:http=>"myproxyaddress:8888"))
    driver = WebDriver.for :remote, :desired_capalibities=>caps
     
    如果是一个远程的remote服务
     
    include Selenium
     
    client = WebDriver::Remote::Http::Default.new
     
    client.proxy = Proxy.new :http=>"proxy.org:8888"
    driver = WebDriver.for :remote, :http_client=>client
     
     
    Firefox
    加扩展插件
    在用firefox时,经常要用到firebug进行查看,启动firefox带firebug
    include Selenium
     
    profile = WebDriver::Firefox::Profile.new
    profile.add_extension "path/firebug.xpi"
     
    driver = WebDriver.for :firefox, :profile=>profile
     
    使用已经存在的profile
    使用一个已经存在的profile模板,可以用firefox -profilemanger把profile保存出来(这个命令在ff8上似乎无用,还要进一步验证)
     
    driver = Selenium::WebDriver.for :firefox, :profile=>"my_existing_profile"
     
    如果想用默认profile,可以通过 :profile=>"default"
     
    或者也可以这通过profile实例来使用已经存在的和自定义的profile。此方法不能修改已经存在的profile,且只能在webdriver下使用
     
    default_profile = Selenium::WebDriver::Firdfox::Profile.from_name "default"
    default_profile.native_events = true
     
    driver = Selenium::WebDriver.for :firefox, :profile=>default_profile
     
    导出firefox profile见《Firefox 8导出profile文件》
     
    Firefox个性化设置
    端口号
    profile = Selenium::WebDriver::Firefox::Profile.new
    profile['browser.download.dir'] = 'd:download'
    profile['browser.download.folderlist'] = 2
    profile['browser.helperApps.neverAsk.saveToDisk'] = "application/pdf"
     
    driver = Selenium::WebDriver.for :firefox, :profile=>profile
     
    用remote driver启动时,任然可以对firefox进行个性化设置
     
    profile = Selenium::WebDriver::Firfox::Profile.new
    profile['foo.bar'] = true
     
    capabilities = Selenium::WebDriver::Remote::Capabilities.firefox :firefox_profile=>profile
    driver = Selenium::WebDriver.fox :remote, :desired_capabilities=>capabilities
     
    设置Firefox路径
    当firfox不是安装在默认路径中时,要设定firefox的安装路径
    Selenium::WebDriver::Firefox.path = "/path/to/firefox" # firefox安装路径
    driver = Selenium::WebDriver.for :firefox
     
     
    本地事件
    本地事件在window下默认是激活的,可以设置关闭:
    profile = Selenium::WebDriver::Firefox::Profile.new
    profile.native_events = false # 关闭
     
    driver = Selenium::WebDriver.for :firefox, :profile=>profile
     
    理论上linux支持本地事件,profile.native_events = true # 开启
     
     
    Opera
    opera也是用remote的方法进行启动的。在开始之前,要下载selenium-server-standalone-x.xx.x.jar,并且新建环境变量SELENIUM_SERVER_JAR到本地系统中。
    window: SELENIUM_SERVER_JAR=..server-standalone.jar 
    liunx: export SELENIUM_SERVER_JAR=path_to/server-standalone.jar 
     
    便可以轻松的用Selenium::WebDriver启动Opera
     
    driver = Selenium::WebDriver.for :opera
    driver.navigate.to "http://www.baidu.com"
     
    Timeouts
     
    隐式等待
    WebDriver可以设置隐式等待,所以在应用#find_element定位元素时,会等待元素的出现,直至NoSuchElementError的出现。
     
    driver = Selenium::WebDriver.for :firefox
    driver.manager.timeouts.implicit_wait = 3 # 等待3秒
     
    显示等待
    可以用wait类去实例化一些等待条件
     
    wait = Selenium::WebDriver::Wait.new(:timeout=>3)
    wait.until {driver.find_elenium(:id=>"foo").displayed?}
     
     
    内部的超时
    WebDriver使用了大量的HTTP的驱动(jsonWireProtocol)。Net::HTTP作为ruby的标准库使用,默认超时为60秒。如果用WebDriver#get启动一个加载时间超过60秒的页面,你会看Net::HTTP的TimeoutError错误,可以在启动浏览器之前修改timeout来改变默认超时时长。
     
    client = Selenium::WebDriver::Remote::Http::Default.new
    client.titmeout = 120 # 设置为120秒
    driver = Selenium::WebDriver.for :temote, :http_client=>client
     
    js弹出框
    获取js的alert,prompt和comfirm弹出窗都是用switch_to
     
     
    require "selenium-webdriver"
     
    driver = Selenium::WebDriver.for :firefox
    driver.navigate.to "http://mysite.com/page_with_alert.html"
     
    driver.find_element(:name, 'element_with_alert_javascript').click
    a = driver.switch_to.alert
    if a.text == 'A value you are looking for'
      a.dismiss
    else
      a.accept
    end
     
     
    用Curb或者自己的http client
    HTTP通信内部默认使用Net::HTTP,如果有安装Curb gem就可以选择这么做
     
    require "selenium/webdriver/remot/http/curb"
    include Selenium
     
    client = WebDriver::Remote::Http::Curb.new
    driver = WebDriver.for :remote, :http_client=>client
  • 相关阅读:
    Android 2.2 r1 API 中文文档系列(11) —— RadioButton
    Android API 中文 (15) —— GridView
    Android 中文 API (16) —— AnalogClock
    Android2.2 API 中文文档系列(7) —— ImageButton
    Android2.2 API 中文文档系列(6) —— ImageView
    Android 2.2 r1 API 中文文档系列(12) —— Button
    Android2.2 API 中文文档系列(8) —— QuickContactBadge
    [Android1.5]TextView跑马灯效果
    [Android1.5]ActivityManager: [1] Killed am start n
    Android API 中文(14) —— ViewStub
  • 原文地址:https://www.cnblogs.com/hua198/p/5709355.html
Copyright © 2020-2023  润新知