Module: WatirRobot::Native

Included in:
KeywordLibrary
Defined in:
lib/watir_robot/keywords/native.rb

Overview

Native functionality for manipulating the mouse and keyboard via Java’s java.awt.Robot class

Instance Method Summary collapse

Instance Method Details

#capture_screenshot(path = nil) ⇒ Object

Take a screenshot of the entire screen

Parameters:

  • path (String) (defaults to: nil)

    the path (including file name) to which to save the screenshot image



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/watir_robot/keywords/native.rb', line 132

def capture_screenshot(path = nil)
  t = Toolkit.getDefaultToolkit
  s = t.getScreenSize
  rect = Rectangle.new(0, 0, s.width, s.height);
  
  @robot ||= Robot.new
  image = @robot.createScreenCapture(rect)
  if path.nil?
    path = Time.now.to_i.to_s + '_screenshot.png'
  end
  file = java.io.File.new(path)
  ImageIO.write(image, 'png', file)
end

#click_left_mouse_buttonObject

Left mouse click



63
64
65
66
67
# File 'lib/watir_robot/keywords/native.rb', line 63

def click_left_mouse_button
  @robot ||= Robot.new
  press_left_mouse_button
  release_left_mouse_button
end

#click_right_mouse_buttonObject

Right mouse click



72
73
74
75
76
# File 'lib/watir_robot/keywords/native.rb', line 72

def click_right_mouse_button
  @robot ||= Robot.new
  press_right_mouse_button
  release_right_mouse_button
end

#drag_and_drop(start_x, start_y, finish_x, finish_y) ⇒ Object

Drag and drop with the mouse

This is at the operating system level. Consider using WebDriver’s facilities for drag and drop if you need drag-and-drop within a web page.

Parameters:

  • start_x (String)

    x-axis coordinate of initial mouse press (as string because arg comes from Robot Framework)

  • start_y (String)

    y-axis coordinate of iniital mouse press (as string because arg comes from Robot Framework)

  • finish_x (String)

    x-axis coordinate of final mouse release (as string because arg comes from Robot Framework)

  • finish_y (String)

    y-axis coordinate of final mouse release (as string because arg comes from Robot Framework)



119
120
121
122
123
124
125
# File 'lib/watir_robot/keywords/native.rb', line 119

def drag_and_drop(start_x, start_y, finish_x, finish_y)
  @robot ||= Robot.new
  move_mouse_to_position(start_x.to_i, start_y.to_i)
  press_left_mouse_button
  move_mouse_to_position(finish_x.to_i, finish_y.to_i)
  release_left_mouse_button
end

#move_mouse_to_position(x, y) ⇒ Object

Move mouse to specific coordinates

Parameters:

  • x (String)

    x-axis coordinate (as string because arg comes from Robot Framework)

  • y (String)

    y-axis coordinate (as string because arg comes from Robot Framework)



23
24
25
26
# File 'lib/watir_robot/keywords/native.rb', line 23

def move_mouse_to_position(x, y)
  @robot ||= Robot.new
  @robot.mouseMove(x.to_i, y.to_i)
end

#press_left_mouse_buttonObject

Left mouse press



31
32
33
34
# File 'lib/watir_robot/keywords/native.rb', line 31

def press_left_mouse_button
  @robot ||= Robot.new
  @robot.mousePress(InputEvent::BUTTON1_MASK)
end

#press_right_mouse_buttonObject

Right mouse press



39
40
41
42
# File 'lib/watir_robot/keywords/native.rb', line 39

def press_right_mouse_button
  @robot ||= Robot.new
  @robot.mousePress(InputEvent::BUTTON2_MASK)
end

#release_left_mouse_buttonObject

Left mouse release



47
48
49
50
# File 'lib/watir_robot/keywords/native.rb', line 47

def release_left_mouse_button
  @robot ||= Robot.new
  @robot.mouseRelease(InputEvent::BUTTON1_MASK)
end

#release_right_mouse_buttonObject

Right mouse release



55
56
57
58
# File 'lib/watir_robot/keywords/native.rb', line 55

def release_right_mouse_button
  @robot ||= Robot.new
  @robot.mouseRelease(InputEvent::BUTTON2_MASK)
end