Class: Rudra
- Inherits:
-
Object
- Object
- Rudra
- Defined in:
- lib/rudra.rb
Overview
Selenium IDE-like WebDriver based upon Ruby binding
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
-
#auth_password ⇒ String
readonly
password for Basic Access Authentication Extension (Chrome only).
-
#auth_username ⇒ String
readonly
username for Basic Access Authentication Extension (Chrome only).
-
#browser ⇒ Symbol
readonly
The chosen browser.
-
#driver ⇒ Selenium::WebDriver::Driver
readonly
The driver instance of the chosen browser.
-
#headless ⇒ Boolean
readonly
Headless mode for Google Chrome.
-
#install_dir ⇒ String
readonly
The install directory of WebDrivers.
-
#locale ⇒ String
readonly
The browser locale.
-
#log_prefix ⇒ String
readonly
Prefix for logging descriptions and methods.
-
#screen_dir ⇒ String
readonly
The screenshot directory of save_screenshot.
-
#silent ⇒ Boolean
readonly
Turn off Turn on/off descriptions.
-
#timeout ⇒ Integer
readonly
The driver timeout.
-
#verbose ⇒ Boolean
readonly
Turn on/off Verbose mode.
-
#window_size ⇒ String
readonly
Chrome window size when headless.
Instance Method Summary collapse
-
#action ⇒ Selenium::WebDriver::ActionBuilder
Initialize ActionBuilder.
-
#active_element ⇒ Selenium::WebDriver::Element
Get the active element.
-
#add_cookie(opts = {}) ⇒ Object
Add a cookie to the browser.
-
#alert_accept ⇒ Object
Accept an alert.
-
#alert_dismiss ⇒ Object
Dismiss an alert.
-
#alert_send_keys(keys) ⇒ Object
Send keys to an alert.
-
#attribute(locator, attribute) ⇒ String?
Get the value of the given attribute of the element, identified by locator.
-
#attribute?(locator, attribute) ⇒ Boolean
If the element, identified by locator, has the given attribute.
-
#back ⇒ Object
Move back a single entry in the browser’s history.
-
#blank ⇒ Object
Open a blank page.
-
#blur(locator) ⇒ Object
Blur the given element, identfied by locator.
-
#clear(locator) ⇒ Object
Clear the input of the given element, identified by locator.
-
#clear_drawings ⇒ Object
Clear all drawing.
-
#click(locator) ⇒ Object
Click the given element, identified by locator.
-
#click_at(locator, offset = {}) ⇒ Object
Click the given element, identified by locator, with an offset.
-
#close ⇒ Object
Close the current window, or the browser if no windows are left.
-
#cookie_named(name) ⇒ Hash?
Get the cookie with the given name.
-
#css_value(locator, property) ⇒ Object
Get the value of the given CSS property of the given element.
-
#current_url ⇒ String
Get the URL of the current page.
-
#delete_all_cookies ⇒ Object
Delete all cookies.
-
#delete_cookie(name) ⇒ Object
Delete the cookie with the given name.
-
#displayed?(locator) ⇒ Boolean
If the given element, identified by locator, is displayed.
-
#double_click(locator, offset = {}) ⇒ Object
Double-click the given element, identified by locator, with an offset.
-
#drag_and_drop(from_locator, to_locator) ⇒ Object
Drag and drop.
-
#drag_and_drop_by(source, offset = {}) ⇒ Object
Drag and drop to an offset.
-
#draw_arrow(from_locator, to_locator) ⇒ Selenium::WebDriver::Element
Draw an arrow from an element to an element2.
-
#draw_color_fill(locator, color = 'rgba(255,0,0,0.8)') ⇒ Selenium::WebDriver::Element
Draw color fill on the given element, identfied by locator.
-
#draw_flyover(locator, options = {}) ⇒ Selenium::WebDriver::Element
Draw tooltip of the given element, identfied by locator.
-
#draw_redmark(locator, padding = {}) ⇒ Selenium::WebDriver::Element
Draw redmark around the given element, identfied by locator.
-
#draw_select(locator, options = {}) ⇒ Selenium::WebDriver::Element
Draw dropdown menu on the given SELECT element, identfied by locator.
-
#draw_text(locator, text, options = {}) ⇒ Selenium::WebDriver::Element
Draw text on top of the given element, identfied by locator.
-
#element_found?(locator, seconds = 1) ⇒ Boolean
Check if an element is found.
-
#enabled?(locator) ⇒ Boolean
If the given element, identified by locator, is enabled.
-
#execute_script(script, *args) ⇒ Selenium::WebDriver::Element, ...
Execute the given JavaScript.
-
#find_element(locator) ⇒ Selenium::WebDriver::Element
Find the first element matching the given locator.
-
#find_elements(locator) ⇒ Array<Selenium::WebDriver::Element>
Find all elements matching the given locator.
-
#focus(locator) ⇒ Object
Focus the given element, identfied by locator.
-
#forward ⇒ Object
Move forward a single entry in the browser’s history.
-
#full_screen ⇒ Object
Make the current window full screen.
-
#hide(locator) ⇒ Object
Hide the given element, identfied by locator.
-
#highlight(locator) ⇒ Object
Hightlight the given element, identfied by locator.
-
#implicit_wait(seconds) ⇒ Object
Set implicit_wait timeout.
-
#initialize(options = {}) ⇒ Rudra
constructor
Initialize an instance of Rudra.
-
#js_click(locator) ⇒ Object
Click the given element, identified by locator, via Javascript.
-
#location(locator) ⇒ Selenium::WebDriver::Point
Get the location of the given element, identfied by locator.
-
#maximize ⇒ Object
Maximize the current window.
-
#maximize_to_screen ⇒ Object
Maximize the current window to the size of the screen.
-
#minimize ⇒ Object
Minimize the current window.
-
#mkdir(dir) ⇒ Object
Create directories, recursively, for the given dir.
-
#move_by(right_by = 0, down_by = 0) ⇒ Object
Moves the mouse from its current position (or 0,0) by the given offset.
-
#move_to(locator, offset = {}) ⇒ Object
Move to the given element, identified by locator, with an offset.
-
#move_window_to(point_x, point_y) ⇒ Object
Move the current window to the given position.
-
#new_tab ⇒ String
Open a new tab.
-
#new_window(name) ⇒ Object
Open a new window.
-
#open(url) ⇒ Object
Open the specified URL in the browser.
-
#page_load(seconds) ⇒ Object
Set page_load timeout.
-
#page_source ⇒ String
Get the source of the current page.
-
#puts(description) ⇒ Object
Print description in the console.
-
#quit ⇒ Object
Quit the browser.
-
#rect(locator) ⇒ Selenium::WebDriver::Rectangle
Get the dimensions and coordinates of the given element, identfied by locator.
-
#refresh ⇒ Object
Refresh the current pagef.
-
#remove_attribute(locator, attribute) ⇒ Object
Remove the given attribute from the given element, identfied by locator.
-
#resize_window_to(width, height) ⇒ Object
Resize the current window to the given dimension.
-
#right_click(locator, offset = {}) ⇒ Object
Right-click the given element, identified by locator, with an offset.
-
#save_screenshot(filename) ⇒ Object
Save a PNG screenshot to file.
-
#script_timeout(seconds) ⇒ Object
Set script_timeout timeout.
-
#scroll_into_view(locator, align_to = true) ⇒ Object
Scroll the given element, identfied by locator, into view.
-
#select(option_locator) ⇒ Object
Select the given option, identified by locator.
-
#selected?(locator) ⇒ Boolean
If the given element, identified by locator, is selected.
-
#send_keys(locator, *args) ⇒ Object
Send keystrokes to the given element, identfied by locator.
-
#set_attribute(locator, attribute, value) ⇒ Object
Set the attribute’s value of the given element, identfied by locator.
-
#show(locator) ⇒ Object
Show the given element, identfied by locator.
-
#size(locator) ⇒ Selenium::WebDriver::Dimension
Get the size of the given element, identfied by locator.
-
#submit(locator) ⇒ Object
Submit the given element, identfied by locator.
-
#switch_to_alert ⇒ Object
Switch to the currently active modal dialog.
-
#switch_to_default_content ⇒ Object
Select either the first frame on the page, or the main document when a page contains iframes.
-
#switch_to_frame(id) ⇒ Object
Switch to the frame with the given id.
-
#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.
-
#switch_to_parent_frame ⇒ Object
Switch to the parent frame.
-
#switch_to_window(id) ⇒ Object
Switch to the given window handle.
-
#tag_name(locator) ⇒ String
Get the tag name of the given element, identfied by locator.
-
#text(locator) ⇒ String
Get the text content of the given element, identfied by locator.
-
#title ⇒ String
Get the title of the current page.
-
#trigger(locator, event) ⇒ Object
Trigger the given event on the given element, identfied by locator.
-
#wait_for(seconds = timeout) ⇒ Object
Wait until the given block returns a true value.
-
#wait_for_attribute_to_include(locator, attribute, value) ⇒ Object
Wait until the element, identified by locator, attribute has value.
-
#wait_for_enabled(locator) ⇒ Object
Wait until the element, identified by locator, is enabled.
-
#wait_for_not_visible(locator, seconds = 3) ⇒ Object
Wait (in seconds) until the element is not displayed.
-
#wait_for_text_to_exclude(locator, string) ⇒ Object
Wait until the element, identified by locator, excluding string in text.
-
#wait_for_text_to_include(locator, string) ⇒ Object
Wait until the element, identified by locator, including string in text.
-
#wait_for_title(string) ⇒ Object
Wait until the title of the page including the given string.
-
#wait_for_url(url) ⇒ Object
Wait until the URL of the page including the given url.
-
#wait_for_visible(locator) ⇒ Object
Wait until the element, identified by locator, is visible.
-
#window_handle ⇒ String
Get the current window handle.
-
#window_handles ⇒ Array<String>
Get the window handles of open browser windows.
-
#zoom(scale) ⇒ Object
Zoom the current page.
Constructor Details
#initialize(options = {}) ⇒ Rudra
Initialize an instance of Rudra
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( = {}) self.browser = .fetch(:browser, :chrome) self.install_dir = .fetch(:install_dir, './webdrivers/') self.locale = .fetch(:locale, :en) self.headless = .fetch(:headless, false) self.window_size = .fetch(:window_size, '1280,720') self.screen_dir = .fetch(:screen_dir, './screens/') self.log_prefix = .fetch(:log_prefix, ' - ') self.verbose = .fetch(:verbose, false) self.silent = .fetch(:silent, false) self.auth_username = .fetch(:auth_username, '') self.auth_password = .fetch(:auth_password, '') self.main_label = caller_locations(2, 1).first.label initialize_driver self.timeout = .fetch(:timeout, 30) end |
Instance Attribute Details
#auth_password ⇒ String
password for Basic Access Authentication Extension (Chrome only)
24 25 26 |
# File 'lib/rudra.rb', line 24 def auth_password @auth_password end |
#auth_username ⇒ String
username for Basic Access Authentication Extension (Chrome only)
24 25 26 |
# File 'lib/rudra.rb', line 24 def auth_username @auth_username end |
#browser ⇒ Symbol
The chosen browser
24 25 26 |
# File 'lib/rudra.rb', line 24 def browser @browser end |
#driver ⇒ Selenium::WebDriver::Driver (readonly)
The driver instance of the chosen browser
24 25 26 |
# File 'lib/rudra.rb', line 24 def driver @driver end |
#headless ⇒ Boolean
Headless mode for Google Chrome
24 25 26 |
# File 'lib/rudra.rb', line 24 def headless @headless end |
#install_dir ⇒ String
The install directory of WebDrivers
24 25 26 |
# File 'lib/rudra.rb', line 24 def install_dir @install_dir end |
#locale ⇒ String
The browser locale
24 25 26 |
# File 'lib/rudra.rb', line 24 def locale @locale end |
#log_prefix ⇒ String
Prefix for logging descriptions and methods
24 25 26 |
# File 'lib/rudra.rb', line 24 def log_prefix @log_prefix end |
#screen_dir ⇒ String
The screenshot directory of save_screenshot
24 25 26 |
# File 'lib/rudra.rb', line 24 def screen_dir @screen_dir end |
#silent ⇒ Boolean
Turn off Turn on/off descriptions
24 25 26 |
# File 'lib/rudra.rb', line 24 def silent @silent end |
#timeout ⇒ Integer
The driver timeout
24 25 26 |
# File 'lib/rudra.rb', line 24 def timeout @timeout end |
#verbose ⇒ Boolean
Turn on/off Verbose mode
24 25 26 |
# File 'lib/rudra.rb', line 24 def verbose @verbose end |
#window_size ⇒ String
Chrome window size when headless
24 25 26 |
# File 'lib/rudra.rb', line 24 def window_size @window_size end |
Instance Method Details
#action ⇒ Selenium::WebDriver::ActionBuilder
Initialize ActionBuilder
88 89 90 |
# File 'lib/rudra.rb', line 88 def action driver.action end |
#active_element ⇒ Selenium::WebDriver::Element
Get the active element
94 95 96 |
# File 'lib/rudra.rb', line 94 def active_element driver.switch_to.active_element end |
#add_cookie(opts = {}) ⇒ Object
Add a cookie to the browser
105 106 107 |
# File 'lib/rudra.rb', line 105 def (opts = {}) driver.manage.(opts) end |
#alert_accept ⇒ Object
Accept an alert
110 111 112 |
# File 'lib/rudra.rb', line 110 def alert_accept switch_to_alert.accept end |
#alert_dismiss ⇒ Object
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
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
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
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 |
#back ⇒ Object
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 |
#blank ⇒ Object
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
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
506 507 508 |
# File 'lib/rudra.rb', line 506 def clear(locator) find_element(locator).clear end |
#clear_drawings ⇒ Object
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
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
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 |
#close ⇒ Object
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 |
#cookie_named(name) ⇒ Hash?
Get the cookie with the given name
143 144 145 |
# File 'lib/rudra.rb', line 143 def (name) driver.manage.(name) end |
#css_value(locator, property) ⇒ Object
Get the value of the given CSS property of the given element
546 547 548 |
# File 'lib/rudra.rb', line 546 def css_value(locator, property) find_element(locator).css_value(property) end |
#current_url ⇒ String
Get the URL of the current page
149 150 151 |
# File 'lib/rudra.rb', line 149 def current_url driver.current_url end |
#delete_all_cookies ⇒ Object
Delete all cookies
154 155 156 |
# File 'lib/rudra.rb', line 154 def driver.manage. end |
#delete_cookie(name) ⇒ Object
Delete the cookie with the given name
160 161 162 |
# File 'lib/rudra.rb', line 160 def (name) driver.manage.(name) end |
#displayed?(locator) ⇒ Boolean
If the given element, identified by locator, is displayed
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
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
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
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
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
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
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, = {}) attribute_name = .fetch(:attribute, 'title') offset_x = .fetch(:offset_x, 5) offset_y = .fetch(:offset_y, 15) from_last_pos = .fetch(:from_last_pos, false) draw_symbol = .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
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
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, = {}) offset_x = .fetch(:offset_x, 0) offset_y = .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
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, = {}) color = .fetch(:color, '#f00') font_size = .fetch(:font_size, 13) top = .fetch(:top, 2) right = .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
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
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
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
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
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
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 |
#forward ⇒ Object
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_screen ⇒ Object
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
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
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
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
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
653 654 655 |
# File 'lib/rudra.rb', line 653 def location(locator) find_element(locator).location end |
#maximize ⇒ Object
Maximize the current window
247 248 249 |
# File 'lib/rudra.rb', line 247 def maximize driver.manage.window.maximize unless headless end |
#maximize_to_screen ⇒ Object
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 |
#minimize ⇒ Object
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
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
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
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
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_tab ⇒ String
Open a new tab
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
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
296 297 298 |
# File 'lib/rudra.rb', line 296 def open(url) driver.get(url) end |
#page_load(seconds) ⇒ Object
Set page_load timeout
683 684 685 |
# File 'lib/rudra.rb', line 683 def page_load(seconds) driver.manage.timeouts.page_load = seconds end |
#page_source ⇒ String
Get 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
308 309 310 |
# File 'lib/rudra.rb', line 308 def puts(description) $stdout.puts "#{log_prefix}#{description.chomp}" unless silent end |
#quit ⇒ Object
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
692 693 694 |
# File 'lib/rudra.rb', line 692 def rect(locator) find_element(locator).rect end |
#refresh ⇒ Object
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
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
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
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
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
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
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
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
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
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
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
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
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
801 802 803 |
# File 'lib/rudra.rb', line 801 def submit(locator) find_element(locator).submit end |
#switch_to_alert ⇒ Object
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_content ⇒ Object
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
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
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_frame ⇒ Object
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
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
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
817 818 819 |
# File 'lib/rudra.rb', line 817 def text(locator) find_element(locator).text end |
#title ⇒ String
Get 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
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
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
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
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
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
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
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
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
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
446 447 448 |
# File 'lib/rudra.rb', line 446 def wait_for_visible(locator) wait_for { find_element(locator).displayed? } end |
#window_handle ⇒ String
Get the current window handle
452 453 454 |
# File 'lib/rudra.rb', line 452 def window_handle driver.window_handle end |
#window_handles ⇒ Array<String>
Get the window handles of open browser windows
458 459 460 |
# File 'lib/rudra.rb', line 458 def window_handles driver.window_handles end |
#zoom(scale) ⇒ Object
Zoom the current page
464 465 466 |
# File 'lib/rudra.rb', line 464 def zoom(scale) execute_script(%(document.body.style.zoom = arguments[0];), scale) end |