Module: Pyrite::Dsl
- Included in:
- PyriteTest
- Defined in:
- lib/pyrite/dsl.rb
Instance Method Summary collapse
-
#check(locator) ⇒ Object
Check a chek box or toggle a radio button.
-
#click(locator) ⇒ Object
Click anything else.
-
#code_me ⇒ Object
Write the HTML of a page to a file and open it.
-
#drag_and_drop(opts = {}) ⇒ Object
drag an jquery selector to the center pixel of another, e.g.
-
#fill_in(element, value) ⇒ Object
Fill in a form field.
-
#follow(text) ⇒ Object
Follow a link based on its text.
-
#get_confirmation ⇒ Object
Use this to consume JS alerts i.e.
-
#inside_iframe(frame) ⇒ Object
Excecute commands within an iframe, then switch back to the parent frame i.e.
-
#press(text) ⇒ Object
Press an submit (not button) input based on its text.
-
#select(element, option) ⇒ Object
Pick an option from a select element.
-
#select_date(element, value) ⇒ Object
Fill in a Rails-ish set of date_select fields.
-
#show_me ⇒ Object
Capture the page and try to open it (probably only works on OS X).
-
#visit(path) ⇒ Object
Open a URL.
-
#wait_for(element) ⇒ Object
Wait for a specific element, the page to load or an AJAX request to return.
-
#wait_for_frame(frame) ⇒ Object
Wait for a frame with a give ID to finish loading.
Instance Method Details
#check(locator) ⇒ Object
Check a chek box or toggle a radio button
45 46 47 |
# File 'lib/pyrite/dsl.rb', line 45 def check(locator) browser.check("jquery=#{locator}") end |
#click(locator) ⇒ Object
Click anything else
50 51 52 |
# File 'lib/pyrite/dsl.rb', line 50 def click(locator) browser.click("jquery=#{locator}") end |
#code_me ⇒ Object
Write the HTML of a page to a file and open it. (probably only works on OS X)
117 118 119 120 121 122 |
# File 'lib/pyrite/dsl.rb', line 117 def code_me html = browser.get_html_source filename = "#{Rails.root.join('tmp')}/pyrite-#{Time.now.to_i}.html" code = File.open(filename, 'w') { |f| f.write(html) } puts `open #{code}` end |
#drag_and_drop(opts = {}) ⇒ Object
drag an jquery selector to the center pixel of another, e.g.
`drag_and_drop(:from => "li#element_#{my_oject.id}", :to => "div#trash")
ProTip: if you have a list of elements you wish to re-order, drag the top element down.
103 104 105 |
# File 'lib/pyrite/dsl.rb', line 103 def drag_and_drop(opts={}) browser.drag_and_drop_to_object("jquery=#{opts[:from]}", "jquery=#{opts[:to]}") end |
#fill_in(element, value) ⇒ Object
Fill in a form field
5 6 7 8 9 10 11 |
# File 'lib/pyrite/dsl.rb', line 5 def fill_in(element, value) if element.match /date/ # Try to guess at date selects select_date(element, value) else browser.type("jquery=#{element}", value) end end |
#follow(text) ⇒ Object
Follow a link based on its text
35 36 37 |
# File 'lib/pyrite/dsl.rb', line 35 def follow(text) browser.click("link=#{text}") end |
#get_confirmation ⇒ Object
Use this to consume JS alerts i.e.
follow 'delete'
get_confirmation
assert_no_element "h2:contains('user1')"
96 97 98 |
# File 'lib/pyrite/dsl.rb', line 96 def get_confirmation browser.get_confirmation end |
#inside_iframe(frame) ⇒ Object
Excecute commands within an iframe, then switch back to the parent frame i.e.
inside_frame(iframe) do
click "Submit"
end
82 83 84 85 86 87 88 89 |
# File 'lib/pyrite/dsl.rb', line 82 def inside_iframe(frame) # massive hack but the only way to reliably wait # for the frame sleep 2 browser.select_frame(frame) yield browser.select_frame("relative=parent") end |
#press(text) ⇒ Object
Press an submit (not button) input based on its text
40 41 42 |
# File 'lib/pyrite/dsl.rb', line 40 def press(text) browser.click("jquery=input[type=submit][value='#{text}']") end |
#select(element, option) ⇒ Object
Pick an option from a select element
55 56 57 |
# File 'lib/pyrite/dsl.rb', line 55 def select(element, option) browser.select("jquery=#{element}", option) end |
#select_date(element, value) ⇒ Object
Fill in a Rails-ish set of date_select fields
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/pyrite/dsl.rb', line 14 def select_date(element, value) suffixes = { :year => '1i', :month => '2i', :day => '3i', :hour => '4i', :minute => '5i' } date = value.respond_to?(:year) ? value : Date.parse(value) browser.select "jquery=#{element}_#{suffixes[:year]}", date.year browser.select "jquery=#{element}_#{suffixes[:month]}", date.strftime('%B') browser.select "jquery=#{element}_#{suffixes[:day]}", date.day end |
#show_me ⇒ Object
Capture the page and try to open it (probably only works on OS X)
109 110 111 112 113 |
# File 'lib/pyrite/dsl.rb', line 109 def show_me image = "#{Rails.root.join('tmp')}/pyrite-#{Time.now.to_i}.png" browser.capture_entire_page_screenshot(image, '') puts `open #{image}` end |
#visit(path) ⇒ Object
Open a URL
29 30 31 32 |
# File 'lib/pyrite/dsl.rb', line 29 def visit(path) browser.open(path) wait_for :page_load end |
#wait_for(element) ⇒ Object
Wait for a specific element, the page to load or an AJAX request to return
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/pyrite/dsl.rb', line 61 def wait_for(element) case element when :page_load browser.wait_for(:wait_for => :page) when :ajax browser.wait_for(:wait_for => :ajax, :javascript_framework => Pyrite.js_framework) else browser.wait_for(:element => "jquery=#{element}") end end |
#wait_for_frame(frame) ⇒ Object
Wait for a frame with a give ID to finish loading
73 74 75 |
# File 'lib/pyrite/dsl.rb', line 73 def wait_for_frame(frame) browser.wait_for_frame_to_load(frame, 10000) end |