Class: Browser
- Inherits:
-
Object
- Object
- Browser
- Defined in:
- lib/actir/webdriver/browser.rb
Overview
初始化测试用浏览器
@author: Hub
@Date: 2015-1-5
Constant Summary collapse
- PHANTOMJS_SIZE =
{'width' => 1334, 'height' => 750}
- REMOTE_SIZE =
{'width' => 1050, 'height' => 611}
Instance Attribute Summary collapse
-
#browser ⇒ Object
readonly
Returns the value of attribute browser.
Instance Method Summary collapse
-
#browser_wap ⇒ Object
通过useragent打开WAP页面.
-
#browser_www ⇒ Object
打开普通www浏览器.
-
#define_page_method ⇒ Object
自动定义驱动需要调用的所有元素相关的方法 需要所有的页面元素相关的类名以XXXPage方式命名.
- #goto(uri) ⇒ Object
-
#init_args(args = {}) ⇒ Object
初始化入参.
-
#initialize(type = :www, *args) ⇒ Browser
constructor
初始化函数.
- #method_missing(m, *args, &blk) ⇒ Object
-
#new_window(url) ⇒ Object
在同一个浏览器的新tab页上打开指定url的页面,并自动将焦点放到新开窗口中.
- #valid_page_klass?(klass) ⇒ Boolean (also: #basic_page?)
Constructor Details
#initialize(type = :www, *args) ⇒ Browser
初始化函数
可以配置测试的mode:
1.:mode
:local 本地环境上测试
:remote 远程测试环境上测试
2.:browser
:chrome
:firefox
3.:agent wap页面的useragent类型
:iphone
:android_phone
4.:address 当mode为:remote时,url指定远程执行脚本的地址
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/actir/webdriver/browser.rb', line 32 def initialize(type = :www, *args) args = init_args(*args) @browser_type = args[:browser] @agent = args[:agent] @mode = args[:mode] @window_size = args[:window_size] if @mode == :remote @url = if args[:url] "http://#{args[:url]}/wd/hub" else #TO-DO ,远程模式没有传入IP,改成local模式 @mode = :local puts "selenium-node's IPAddress is null. switch to local mode" end end if type == :www @watir_browser = browser_www elsif type == :wap @watir_browser = browser_wap else raise "error browser type , please send args with :www or :wap!" end define_page_method end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &blk) ⇒ Object
205 206 207 208 209 210 211 |
# File 'lib/actir/webdriver/browser.rb', line 205 def method_missing(m, *args, &blk) if @watir_browser.respond_to? m @watir_browser.send(m, *args, &blk) else super end #if end |
Instance Attribute Details
#browser ⇒ Object (readonly)
Returns the value of attribute browser.
12 13 14 |
# File 'lib/actir/webdriver/browser.rb', line 12 def browser @browser end |
Instance Method Details
#browser_wap ⇒ Object
通过useragent打开WAP页面
local模式下直接打开本地chrome浏览器,或者使用phantomjs无界面执行
remote模式下通过设置chrome的switches参数远程打开测试环境上的chrome浏览器
TO-DO: remote模式的phantomjs
162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/actir/webdriver/browser.rb', line 162 def browser_wap case @mode when :local driver = Actir::Webdriver.driver(:browser => @browser_type, :agent => @agent) when :remote driver = Actir::Webdriver.driver(:browser => @browser_type, :agent => @agent, :url => @url) end browser = Watir::Browser.new driver if @browser_type == :phantomjs #重新设置窗口大小,不然phantomjs的ghost driver各种问题 = = browser.window.resize_to(PHANTOMJS_SIZE["width"], PHANTOMJS_SIZE["height"]) end browser end |
#browser_www ⇒ Object
打开普通www浏览器
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/actir/webdriver/browser.rb', line 129 def browser_www case @mode when :local #本地chrome浏览器 browser = Watir::Browser.new @browser_type when :remote #远程服务器的chrome浏览器 browser = Watir::Browser.new( :remote, :desired_capabilities => @browser_type, :url => @url ) end #重新设置窗口大小,不然phantomjs的ghost driver各种问题 if @browser_type == :phantomjs browser.window.resize_to(PHANTOMJS_SIZE["width"], PHANTOMJS_SIZE["height"]) elsif @mode == :remote browser.window.resize_to(REMOTE_SIZE["width"], REMOTE_SIZE["height"]) elsif @window_size != nil browser.window.resize_to(@window_size["width"], @window_size["height"]) end browser end |
#define_page_method ⇒ Object
自动定义驱动需要调用的所有元素相关的方法 需要所有的页面元素相关的类名以XXXPage方式命名
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/actir/webdriver/browser.rb', line 179 def define_page_method p Module.constants.grep(/(Page|Wap)$/) if $debug Module.constants.grep(/(Page|Wap)$/).each do |page_klass| if basic_page?(page_klass) method = "" if page_klass.to_s =~ /Page$/ method_name = page_klass.to_s.gsub!(/Page$/, "_page") elsif page_klass.to_s =~ /Wap$/ method_name = page_klass.to_s.gsub!(/Wap$/, "_wap") end self.class.send :define_method, "#{method_name.downcase}" do page = Module.const_get(page_klass).new(@watir_browser) page end #define_method puts "defined #{method_name.downcase}" if $debug end #if end #each end |
#goto(uri) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/actir/webdriver/browser.rb', line 57 def goto(uri) hasLoaded = 0 for i in 1..3 begin Timeout::timeout(10) do # puts "Time #{i}" super(uri) if self.execute_script("return document.readyState;") == "complete" # puts "has completed" hasLoaded = 1 break end end rescue Timeout::Error => e puts "Page load timed out: #{e}" end if hasLoaded == 1 break end sleep(0.05) end end |
#init_args(args = {}) ⇒ Object
初始化入参
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/actir/webdriver/browser.rb', line 83 def init_args(args = {}) # config_exist = File.exist?(config_file) config_exist = ($config != nil) unless args.has_key?(:mode) && args[:mode] != nil #若通过actir执行测试用例,则会配置ENV的模式 if ENV["mode"] args[:mode] = ENV["mode"].to_sym else #若ENV为空,则读取配置文件,判断有无配置文件 if config_exist mode = $config["config"]["test_mode"]["mode"] args[:mode] = (mode == nil) ? :local : mode else args[:mode] = :local end end end unless args.has_key?(:browser) && args[:browser] != nil if config_exist browser_type = $config["config"]["test_mode"]["browser"] args[:browser] = (browser_type == nil) ? :chrome : browser_type else args[:browser] = :chrome end end unless args.has_key?(:window_size) && args[:window_size] != nil if config_exist window_size = $config["config"]["window_size"] if window_size != nil width = window_size["width"] height = window_size["height"] end args[:window_size] = (width == nil || height == nil) ? nil : window_size else args[:window_size] = nil end end args[:agent] = :iphone unless args.has_key?(:agent) && args[:agent] != nil args[:url] = $address unless args.has_key?(:url) && args[:url] != nil args end |
#new_window(url) ⇒ Object
在同一个浏览器的新tab页上打开指定url的页面,并自动将焦点放到新开窗口中
223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/actir/webdriver/browser.rb', line 223 def new_window url @watir_browser.execute_script("window.open(\"#{url}\")") #获取浏览器中的窗口数目 window_num = @watir_browser.windows.length #默认要操作的新窗口都是最后一个窗口 @watir_browser.windows[window_num - 1].use do #重新设置窗口大小,不然phantomjs的ghost driver各种问题 = = if @browser_type == :phantomjs @watir_browser.window.resize_to(PHANTOMJS_SIZE["width"],PHANTOMJS_SIZE["height"]) end #使用执行块,由调用者决定在新开的订单详情页面内做些啥 yield end end |