Module: RWebUnit::Popup

Included in:
Driver
Defined in:
lib/rwebunit/popup.rb

Instance Method Summary collapse

Instance Method Details

#check_for_popupsObject

Start background thread to click popup windows

Warning:
  Make browser window active
  Don't mouse your mouse to focus other window during test execution


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rwebunit/popup.rb', line 11

def check_for_popups
  autoit = WIN32OLE.new('AutoItX3.Control')
  #
  # Do forever - assumes popups could occur anywhere/anytime in your
  # application.
  loop do
    # Look for window with given title. Give up after 1 second.
    ret = autoit.WinWait('Windows Internet Explorer', '', 1)
    #
    # If window found, send appropriate keystroke (e.g. {enter}, {Y}, {N}).
    if (ret==1) then
      autoit.Send('{enter}')
    end
    #
    # Take a rest to avoid chewing up cycles and give another thread a go.
    # Then resume the loop.
    sleep(3)
  end
end

#check_for_security_alertsObject

Check for “Security Information” and “Security Alert” alert popup, click ‘Yes’

Usage: For individual test suite

before(:all) do

$popup = Thread.new { check_for_alerts }
open_in_browser
...

end

after(:all) do

close_browser
Thread.kill($popup)

end

or for all tests,

$popup = Thread.new { check_for_alerts }
at_exit{ Thread.kill($popup) }


50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rwebunit/popup.rb', line 50

def check_for_security_alerts
  autoit = WIN32OLE.new('AutoItX3.Control')
  loop do
    ["Security Alert", "Security Information"].each do |win_title|
      ret = autoit.WinWait(win_title, '', 1)
      if (ret==1) then
        autoit.Send('{Y}')
      end
    end
    sleep(3)
  end
end

#click_button_in_javascript_popup(button = "OK") ⇒ Object Also known as: click_javascript_popup



81
82
83
# File 'lib/rwebunit/popup.rb', line 81

def click_button_in_javascript_popup(button = "OK")
  verify_alert()
end

#click_button_in_popup_after(options = {:button => "OK", :wait_time => 3}, &block) ⇒ Object

Click the button in javascript popup dialog Usage:

click_button_in_popup_after { click_link('Cancel')}
click_button_in_popup_after("OK") { click_link('Cancel')}


137
138
139
140
141
142
143
144
# File 'lib/rwebunit/popup.rb', line 137

def click_button_in_popup_after(options = {:button => "OK", :wait_time => 3}, &block)
  if is_windows?  then
    start_checking_js_dialog(options[:button], options[:wait_time])
    yield
  else
    raise "this only support on Windows and on IE"
  end
end

#click_button_in_security_alert_popup(button = "&Yes") ⇒ Object Also known as: click_security_alert_popup



76
77
78
# File 'lib/rwebunit/popup.rb', line 76

def click_button_in_security_alert_popup(button = "&Yes")
  verify_alert("Security Alert", "", button)
end

#click_button_in_security_information_popup(button = "&Yes") ⇒ Object Also known as: click_security_information_popup



71
72
73
# File 'lib/rwebunit/popup.rb', line 71

def click_button_in_security_information_popup(button = "&Yes")
  verify_alert("Security Information", "", button)
end

#click_popup_window(button, wait_time = 9, user_input = nil) ⇒ Object



105
106
107
108
# File 'lib/rwebunit/popup.rb', line 105

def click_popup_window(button, wait_time= 9, user_input=nil )
  @web_browser.start_clicker(button, wait_time, user_input)
  sleep 0.5
end

#ie_popup_clicker(button_name = "OK", max_wait = 15) ⇒ Object

This only works for IEs

Cons:
  - Slow
  - only works in IE
  - does not work for security alert ?


92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rwebunit/popup.rb', line 92

def ie_popup_clicker(button_name = "OK", max_wait = 15)
  require 'watir/contrib/enabled_popup'
  require 'win32ole'
  hwnd = ie.enabled_popup(15)
  if (hwnd)  #yeah! a popup
    popup = WinClicker.new
    popup.makeWindowActive(hwnd) #Activate the window.
    popup.clickWindowsButton_hwnd(hwnd, button_name) #Click the button
    #popup.clickWindowsButton(/Internet/,button_name,30)
    popup = nil
  end
end

#prepare_to_click_button_in_popup(button = "OK", wait_time = 3) ⇒ Object

run a separate process waiting for the popup window to click



112
113
114
115
116
117
118
119
120
# File 'lib/rwebunit/popup.rb', line 112

def prepare_to_click_button_in_popup(button = "OK", wait_time = 3)
  #  !@web_browser.is_firefox?
  # TODO: firefox is OK
  if RUBY_PLATFORM =~ /mswin/  then
    start_checking_js_dialog(button, wait_time)
  else
    raise "this only support on Windows and on IE"
  end
end

#start_checking_js_dialog(button = "OK", wait_time = 3) ⇒ Object

Start a background process to click the button on a javascript popup window



123
124
125
126
127
128
129
130
# File 'lib/rwebunit/popup.rb', line 123

def start_checking_js_dialog(button = "OK", wait_time = 3)
  w = WinClicker.new
  longName = File.expand_path(File.dirname(__FILE__)).gsub("/", "\\" )
  shortName = w.getShortFileName(longName)
  c = "start ruby #{shortName}\\clickJSDialog.rb #{button} #{wait_time} "
  w.winsystem(c)
  w = nil
end

#verify_alert(title = "Microsoft Internet Explorer", button = "OK") ⇒ Object



63
64
65
66
67
68
69
# File 'lib/rwebunit/popup.rb', line 63

def verify_alert(title = "Microsoft Internet Explorer", button = "OK")
  if is_windows? && !is_firefox?
    WIN32OLE.new('AutoItX3.Control').ControlClick(title, '', button)
  else
    raise "This function only supports IE"
  end
end