lib/capybara_monkey.rb in superbara-0.4.1 vs lib/capybara_monkey.rb in superbara-0.5.0

- old
+ new

@@ -1,49 +1,140 @@ -module Capybara - module Node - class Element - def highlight - for color in ["DeepPink", "GreenYellow", "Aqua", "Red", "Yellow"] - Superbara::Helpers.highlight_element(self, {color: color}) - sleep 0.1 +module Superbara + module CapybaraMonkey + module Node + class Element + module Includes + def type(*text) + self.native.type(*text) + end + + def show(styles: nil, remove_highlight: 0.1) + execute_script "scrollTo(#{self.native.location.x}, #{self.native.location.y-200})" + styles = [ + { + "border" => "20px dashed Aqua" + } + ] unless styles + + for style in styles + Superbara::Helpers.highlight_element(self, style, remove_highlight) + if styles.size > 1 + sleep 0.05 + end + end + + self + end + end #Includes + + module Prepends + def click(*keys, **offset) + if Superbara.visual? + sleep 0.1 + self.show(styles: [{"border" => "10px dashed GreenYellow"}], remove_highlight: 0.1) + end + Superbara.output "click #{self.tag_name} '#{self.text}'" + super *keys, **offset + end end - "did you see it?" - end - end - end + end #Element + end #Node - class Result - def click_random(highlight: false, once: true) - unless self.any? - Superbara.puts "no elements in collection" - return false - end + class Result + module Includes + def second + self[1] + end + def third + self[2] + end + def fourth + self[3] + end + def fifth + self[4] + end + # active support stops herew + def sixth + self[5] + end + def seventh + self[6] + end + def eight + self[7] + end + def ninth + self[8] + end + def tenth + self[9] + end + # also + def random + self[rand(self.size)] + end + def middle + self[self.size/2] + end - index = if once - @__superbara_clicked ||= [] - if @__superbara_clicked.size == self.size - Superbara.puts "no unclicked elements left, not clicking anything" - return false + def show(styles=nil) + for e in self + e.show styles + end + self end - selected_index = nil - possible_index = nil - loop do - possible_index = rand(self.size) - next if @__superbara_clicked.include? possible_index - selected_index = possible_index - @__superbara_clicked << selected_index - break + def click_random(show: false, once: true) + unless self.any? + return false + end + + index = if once + @__superbara_clicked ||= [] + if @__superbara_clicked.size == self.size + return false + end + + selected_index = nil + possible_index = nil + loop do + possible_index = rand(self.size) + next if @__superbara_clicked.include? possible_index + selected_index = possible_index + @__superbara_clicked << selected_index + break + end + + selected_index + else + rand(self.size) + end + + element = self[index] + element.show if show + element.click end + end #Includes + end #Result + end #Capybara +end #Superbara - selected_index - else - rand(self.size) - end +module Capybara + module DSL + def self.included(base) + #warn "including Capybara::DSL in the global scope is not recommended!" if base == Object + #super + end + end - element = self[index] - element.highlight if highlight - Superbara.puts "clicking index: #{index} out of #{self.size}" - element.click + module Node + class Element + include Superbara::CapybaraMonkey::Node::Element::Includes + prepend Superbara::CapybaraMonkey::Node::Element::Prepends end + end + + class Result + include Superbara::CapybaraMonkey::Result::Includes end end