This repository has been archived by the owner on Jun 6, 2018. It is now read-only.
/
window.rb
68 lines (57 loc) · 1.55 KB
/
window.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module Watir
class Window
include ElementExtensions
class << self
attr_accessor :__main_ie
def wrap *meths
meths.each do |meth|
define_method meth do
result = nil
use {result = browser.send(meth)}
result
end
end
end
end
def initialize(main_browser, locators, browser=nil, &blk)
valid_locators = [:title, :url, :hwnd, :index]
locators.each_pair do |k, v|
raise ArgumentError, "Valid locators are #{valid_locators.join(", ")}" unless valid_locators.include?(k)
end
@main_browser = main_browser
self.class.__main_ie = main_browser.ie
@locators = locators
@browser = browser
end
wrap :url, :title, :hwnd, :close
def browser
@browser ||= begin
IE.find(@locators.keys.first, @locators.values.first)
end
end
def use(&blk)
@main_browser.ie = browser.ie
if blk
begin
blk.call
ensure
@main_browser.ie = self.class.__main_ie
# try to find some existing IE when needed
@main_browser.ie = IE._find(:index, 0) unless @main_browser.exists?
end
end
self
end
def current?
@main_browser.hwnd == browser.hwnd && @main_browser.html == browser.html
end
def ==(other)
browser.hwnd == other.hwnd && browser.html == other.browser.html
end
alias_method :eql?, :==
def present?
@browser = nil
browser && browser.exists?
end
end
end