Class: Rudra

Inherits:
Object
  • Object
show all
Defined in:
lib/rudra.rb

Overview

Selenium IDE-like WebDriver based upon Ruby binding

Author:

  • Aaron Chen

Constant Summary collapse

BROWSERS =

Supported Browsers

%i[chrome firefox ie safari].freeze
HOWS =

Element Finder Methods

%i[
  class class_name css id link link_text
  name partial_link_text tag_name xpath
].freeze
ATTRIBUTES =

Attributes

%i[
  browser driver install_dir locale
  headless window_size screen_dir
  log_prefix timeout verbose silent
  auth_username auth_password
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Rudra

Initialize an instance of Rudra

Parameters:

  • options (Hash) (defaults to: {})

    the options to initialize Rudra

Options Hash (options):

  • :browser (Symbol) — default: :chrome

    the supported browsers: :chrome, :firefox, :safari

  • :install_dir (String) — default: './webdrivers/'

    the install directory of WebDrivers

  • :locale (Symbol) — default: :en

    the browser locale

  • :headless (Boolean) — default: false

    headless mode

  • :window_size (String) — default: '1280,720'

    window size when headless

  • :screen_dir (String) — default: './screens/'

    the location of screenshots

  • :log_prefix (String) — default: ' - '

    prefix for logging descriptions and methods

  • :timeout (Integer) — default: 30

    implicit_wait timeout

  • :verbose (Boolean) — default: false

    Turn on/off verbose mode

  • :silent (Boolean) — default: false

    Turn on/off descriptions

  • :auth_username (String) — default: ''

    username for Basic Access Authentication extension

  • :auth_password (String) — default: ''

    password for Basic Access Authentication extension



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rudra.rb', line 63

def initialize(options = {})
  self.browser = options.fetch(:browser, :chrome)
  self.install_dir = options.fetch(:install_dir, './webdrivers/')
  self.locale = options.fetch(:locale, :en)
  self.headless = options.fetch(:headless, false)
  self.window_size = options.fetch(:window_size, '1280,720')
  self.screen_dir = options.fetch(:screen_dir, './screens/')
  self.log_prefix = options.fetch(:log_prefix, ' - ')
  self.verbose = options.fetch(:verbose, false)
  self.silent = options.fetch(:silent, false)
  self.auth_username = options.fetch(:auth_username, '')
  self.auth_password = options.fetch(:auth_password, '')
  self.main_label = caller_locations(2, 1).first.label

  initialize_driver

  self.timeout = options.fetch(:timeout, 30)
end

Instance Attribute Details

#auth_passwordString

password for Basic Access Authentication Extension (Chrome only)

Returns:

  • (String)

    the current value of auth_password



24
25
26
# File 'lib/rudra.rb', line 24

def auth_password
  @auth_password
end

#auth_usernameString

username for Basic Access Authentication Extension (Chrome only)

Returns:

  • (String)

    the current value of auth_username



24
25
26
# File 'lib/rudra.rb', line 24

def auth_username
  @auth_username
end

#browserSymbol

The chosen browser

Returns:

  • (Symbol)

    the current value of browser



24
25
26
# File 'lib/rudra.rb', line 24

def browser
  @browser
end

#driverSelenium::WebDriver::Driver (readonly)

The driver instance of the chosen browser

Returns:

  • (Selenium::WebDriver::Driver)

    the current value of driver



24
25
26
# File 'lib/rudra.rb', line 24

def driver
  @driver
end

#headlessBoolean

Headless mode for Google Chrome

Returns:

  • (Boolean)

    the current value of headless



24
25
26
# File 'lib/rudra.rb', line 24

def headless
  @headless
end

#install_dirString

The install directory of WebDrivers

Returns:

  • (String)

    the current value of install_dir



24
25
26
# File 'lib/rudra.rb', line 24

def install_dir
  @install_dir
end

#localeString

The browser locale

Returns:

  • (String)

    the current value of locale



24
25
26
# File 'lib/rudra.rb', line 24

def locale
  @locale
end

#log_prefixString

Prefix for logging descriptions and methods

Returns:

  • (String)

    the current value of log_prefix



24
25
26
# File 'lib/rudra.rb', line 24

def log_prefix
  @log_prefix
end

#screen_dirString

The screenshot directory of save_screenshot

Returns:

  • (String)

    the current value of screen_dir



24
25
26
# File 'lib/rudra.rb', line 24

def screen_dir
  @screen_dir
end

#silentBoolean

Turn off Turn on/off descriptions

Returns:

  • (Boolean)

    the current value of silent



24
25
26
# File 'lib/rudra.rb', line 24

def silent
  @silent
end

#timeoutInteger

The driver timeout

Returns:

  • (Integer)

    the current value of timeout



24
25
26
# File 'lib/rudra.rb', line 24

def timeout
  @timeout
end

#verboseBoolean

Turn on/off Verbose mode

Returns:

  • (Boolean)

    the current value of verbose



24
25
26
# File 'lib/rudra.rb', line 24

def verbose
  @verbose
end

#window_sizeString

Chrome window size when headless

Returns:

  • (String)

    the current value of window_size



24
25
26
# File 'lib/rudra.rb', line 24

def window_size
  @window_size
end

Instance Method Details

#actionSelenium::WebDriver::ActionBuilder

Initialize ActionBuilder

Returns:

  • (Selenium::WebDriver::ActionBuilder)

    ActionBuilder



88
89
90
# File 'lib/rudra.rb', line 88

def action
  driver.action
end

#active_elementSelenium::WebDriver::Element

Get the active element

Returns:

  • (Selenium::WebDriver::Element)

    the active element



94
95
96
# File 'lib/rudra.rb', line 94

def active_element
  driver.switch_to.active_element
end

Add a cookie to the browser

Parameters:

  • opts (Hash) (defaults to: {})

    the options to create a cookie with

Options Hash (opts):

  • :name (String)

    a name

  • :value (String)

    a value

  • :path (String) — default: '/'

    a path

  • :secure (Boolean) — default: false

    a boolean

  • :expires (Time, DateTime, Numeric, nil) — default: nil

    expiry date



105
106
107
# File 'lib/rudra.rb', line 105

def add_cookie(opts = {})
  driver.manage.add_cookie(opts)
end

#alert_acceptObject

Accept an alert



110
111
112
# File 'lib/rudra.rb', line 110

def alert_accept
  switch_to_alert.accept
end

#alert_dismissObject

Dismiss an alert



115
116
117
# File 'lib/rudra.rb', line 115

def alert_dismiss
  switch_to_alert.dismiss
end

#alert_send_keys(keys) ⇒ Object

Send keys to an alert

Parameters:

  • keys (String)

    keystrokes to send



121
122
123
# File 'lib/rudra.rb', line 121

def alert_send_keys(keys)
  switch_to_alert.send_keys(keys)
end

#attribute(locator, attribute) ⇒ String?

Get the value of the given attribute of the element, identified by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

  • attribute (String)

    the name of the attribute

Returns:

  • (String, nil)

    attribute value



478
479
480
# File 'lib/rudra.rb', line 478

def attribute(locator, attribute)
  find_element(locator).attribute(attribute)
end

#attribute?(locator, attribute) ⇒ Boolean

If the element, identified by locator, has the given attribute

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

  • attribute (String)

    the name of the attribute

Returns:

  • (Boolean)

    the result of the existence of the given attribute



487
488
489
490
491
# File 'lib/rudra.rb', line 487

def attribute?(locator, attribute)
  execute_script(%(
    return arguments[0].hasAttribute(arguments[1]);
  ), find_element(locator), attribute)
end

#backObject

Move back a single entry in the browser’s history



126
127
128
# File 'lib/rudra.rb', line 126

def back
  driver.navigate.back
end

#blankObject

Open a blank page



131
132
133
# File 'lib/rudra.rb', line 131

def blank
  open('about:blank')
end

#blur(locator) ⇒ Object

Blur the given element, identfied by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element



496
497
498
499
500
501
# File 'lib/rudra.rb', line 496

def blur(locator)
  execute_script(
    'var element = arguments[0]; element.blur();',
    find_element(locator)
  )
end

#clear(locator) ⇒ Object

Clear the input of the given element, identified by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element



506
507
508
# File 'lib/rudra.rb', line 506

def clear(locator)
  find_element(locator).clear
end

#clear_drawingsObject

Clear all drawing



871
872
873
874
875
876
877
878
879
880
# File 'lib/rudra.rb', line 871

def clear_drawings
  execute_script(%(
    var elements = window.document.body.querySelectorAll('[id*="rudra_"]');
    for (var i = 0; i < elements.length; i++) {
      elements[i].remove();
    }
    window.rudraTooltipSymbol = 9311;
    window.rudraTooltipLastPos = { x: 0, y: 0 };
  ))
end

#click(locator) ⇒ Object

Click the given element, identified by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element



513
514
515
516
517
518
519
520
521
522
# File 'lib/rudra.rb', line 513

def click(locator)
  wait_for do
    begin
      element = find_element(locator)
      element.enabled? && element.click.nil?
    rescue Selenium::WebDriver::Error::ElementClickInterceptedError
      false
    end
  end
end

#click_at(locator, offset = {}) ⇒ Object

Click the given element, identified by locator, with an offset

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

  • offset (Hash) (defaults to: {})

    the offset coordinates

Options Hash (offset):

  • :x (Integer) — default: 0

    offset on x coordinate

  • :y (Integer) — default: 0

    offset on y coordinate



530
531
532
533
534
535
536
537
538
539
540
# File 'lib/rudra.rb', line 530

def click_at(locator, offset = {})
  x = offset.fetch(:x, 0)
  y = offset.fetch(:y, 0)

  element = find_element(locator)

  action
    .move_to(element, x, y)
    .click
    .perform
end

#closeObject

Close the current window, or the browser if no windows are left



136
137
138
# File 'lib/rudra.rb', line 136

def close
  driver.close
end

Get the cookie with the given name

Parameters:

  • name (String)

    the name of the cookie

Returns:

  • (Hash, nil)

    the cookie, or nil if it wasn’t found



143
144
145
# File 'lib/rudra.rb', line 143

def cookie_named(name)
  driver.manage.cookie_named(name)
end

#css_value(locator, property) ⇒ Object

Get the value of the given CSS property of the given element

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

  • property (String)

    the longhand name of the property



546
547
548
# File 'lib/rudra.rb', line 546

def css_value(locator, property)
  find_element(locator).css_value(property)
end

#current_urlString

Get the URL of the current page

Returns:

  • (String)

    the URL of the current page



149
150
151
# File 'lib/rudra.rb', line 149

def current_url
  driver.current_url
end

#delete_all_cookiesObject

Delete all cookies



154
155
156
# File 'lib/rudra.rb', line 154

def delete_all_cookies
  driver.manage.delete_all_cookies
end

Delete the cookie with the given name

Parameters:

  • name (String)

    the name of the cookie



160
161
162
# File 'lib/rudra.rb', line 160

def delete_cookie(name)
  driver.manage.delete_cookie(name)
end

#displayed?(locator) ⇒ Boolean

If the given element, identified by locator, is displayed

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

Returns:

  • (Boolean)


554
555
556
# File 'lib/rudra.rb', line 554

def displayed?(locator)
  find_element(locator).displayed?
end

#double_click(locator, offset = {}) ⇒ Object

Double-click the given element, identified by locator, with an offset

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

  • offset (Hash) (defaults to: {})

    the offset coordinates

Options Hash (offset):

  • :x (Integer) — default: 0

    offset on x coordinate

  • :y (Integer) — default: 0

    offset on y coordinate



564
565
566
567
568
569
570
571
572
573
574
# File 'lib/rudra.rb', line 564

def double_click(locator, offset = {})
  x = offset.fetch(:x, 0)
  y = offset.fetch(:y, 0)

  element = find_element(locator)

  action
    .move_to(element, x, y)
    .double_click
    .perform
end

#drag_and_drop(from_locator, to_locator) ⇒ Object

Drag and drop

Parameters:

  • from_locator (String)

    the locator to emulate button down at

  • to_locator (String)

    the locator to to move to and release the mouse at



580
581
582
583
584
585
# File 'lib/rudra.rb', line 580

def drag_and_drop(from_locator, to_locator)
  el1 = find_element(from_locator)
  el2 = find_element(to_locator)

  action.drag_and_drop(el1, el2).perform
end

#drag_and_drop_by(source, offset = {}) ⇒ Object

Drag and drop to an offset

Parameters:

  • source (String)

    the locator to emulate button down at

  • offset (Hash) (defaults to: {})

    the offset coordinates

Options Hash (offset):

  • :x (Integer) — default: 0

    offset on x coordinate

  • :y (Integer) — default: 0

    offset on y coordinate



592
593
594
595
596
597
598
# File 'lib/rudra.rb', line 592

def drag_and_drop_by(source, offset = {})
  element = find_element(source)
  x = offset.fetch(:x, 0)
  y = offset.fetch(:y, 0)

  action.drag_and_drop_by(element, x, y).perform
end

#draw_arrow(from_locator, to_locator) ⇒ Selenium::WebDriver::Element

Draw an arrow from an element to an element2

Parameters:

  • from_locator (String, Selenium::WebDriver::Element)

    the locator or Selenium::WebDriver::Element where the arrow starts

  • to_locator (String, Selenium::WebDriver::Element)

    the locator or Selenium::WebDriver::Element where the arrow ends

Returns:

  • (Selenium::WebDriver::Element)

    the arrow element



888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
# File 'lib/rudra.rb', line 888

def draw_arrow(from_locator, to_locator)
  id = random_id

  execute_script(%(
    var element1 = arguments[0];
    var element2 = arguments[1];
    var rect1 = element1.getBoundingClientRect();
    var rect2 = element2.getBoundingClientRect();
    var from = {y: rect1.top};
    var to = {y: rect2.top};
    if (rect1.left > rect2.left) {
      from.x = rect1.left; to.x = rect2.right;
    } else if (rect1.left < rect2.left) {
      from.x = rect1.right; to.x = rect2.left;
    } else {
      from.x = rect1.left; to.x = rect2.left;
    }
    // create canvas
    var canvas = document.createElement('canvas');
    canvas.id = "#{id}";
    canvas.style.left = "0px";
    canvas.style.top = "0px";
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    canvas.style.zIndex = '100000';
    canvas.style.position = "absolute";
    document.body.appendChild(canvas);
    var headlen = 10;
    var angle = Math.atan2(to.y - from.y, to.x - from.x);
    var ctx = canvas.getContext("2d");
    // line
    ctx.beginPath();
    ctx.moveTo(from.x, from.y);
    ctx.lineTo(to.x, to.y);
    ctx.lineWidth  = 3;
    ctx.strokeStyle = '#f00';
    ctx.stroke();
    // arrow
    ctx.beginPath();
    ctx.moveTo(to.x, to.y);
    ctx.lineTo(
      to.x - headlen * Math.cos(angle - Math.PI/7),
      to.y - headlen * Math.sin(angle - Math.PI/7)
    );
    ctx.lineTo(
      to.x - headlen * Math.cos(angle + Math.PI/7),
      to.y - headlen * Math.sin(angle + Math.PI/7)
    );
    ctx.lineTo(to.x, to.y);
    ctx.lineTo(
      to.x - headlen * Math.cos(angle - Math.PI/7),
      to.y - headlen * Math.sin(angle - Math.PI/7)
    );
    ctx.lineWidth  = 3;
    ctx.strokeStyle = '#f00';
    ctx.stroke();
    return;
  ), find_element(from_locator), find_element(to_locator))

  find_element("id=#{id}")
end

#draw_color_fill(locator, color = 'rgba(255,0,0,0.8)') ⇒ Selenium::WebDriver::Element

Draw color fill on the given element, identfied by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

  • color (String) (defaults to: 'rgba(255,0,0,0.8)')

    CSS style of backgroundColor

Returns:

  • (Selenium::WebDriver::Element)

    the color fill element



955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
# File 'lib/rudra.rb', line 955

def draw_color_fill(locator, color = 'rgba(255,0,0,0.8)')
  rectangle = rect(locator)
  id = random_id

  execute_script(%(
    var colorfill = window.document.createElement('div');
    colorfill.id = '#{id}';
    colorfill.style.backgroundColor = '#{color}';
    colorfill.style.border = 'none';
    colorfill.style.display = 'block';
    colorfill.style.height = #{rectangle.height} + 'px';
    colorfill.style.left = #{rectangle.x} + 'px';
    colorfill.style.margin = '0px';
    colorfill.style.padding = '0px';
    colorfill.style.position = 'absolute';
    colorfill.style.top = #{rectangle.y} + 'px';
    colorfill.style.width = #{rectangle.width} + 'px';
    colorfill.style.zIndex = '99999';
    window.document.body.appendChild(colorfill);
    return;
  ))

  find_element("id=#{id}")
end

#draw_flyover(locator, options = {}) ⇒ Selenium::WebDriver::Element

Draw tooltip of the given element, identfied by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

  • options (Hash) (defaults to: {})

    the options to create a tooltip

Options Hash (options):

  • :attribute (String) — default: title

    attribute to draw the flyover with

  • :offset_x (Integer) — default: 5

    offset on x coordinate

  • :offset_y (Integer) — default: 15

    offset on y coordinate

  • :from_last_pos (Boolean) — default: false

    if to draw from last position

  • :draw_symbol (Boolean) — default: false

    if to draw symbol

Returns:

  • (Selenium::WebDriver::Element)

    the tooltip element



992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
# File 'lib/rudra.rb', line 992

def draw_flyover(locator, options = {})
  attribute_name = options.fetch(:attribute, 'title')
  offset_x = options.fetch(:offset_x, 5)
  offset_y = options.fetch(:offset_y, 15)
  from_last_pos = options.fetch(:from_last_pos, false)
  draw_symbol = options.fetch(:draw_symbol, false)

  symbol_id = random_id
  tooltip_id = random_id

  execute_script(%(
    var element = arguments[0];
    if (! window.rudraTooltipSymbol) {
      window.rudraTooltipSymbol = 9311;
    }
    if (! window.rudraTooltipLastPos) {
      window.rudraTooltipLastPos = { x: 0, y: 0 };
    }
    var rect = element.getBoundingClientRect();
    var title = element.getAttribute("#{attribute_name}") || 'N/A';
    var left = window.scrollX + rect.left;
    var top = window.scrollY + rect.top;
    if (#{draw_symbol}) {
      window.rudraTooltipSymbol++;
      var symbol = document.createElement('div');
      symbol.id = "#{symbol_id}";
      symbol.textContent = String.fromCharCode(rudraTooltipSymbol);
      symbol.style.color = '#f00';
      symbol.style.display = 'block';
      symbol.style.fontSize = '12px';
      symbol.style.left = (left - 12) + 'px';
      symbol.style.position = 'absolute';
      symbol.style.top = top + 'px';
      symbol.style.zIndex = '99999';
      document.body.appendChild(symbol);
    }
    var tooltip = document.createElement('div');
    tooltip.id = "#{tooltip_id}";
    tooltip.textContent = (#{draw_symbol}) ?
      String.fromCharCode(rudraTooltipSymbol) + " " + title : title;
    tooltip.style.position = 'absolute';
    tooltip.style.color = '#000';
    tooltip.style.backgroundColor = '#F5FCDE';
    tooltip.style.border = '3px solid #f00';
    tooltip.style.fontSize = '12px';
    tooltip.style.zIndex = '99999';
    tooltip.style.display = 'block';
    tooltip.style.height = '16px';
    tooltip.style.padding = '2px';
    tooltip.style.verticalAlign = 'middle';
    tooltip.style.top = ((#{from_last_pos}) ?
      window.rudraTooltipLastPos.y : (top + #{offset_y})) + 'px';
    tooltip.style.left = ((#{from_last_pos}) ?
      window.rudraTooltipLastPos.x : (left + #{offset_x})) + 'px';
    document.body.appendChild(tooltip);
    if (tooltip.scrollHeight > tooltip.offsetHeight) {
    	tooltip.style.height = (tooltip.scrollHeight + 3) + 'px';
    }
    var lastPos = tooltip.getBoundingClientRect();
    window.rudraTooltipLastPos = {
      x: window.scrollX + lastPos.left, y: window.scrollY + lastPos.bottom
    };
    return;
  ), find_element(locator))

  find_element("id=#{tooltip_id}")
end

#draw_redmark(locator, padding = {}) ⇒ Selenium::WebDriver::Element

Draw redmark around the given element, identfied by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

  • padding (Hash) (defaults to: {})

    the padding of the given redmark

Options Hash (padding):

  • :top (Integer) — default: 5

    top padding

  • :right (Integer) — default: 5

    right padding

  • :bottom (Integer) — default: 5

    bottom padding

  • :left (Integer) — default: 5

    left padding

Returns:

  • (Selenium::WebDriver::Element)

    the redmark element



1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
# File 'lib/rudra.rb', line 1069

def draw_redmark(locator, padding = {})
  top = padding.fetch(:top, 5)
  right = padding.fetch(:right, 5)
  bottom = padding.fetch(:bottom, 5)
  left = padding.fetch(:left, 5)

  rectangle = rect(locator)
  id = random_id

  execute_script(%(
    var redmark = window.document.createElement('div');
    redmark.id = '#{id}';
    redmark.style.border = '3px solid red';
    redmark.style.display = 'block';
    redmark.style.height = (#{rectangle.height} + 8 + #{bottom}) + 'px';
    redmark.style.left = (#{rectangle.x} - 4 - #{left}) + 'px';
    redmark.style.margin = '0px';
    redmark.style.padding = '0px';
    redmark.style.position = 'absolute';
    redmark.style.top = (#{rectangle.y} - 4 - #{top}) + 'px';
    redmark.style.width = (#{rectangle.width} + 8 + #{right}) + 'px';
    redmark.style.zIndex = '99999';
    window.document.body.appendChild(redmark);
    return;
  ))

  find_element("id=#{id}")
end

#draw_select(locator, options = {}) ⇒ Selenium::WebDriver::Element

Draw dropdown menu on the given SELECT element, identfied by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

  • options (Hash) (defaults to: {})

    the options to create the dropdown menu

Options Hash (options):

  • :offset_x (Integer) — default: 0

    offset on x coordinate

  • :offset_y (Integer) — default: 0

    offset on y coordinate

Returns:

  • (Selenium::WebDriver::Element)

    the dropdown menu element



1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
# File 'lib/rudra.rb', line 1105

def draw_select(locator, options = {})
  offset_x = options.fetch(:offset_x, 0)
  offset_y = options.fetch(:offset_y, 0)

  id = random_id

  execute_script(%(
    var element = arguments[0];
    var rect = element.getBoundingClientRect();
    var x = rect.left;
    var y = rect.bottom;
    var width = element.offsetWidth;
    function escape(str) {
    	return str.replace(
        /[\\x26\\x0A<>'"]/g,
        function(r) { return "&#" + r.charCodeAt(0) + ";"; }
      );
    }
    var content = "";
    for (var i = 0; i < element.length; i++) {
    	if (!element.options[i].disabled) {
        content += escape(element.options[i].text) + "<br />";
      }
    }
    var dropdown = document.createElement('div');
    dropdown.id = "#{id}";
    dropdown.innerHTML = content;
    dropdown.style.backgroundColor = '#fff';
    dropdown.style.border = '1px solid #000';
    dropdown.style.color = '#000';
    dropdown.style.display = 'block';
    dropdown.style.fontSize = '12px';
    dropdown.style.height = '1px';
    dropdown.style.padding = '2px';
    dropdown.style.position = 'absolute';
    dropdown.style.width = width + 'px';
    dropdown.style.zIndex = '99999';
    document.body.appendChild(dropdown);
    dropdown.style.height = (dropdown.scrollHeight + 8) + 'px';
    if (dropdown.scrollWidth > width) {
    	dropdown.style.width = (dropdown.scrollWidth + 8) + 'px';
    }
    dropdown.style.left = (x + #{offset_x}) + "px";
    dropdown.style.top = (y + #{offset_y}) + "px";
    return;
  ), find_element(locator))

  find_element("id=#{id}")
end

#draw_text(locator, text, options = {}) ⇒ Selenium::WebDriver::Element

Draw text on top of the given element, identfied by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

  • text (String)

    the text to draw

  • options (Hash) (defaults to: {})

    the options to create the text

Options Hash (options):

  • :color (String) — default: '#f00'

    the color of the text

  • :font_size (Integer) — default: 13

    the font size of the text

  • :top (Integer) — default: 2

    CSS style of top

  • :right (Integer) — default: 20

    CSS style of right

Returns:

  • (Selenium::WebDriver::Element)

    the text element



1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
# File 'lib/rudra.rb', line 1165

def draw_text(locator, text, options = {})
  color = options.fetch(:color, '#f00')
  font_size = options.fetch(:font_size, 13)
  top = options.fetch(:top, 2)
  right = options.fetch(:right, 20)

  rect = rect(locator)
  id = random_id

  execute_script(%(
    var textbox = window.document.createElement('div');
    textbox.id = '#{id}';
    textbox.innerText = '#{text}';
    textbox.style.border = 'none';
    textbox.style.color = '#{color}';
    textbox.style.display = 'block';
    textbox.style.font = '#{font_size}px Verdana, sans-serif';
    textbox.style.left = #{rect.x} + 'px';
    textbox.style.margin = '0';
    textbox.style.padding = '0';
    textbox.style.position = 'absolute';
    textbox.style.right = #{right} + 'px';
    textbox.style.top = (#{rect.y} + #{rect.height} + #{top}) + 'px';
    textbox.style.zIndex = '99999';
    window.document.body.appendChild(textbox);
    return;
  ))

  find_element("id=#{id}")
end

#element_found?(locator, seconds = 1) ⇒ Boolean

Check if an element is found

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

  • seconds (Integer) (defaults to: 1)

    seconds before timed out

Returns:

  • (Boolean)


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/rudra.rb', line 168

def element_found?(locator, seconds = 1)
  how, what = parse_locator(locator)

  implicit_wait(seconds)

  begin
    wait_for(seconds) { driver.find_element(how, what).displayed? }
  rescue Selenium::WebDriver::Error::TimeoutError
    false
  rescue Net::ReadTimeout
    false
  ensure
    implicit_wait(timeout)
  end
end

#enabled?(locator) ⇒ Boolean

If the given element, identified by locator, is enabled

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

Returns:

  • (Boolean)


604
605
606
# File 'lib/rudra.rb', line 604

def enabled?(locator)
  find_element(locator).enabled?
end

#execute_script(script, *args) ⇒ Selenium::WebDriver::Element, ...

Execute the given JavaScript

Parameters:

  • script (String)

    JavaScript source to execute

  • args (Selenium::WebDriver::Element, Integer, Float, Boolean, NilClass, String, Array)

    arguments will be available in the given script in the ‘arguments’ pseudo-array

Returns:

  • (Selenium::WebDriver::Element, Integer, Float, Boolean, NilClass, String, Array)

    the value returned from the script



191
192
193
# File 'lib/rudra.rb', line 191

def execute_script(script, *args)
  driver.execute_script(script, *args)
end

#find_element(locator) ⇒ Selenium::WebDriver::Element

Find the first element matching the given locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

Returns:

  • (Selenium::WebDriver::Element)

    the element found

Raises:

  • (Selenium::WebDriver::Error::NoSuchElementError)


199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/rudra.rb', line 199

def find_element(locator)
  return locator if locator.is_a?(Selenium::WebDriver::Element)

  element = nil
  how, what = parse_locator(locator)

  if how == :css
    new_what, nth = what.scan(/(.*):eq\((\d+)\)/).flatten
    element = find_elements("#{how}=#{new_what}")[nth.to_i] if nth
  end

  element ||= driver.find_element(how, what)

  raise Selenium::WebDriver::Error::NoSuchElementError, "Failed to find element: #{locator}" unless element

  wait_for { element.displayed? }

  element
end

#find_elements(locator) ⇒ Array<Selenium::WebDriver::Element>

Find all elements matching the given locator

Parameters:

  • locator (String)

    the locator to identify the elements

Returns:

  • (Array<Selenium::WebDriver::Element>)

    the elements found

Raises:

  • (Selenium::WebDriver::Error::NoSuchElementError)


222
223
224
225
226
227
228
229
# File 'lib/rudra.rb', line 222

def find_elements(locator)
  how, what = parse_locator(locator)
  elements = driver.find_elements(how, what)

  raise Selenium::WebDriver::Error::NoSuchElementError, "Failed to find elements: #{locator}" if elements.empty?

  elements
end

#focus(locator) ⇒ Object

Focus the given element, identfied by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element



611
612
613
614
615
616
# File 'lib/rudra.rb', line 611

def focus(locator)
  execute_script(
    'var element = arguments[0]; element.focus();',
    find_element(locator)
  )
end

#forwardObject

Move forward a single entry in the browser’s history



232
233
234
# File 'lib/rudra.rb', line 232

def forward
  driver.navigate.forward
end

#full_screenObject

Make the current window full screen



237
238
239
# File 'lib/rudra.rb', line 237

def full_screen
  driver.manage.window.full_screen
end

#hide(locator) ⇒ Object

Hide the given element, identfied by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element



621
622
623
624
625
# File 'lib/rudra.rb', line 621

def hide(locator)
  execute_script(%(
    arguments[0].style.display = 'none';
  ), find_element(locator))
end

#highlight(locator) ⇒ Object

Hightlight the given element, identfied by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element



630
631
632
633
634
# File 'lib/rudra.rb', line 630

def highlight(locator)
  execute_script(%(
    arguments[0].style.backgroundColor = '#ff3';
  ), find_element(locator))
end

#implicit_wait(seconds) ⇒ Object

Set implicit_wait timeout

Parameters:

  • seconds (Integer)

    timeout for implicit_wait



638
639
640
# File 'lib/rudra.rb', line 638

def implicit_wait(seconds)
  driver.manage.timeouts.implicit_wait = seconds
end

#js_click(locator) ⇒ Object

Click the given element, identified by locator, via Javascript

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element



645
646
647
# File 'lib/rudra.rb', line 645

def js_click(locator)
  execute_script('arguments[0].click();', find_element(locator))
end

#location(locator) ⇒ Selenium::WebDriver::Point

Get the location of the given element, identfied by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

Returns:

  • (Selenium::WebDriver::Point)

    the point of the given element



653
654
655
# File 'lib/rudra.rb', line 653

def location(locator)
  find_element(locator).location
end

#maximizeObject

Maximize the current window



247
248
249
# File 'lib/rudra.rb', line 247

def maximize
  driver.manage.window.maximize unless headless
end

#maximize_to_screenObject

Maximize the current window to the size of the screen



252
253
254
255
256
257
258
259
# File 'lib/rudra.rb', line 252

def maximize_to_screen
  size = execute_script(%(
    return { width: window.screen.width, height: window.screen.height };
  ))

  move_window_to(0, 0)
  resize_window_to(size['width'], size['height'])
end

#minimizeObject

Minimize the current window



262
263
264
# File 'lib/rudra.rb', line 262

def minimize
  driver.manage.window.minimize
end

#mkdir(dir) ⇒ Object

Create directories, recursively, for the given dir

Parameters:

  • dir (String)

    the directories to create



1198
1199
1200
# File 'lib/rudra.rb', line 1198

def mkdir(dir)
  FileUtils.mkdir_p(dir)
end

#move_by(right_by = 0, down_by = 0) ⇒ Object

Moves the mouse from its current position (or 0,0) by the given offset

Parameters:

  • right_by (Integer) (defaults to: 0)

    horizontal offset

  • down_by (Integer) (defaults to: 0)

    vertical offset



660
661
662
# File 'lib/rudra.rb', line 660

def move_by(right_by = 0, down_by = 0)
  action.move_by(right_by, down_by).perform
end

#move_to(locator, offset = {}) ⇒ Object

Move to the given element, identified by locator, with an offset

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

  • offset (Hash) (defaults to: {})

    the offset coordinates

Options Hash (offset):

  • :x (Integer) — default: 0

    offset on x coordinate

  • :y (Integer) — default: 0

    offset on y coordinate



670
671
672
673
674
675
676
677
678
679
# File 'lib/rudra.rb', line 670

def move_to(locator, offset = {})
  x = offset.fetch(:x, 0)
  y = offset.fetch(:y, 0)

  element = find_element(locator)

  action
    .move_to(element, x, y)
    .perform
end

#move_window_to(point_x, point_y) ⇒ Object

Move the current window to the given position

Parameters:

  • point_x (Integer)

    the x coordinate

  • point_y (Integer)

    the y coordinate



269
270
271
# File 'lib/rudra.rb', line 269

def move_window_to(point_x, point_y)
  driver.manage.window.move_to(point_x, point_y)
end

#new_tabString

Open a new tab

Returns:

  • (String)

    the id of the new tab obtained from #window_handles



275
276
277
278
# File 'lib/rudra.rb', line 275

def new_tab
  execute_script('window.open();')
  window_handles.last
end

#new_window(name) ⇒ Object

Open a new window

Parameters:

  • name (String)

    the name of the window



282
283
284
285
286
287
288
289
290
291
292
# File 'lib/rudra.rb', line 282

def new_window(name)
  execute_script(%(
    var w = Math.max(
      document.documentElement.clientWidth, window.innerWidth || 0
    );
    var h = Math.max(
      document.documentElement.clientHeight, window.innerHeight || 0
    );
    window.open("about:blank", arguments[0], `width=${w},height=${h}`);
  ), name)
end

#open(url) ⇒ Object

Open the specified URL in the browser

Parameters:

  • url (String)

    the URL of the page to open



296
297
298
# File 'lib/rudra.rb', line 296

def open(url)
  driver.get(url)
end

#page_load(seconds) ⇒ Object

Set page_load timeout

Parameters:

  • seconds (Integer)

    timeout for page_load



683
684
685
# File 'lib/rudra.rb', line 683

def page_load(seconds)
  driver.manage.timeouts.page_load = seconds
end

#page_sourceString

Get the source of the current page

Returns:

  • (String)

    the source of the current page



302
303
304
# File 'lib/rudra.rb', line 302

def page_source
  driver.page_source
end

#puts(description) ⇒ Object

Print description in the console

Parameters:

  • description (String)

    description to show



308
309
310
# File 'lib/rudra.rb', line 308

def puts(description)
  $stdout.puts "#{log_prefix}#{description.chomp}" unless silent
end

#quitObject

Quit the browser



242
243
244
# File 'lib/rudra.rb', line 242

def quit
  driver.quit
end

#rect(locator) ⇒ Selenium::WebDriver::Rectangle

Get the dimensions and coordinates of the given element, identfied by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

Returns:

  • (Selenium::WebDriver::Rectangle)

    the retangle of the given element



692
693
694
# File 'lib/rudra.rb', line 692

def rect(locator)
  find_element(locator).rect
end

#refreshObject

Refresh the current pagef



313
314
315
# File 'lib/rudra.rb', line 313

def refresh
  driver.navigate.refresh
end

#remove_attribute(locator, attribute) ⇒ Object

Remove the given attribute from the given element, identfied by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

  • attribute (String)

    the name of the attribute



701
702
703
704
705
706
707
708
709
# File 'lib/rudra.rb', line 701

def remove_attribute(locator, attribute)
  execute_script(%(
    var element = arguments[0];
    var attributeName = arguments[1];
    if (element.hasAttribute(attributeName)) {
      element.removeAttribute(attributeName);
    }
  ), find_element(locator), attribute)
end

#resize_window_to(width, height) ⇒ Object

Resize the current window to the given dimension

Parameters:

  • width (Integer)

    the width of the window

  • height (Integer)

    the height of the window



320
321
322
# File 'lib/rudra.rb', line 320

def resize_window_to(width, height)
  driver.manage.window.resize_to(width, height)
end

#right_click(locator, offset = {}) ⇒ Object

Right-click the given element, identified by locator, with an offset

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

  • offset (Hash) (defaults to: {})

    the offset coordinates

Options Hash (offset):

  • :x (Integer) — default: 0

    offset on x coordinate

  • :y (Integer) — default: 0

    offset on y coordinate



717
718
719
720
721
722
723
# File 'lib/rudra.rb', line 717

def right_click(locator, offset = {})
  x = offset.fetch(:x, 0)
  y = offset.fetch(:y, 0)

  element = find_element(locator)
  action.move_to(element, x, y).context_click.perform
end

#save_screenshot(filename) ⇒ Object

Save a PNG screenshot to file

Parameters:

  • filename (String)

    the filename of PNG screenshot



326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/rudra.rb', line 326

def save_screenshot(filename)
  file = File.join(
    @screen_dir,
    sanitize(filename.end_with?('.png') ? filename : "#{filename}.png")
  )

  dir = File.dirname(file)

  mkdir(dir) unless Dir.exist?(dir)

  driver.save_screenshot(file)
end

#script_timeout(seconds) ⇒ Object

Set script_timeout timeout

Parameters:

  • seconds (Integer)

    timeout for script_timeout



727
728
729
# File 'lib/rudra.rb', line 727

def script_timeout(seconds)
  driver.manage.timeouts.script_timeout = seconds
end

#scroll_into_view(locator, align_to = true) ⇒ Object

Scroll the given element, identfied by locator, into view

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

  • align_to (Boolean) (defaults to: true)

    true if aligned on top or false if aligned at the bottom



736
737
738
739
740
741
742
# File 'lib/rudra.rb', line 736

def scroll_into_view(locator, align_to = true)
  execute_script(
    'arguments[0].scrollIntoView(arguments[1]);',
    find_element(locator),
    align_to
  )
end

#select(option_locator) ⇒ Object

Select the given option, identified by locator

Parameters:

  • option_locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element



747
748
749
# File 'lib/rudra.rb', line 747

def select(option_locator)
  find_element(option_locator).click
end

#selected?(locator) ⇒ Boolean

If the given element, identified by locator, is selected

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

Returns:

  • (Boolean)


755
756
757
# File 'lib/rudra.rb', line 755

def selected?(locator)
  find_element(locator).selected?
end

#send_keys(locator, *args) ⇒ Object

Send keystrokes to the given element, identfied by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

  • args (String, Symbol, Array)

    keystrokes to send



763
764
765
# File 'lib/rudra.rb', line 763

def send_keys(locator, *args)
  find_element(locator).send_keys(*args)
end

#set_attribute(locator, attribute, value) ⇒ Object

Set the attribute’s value of the given element, identfied by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

  • attribute (String)

    the name of the attribute

  • value (String)

    the value of the attribute



772
773
774
775
776
777
778
779
# File 'lib/rudra.rb', line 772

def set_attribute(locator, attribute, value)
  execute_script(%(
    var element = arguments[0];
    var attribute = arguments[1];
    var value = arguments[2];
    element.setAttribute(attribute, value);
  ), find_element(locator), attribute, value)
end

#show(locator) ⇒ Object

Show the given element, identfied by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element



784
785
786
787
788
# File 'lib/rudra.rb', line 784

def show(locator)
  execute_script(%(
    arguments[0].style.display = '';
  ), find_element(locator))
end

#size(locator) ⇒ Selenium::WebDriver::Dimension

Get the size of the given element, identfied by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

Returns:

  • (Selenium::WebDriver::Dimension)


794
795
796
# File 'lib/rudra.rb', line 794

def size(locator)
  find_element(locator).size
end

#submit(locator) ⇒ Object

Submit the given element, identfied by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element



801
802
803
# File 'lib/rudra.rb', line 801

def submit(locator)
  find_element(locator).submit
end

#switch_to_alertObject

Switch to the currently active modal dialog



340
341
342
# File 'lib/rudra.rb', line 340

def switch_to_alert
  driver.switch_to.alert
end

#switch_to_default_contentObject

Select either the first frame on the page, or the main document when a page contains iframes



346
347
348
# File 'lib/rudra.rb', line 346

def switch_to_default_content
  driver.switch_to.default_content
end

#switch_to_frame(id) ⇒ Object

Switch to the frame with the given id

Parameters:

  • id (String)

    the frame id



352
353
354
# File 'lib/rudra.rb', line 352

def switch_to_frame(id)
  driver.switch_to.frame(id)
end

#switch_to_frame_and_wait_for_element_found(frame_id, locator) ⇒ Object

Switch to a frame and wait until the element, identified by locator, is found

Parameters:

  • frame_id (String)

    the frame id

  • locator (String)

    the locator to identify the element



390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/rudra.rb', line 390

def switch_to_frame_and_wait_for_element_found(frame_id, locator)
  switch_to_frame frame_id

  how, what = parse_locator(locator)

  wait_for do
    begin
      driver.find_element(how, what)
    rescue Selenium::WebDriver::Error::NoSuchWindowError
      false
    end
  end
end

#switch_to_parent_frameObject

Switch to the parent frame



357
358
359
# File 'lib/rudra.rb', line 357

def switch_to_parent_frame
  driver.switch_to.parent_frame
end

#switch_to_window(id) ⇒ Object

Switch to the given window handle

Parameters:

  • id (String)

    the window handle obtained through #window_handles



363
364
365
# File 'lib/rudra.rb', line 363

def switch_to_window(id)
  driver.switch_to.window(id)
end

#tag_name(locator) ⇒ String

Get the tag name of the given element, identfied by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

Returns:

  • (String)

    the tag name of the given element



809
810
811
# File 'lib/rudra.rb', line 809

def tag_name(locator)
  find_element(locator).tag_name
end

#text(locator) ⇒ String

Get the text content of the given element, identfied by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

Returns:

  • (String)

    the text content of the given element



817
818
819
# File 'lib/rudra.rb', line 817

def text(locator)
  find_element(locator).text
end

#titleString

Get the title of the current page

Returns:

  • (String)

    the title of the current page



369
370
371
# File 'lib/rudra.rb', line 369

def title
  driver.title
end

#trigger(locator, event) ⇒ Object

Trigger the given event on the given element, identfied by locator

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

  • event (String)

    the event name



825
826
827
828
829
830
831
832
# File 'lib/rudra.rb', line 825

def trigger(locator, event)
  execute_script(%(
    var element = arguments[0];
    var eventName = arguments[1];
    var event = new Event(eventName, {"bubbles": false, "cancelable": false});
    element.dispatchEvent(event);
  ), find_element(locator), event)
end

#wait_for(seconds = timeout) ⇒ Object

Wait until the given block returns a true value

Parameters:

  • seconds (Integer) (defaults to: timeout)

    seconds before timed out

Returns:

  • (Object)

    the result of the block



376
377
378
# File 'lib/rudra.rb', line 376

def wait_for(seconds = timeout)
  Selenium::WebDriver::Wait.new(timeout: seconds).until { yield }
end

#wait_for_attribute_to_include(locator, attribute, value) ⇒ Object

Wait until the element, identified by locator, attribute has value

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element

  • attribute (String)

    the name of the attribute

  • value (String)

    the value of the attribute



838
839
840
841
842
843
844
845
846
847
848
# File 'lib/rudra.rb', line 838

def wait_for_attribute_to_include(locator, attribute, value)
  how, what = parse_locator(locator)

  wait_for do
    begin
      driver.find_element(how, what)&.attribute(attribute)&.downcase&.include?(value.downcase)
    rescue Selenium::WebDriver::Error::StaleElementReferenceError
      false
    end
  end
end

#wait_for_enabled(locator) ⇒ Object

Wait until the element, identified by locator, is enabled

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element



383
384
385
# File 'lib/rudra.rb', line 383

def wait_for_enabled(locator)
  wait_for { find_element(locator).enabled? }
end

#wait_for_not_visible(locator, seconds = 3) ⇒ Object

Wait (in seconds) until the element is not displayed

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

  • seconds (Integer) (defaults to: 3)

    seconds before timed out



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/rudra.rb', line 408

def wait_for_not_visible(locator, seconds = 3)
  how, what = parse_locator(locator)

  implicit_wait(seconds)

  begin
    wait_for(seconds) do
      begin
        elements = driver.find_elements(how, what)
        elements.empty? || elements.map(&:displayed?).none?
      rescue Selenium::WebDriver::Error::StaleElementReferenceError
        false
      end
    end
  rescue Selenium::WebDriver::Error::TimeoutError
    true
  rescue Net::ReadTimeout
    true
  ensure
    implicit_wait(timeout)
  end
end

#wait_for_text_to_exclude(locator, string) ⇒ Object

Wait until the element, identified by locator, excluding string in text

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

  • string (String)

    the string to exclude



854
855
856
# File 'lib/rudra.rb', line 854

def wait_for_text_to_exclude(locator, string)
  wait_for { text(locator).exclude?(string) }
end

#wait_for_text_to_include(locator, string) ⇒ Object

Wait until the element, identified by locator, including string in text

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element

  • string (String)

    the string to compare



862
863
864
# File 'lib/rudra.rb', line 862

def wait_for_text_to_include(locator, string)
  wait_for { text(locator).include?(string) }
end

#wait_for_title(string) ⇒ Object

Wait until the title of the page including the given string

Parameters:

  • string (String)

    the string to compare



433
434
435
# File 'lib/rudra.rb', line 433

def wait_for_title(string)
  wait_for { title.downcase.include?(string.downcase) }
end

#wait_for_url(url) ⇒ Object

Wait until the URL of the page including the given url

Parameters:

  • url (String)

    the URL to compare



439
440
441
# File 'lib/rudra.rb', line 439

def wait_for_url(url)
  wait_for { current_url.include?(url) }
end

#wait_for_visible(locator) ⇒ Object

Wait until the element, identified by locator, is visible

Parameters:

  • locator (String, Selenium::WebDriver::Element)

    the locator to identify the element or Selenium::WebDriver::Element



446
447
448
# File 'lib/rudra.rb', line 446

def wait_for_visible(locator)
  wait_for { find_element(locator).displayed? }
end

#window_handleString

Get the current window handle

Returns:

  • (String)

    the id of the current window handle



452
453
454
# File 'lib/rudra.rb', line 452

def window_handle
  driver.window_handle
end

#window_handlesArray<String>

Get the window handles of open browser windows

Returns:

  • (Array<String>)

    the ids of window handles



458
459
460
# File 'lib/rudra.rb', line 458

def window_handles
  driver.window_handles
end

#zoom(scale) ⇒ Object

Zoom the current page

Parameters:

  • scale (Float)

    the scale of zoom



464
465
466
# File 'lib/rudra.rb', line 464

def zoom(scale)
  execute_script(%(document.body.style.zoom = arguments[0];), scale)
end