Module: Unmagic::Browser::Console::Helpers
- Defined in:
- lib/unmagic/browser/console/helpers.rb
Overview
The verbs, as bare words.
A console is the one place where open "https://example.com" beats
session = browser.open("https://example.com") — you're typing, not
writing code that has to survive. Every verb here drives one current
session, opened by #open and remembered until you #close it, so the
reads and the actions all know what page they're on without being told.
The four verbs that work on a whole page — #markdown, #html, #screenshot and #pdf — take an optional URL. With one they're a one-off grab that opens nothing; without one they read the current session.
start mixes this into a console's main. Mix it in yourself
to get the same verbs somewhere else.
Instance Method Summary collapse
- #all(query = nil, **options) ⇒ Array<Unmagic::Browser::Element>
- #attach_file(query = nil, *paths, **options) ⇒ Unmagic::Browser::Session
- #back ⇒ Unmagic::Browser::Session
-
#browser ⇒ Unmagic::Browser
The browser the verbs run against.
- #check(query = nil, **options) ⇒ Unmagic::Browser::Session
- #choose(query = nil, **options) ⇒ Unmagic::Browser::Session
- #click(query = nil, **options) ⇒ Unmagic::Browser::Session
- #click_button(query = nil, **options) ⇒ Unmagic::Browser::Session
- #click_link(query = nil, **options) ⇒ Unmagic::Browser::Session
- #close ⇒ nil
-
#emulate(config = nil, **knobs) ⇒ Unmagic::Browser::Emulation
What's being emulated now.
- #fill_in(query = nil, with:, **options) ⇒ Unmagic::Browser::Session
- #find(query = nil, **options) ⇒ Unmagic::Browser::Element
- #forward ⇒ Unmagic::Browser::Session
-
#has_button?(query = nil, **options) ⇒ Boolean
Whether such a button is on the page right now.
-
#has_field?(query = nil, **options) ⇒ Boolean
Whether such a field is on the page right now.
-
#has_link?(query = nil, **options) ⇒ Boolean
Whether such a link is on the page right now.
-
#has_selector?(css) ⇒ Boolean
Whether anything matches it right now.
-
#has_text?(string) ⇒ Boolean
Whether it's on the page right now.
-
#help ⇒ nil
What you can type, and what it does.
- #hover(query = nil, **options) ⇒ Unmagic::Browser::Session
-
#html(target = nil) ⇒ Code
The HTML, syntax-highlighted when shown.
-
#js(script, *args) ⇒ Object
(also: #evaluate)
Whatever it returns.
-
#markdown(target = nil) ⇒ Code
The Markdown, syntax-highlighted when shown.
-
#open(url = nil, **options) ⇒ Unmagic::Browser::Session
Open a page and make it the one every other verb drives.
-
#pdf(target = nil) ⇒ Document
PDF bytes, written to a file when shown.
- #press(key, **options) ⇒ Unmagic::Browser::Session
- #reload ⇒ Unmagic::Browser::Session
-
#screenshot(target = nil, full_page: false, selector: nil) ⇒ Image
PNG bytes, drawn in the terminal when shown.
- #select(option, from: nil, **options) ⇒ Unmagic::Browser::Session
-
#session ⇒ Unmagic::Browser::Session?
The session the verbs are driving.
-
#text ⇒ String
The visible text of the page.
-
#title ⇒ String
The page's title.
- #uncheck(query = nil, **options) ⇒ Unmagic::Browser::Session
-
#url ⇒ String
The URL currently shown.
-
#use(driver) ⇒ Unmagic::Browser
Switch drivers, closing whatever's open.
-
#value(query = nil, **options) ⇒ String?
What's in the field.
- #visit(url) ⇒ Unmagic::Browser::Session
- #wait_for(**options) ⇒ Unmagic::Browser::Session
-
#within(query = nil, **options) { ... } ⇒ Object
Narrow every verb inside the block to one part of the page.
Instance Method Details
#all(query = nil, **options) ⇒ Array<Unmagic::Browser::Element>
277 278 279 |
# File 'lib/unmagic/browser/console/helpers.rb', line 277 def all(query = nil, **) current.all(query, **) end |
#attach_file(query = nil, *paths, **options) ⇒ Unmagic::Browser::Session
153 154 155 |
# File 'lib/unmagic/browser/console/helpers.rb', line 153 def attach_file(query = nil, *paths, **) current.attach_file(query, *paths, **) end |
#back ⇒ Unmagic::Browser::Session
70 71 72 |
# File 'lib/unmagic/browser/console/helpers.rb', line 70 def back current.go_back end |
#browser ⇒ Unmagic::Browser
Returns the browser the verbs run against.
25 26 27 |
# File 'lib/unmagic/browser/console/helpers.rb', line 25 def browser Console.browser end |
#check(query = nil, **options) ⇒ Unmagic::Browser::Session
131 132 133 |
# File 'lib/unmagic/browser/console/helpers.rb', line 131 def check(query = nil, **) current.check(query, **) end |
#choose(query = nil, **options) ⇒ Unmagic::Browser::Session
145 146 147 |
# File 'lib/unmagic/browser/console/helpers.rb', line 145 def choose(query = nil, **) current.choose(query, **) end |
#click(query = nil, **options) ⇒ Unmagic::Browser::Session
102 103 104 |
# File 'lib/unmagic/browser/console/helpers.rb', line 102 def click(query = nil, **) current.click(query, **) end |
#click_button(query = nil, **options) ⇒ Unmagic::Browser::Session
109 110 111 |
# File 'lib/unmagic/browser/console/helpers.rb', line 109 def (query = nil, **) current.(query, **) end |
#click_link(query = nil, **options) ⇒ Unmagic::Browser::Session
116 117 118 |
# File 'lib/unmagic/browser/console/helpers.rb', line 116 def click_link(query = nil, **) current.click_link(query, **) end |
#close ⇒ nil
301 302 303 304 |
# File 'lib/unmagic/browser/console/helpers.rb', line 301 def close Console.session&.close Console.session = nil end |
#emulate(config = nil, **knobs) ⇒ Unmagic::Browser::Emulation
Returns what's being emulated now.
296 297 298 |
# File 'lib/unmagic/browser/console/helpers.rb', line 296 def emulate(config = nil, **knobs) current.emulate(config, **knobs) end |
#fill_in(query = nil, with:, **options) ⇒ Unmagic::Browser::Session
95 96 97 |
# File 'lib/unmagic/browser/console/helpers.rb', line 95 def fill_in(query = nil, with:, **) current.fill_in(query, with: with, **) end |
#find(query = nil, **options) ⇒ Unmagic::Browser::Element
270 271 272 |
# File 'lib/unmagic/browser/console/helpers.rb', line 270 def find(query = nil, **) current.find(query, **) end |
#forward ⇒ Unmagic::Browser::Session
75 76 77 |
# File 'lib/unmagic/browser/console/helpers.rb', line 75 def forward current.go_forward end |
#has_button?(query = nil, **options) ⇒ Boolean
Returns whether such a button is on the page right now.
241 242 243 |
# File 'lib/unmagic/browser/console/helpers.rb', line 241 def (query = nil, **) current.(query, **) end |
#has_field?(query = nil, **options) ⇒ Boolean
Returns whether such a field is on the page right now.
255 256 257 |
# File 'lib/unmagic/browser/console/helpers.rb', line 255 def has_field?(query = nil, **) current.has_field?(query, **) end |
#has_link?(query = nil, **options) ⇒ Boolean
Returns whether such a link is on the page right now.
248 249 250 |
# File 'lib/unmagic/browser/console/helpers.rb', line 248 def has_link?(query = nil, **) current.has_link?(query, **) end |
#has_selector?(css) ⇒ Boolean
Returns whether anything matches it right now.
261 262 263 |
# File 'lib/unmagic/browser/console/helpers.rb', line 261 def has_selector?(css) current.has_selector?(css) end |
#has_text?(string) ⇒ Boolean
Returns whether it's on the page right now.
234 235 236 |
# File 'lib/unmagic/browser/console/helpers.rb', line 234 def has_text?(string) current.has_text?(string) end |
#help ⇒ nil
What you can type, and what it does.
309 310 311 312 |
# File 'lib/unmagic/browser/console/helpers.rb', line 309 def help puts(Banner.help) nil end |
#hover(query = nil, **options) ⇒ Unmagic::Browser::Session
167 168 169 |
# File 'lib/unmagic/browser/console/helpers.rb', line 167 def hover(query = nil, **) current.hover(query, **) end |
#html(target = nil) ⇒ Code
Returns the HTML, syntax-highlighted when shown.
180 181 182 |
# File 'lib/unmagic/browser/console/helpers.rb', line 180 def html(target = nil) Code.new(target ? browser.html(target) : current.html, language: "html", name: "page") end |
#js(script, *args) ⇒ Object Also known as: evaluate
Returns whatever it returns.
219 220 221 |
# File 'lib/unmagic/browser/console/helpers.rb', line 219 def js(script, *args) current.evaluate(script, *args) end |
#markdown(target = nil) ⇒ Code
Returns the Markdown, syntax-highlighted when shown.
186 187 188 |
# File 'lib/unmagic/browser/console/helpers.rb', line 186 def markdown(target = nil) Code.new(target ? browser.markdown(target) : current.markdown, language: "markdown", name: "page") end |
#open(url = nil, **options) ⇒ Unmagic::Browser::Session
Open a page and make it the one every other verb drives. Closes whatever was open first — one session at a time is what a console wants.
53 54 55 56 |
# File 'lib/unmagic/browser/console/helpers.rb', line 53 def open(url = nil, **) Console.session&.close Console.session = browser.open(url, **) end |
#pdf(target = nil) ⇒ Document
Returns PDF bytes, written to a file when shown.
205 206 207 |
# File 'lib/unmagic/browser/console/helpers.rb', line 205 def pdf(target = nil) Document.new(target ? browser.pdf(target) : current.pdf, name: "page") end |
#press(key, **options) ⇒ Unmagic::Browser::Session
160 161 162 |
# File 'lib/unmagic/browser/console/helpers.rb', line 160 def press(key, **) current.press(key, **) end |
#reload ⇒ Unmagic::Browser::Session
65 66 67 |
# File 'lib/unmagic/browser/console/helpers.rb', line 65 def reload current.reload end |
#screenshot(target = nil, full_page: false, selector: nil) ⇒ Image
Returns PNG bytes, drawn in the terminal when shown.
194 195 196 197 198 199 200 201 |
# File 'lib/unmagic/browser/console/helpers.rb', line 194 def screenshot(target = nil, full_page: false, selector: nil) bytes = if target browser.screenshot(target, full_page: full_page, selector: selector) else current.screenshot(full_page: full_page, selector: selector) end Image.new(bytes) end |
#select(option, from: nil, **options) ⇒ Unmagic::Browser::Session
124 125 126 |
# File 'lib/unmagic/browser/console/helpers.rb', line 124 def select(option, from: nil, **) current.select(option, from: from, **) end |
#session ⇒ Unmagic::Browser::Session?
Returns the session the verbs are driving.
40 41 42 |
# File 'lib/unmagic/browser/console/helpers.rb', line 40 def session Console.session end |
#text ⇒ String
Returns the visible text of the page.
174 175 176 |
# File 'lib/unmagic/browser/console/helpers.rb', line 174 def text current.text end |
#title ⇒ String
Returns the page's title.
85 86 87 |
# File 'lib/unmagic/browser/console/helpers.rb', line 85 def title current.title end |
#uncheck(query = nil, **options) ⇒ Unmagic::Browser::Session
138 139 140 |
# File 'lib/unmagic/browser/console/helpers.rb', line 138 def uncheck(query = nil, **) current.uncheck(query, **) end |
#url ⇒ String
Returns the URL currently shown.
80 81 82 |
# File 'lib/unmagic/browser/console/helpers.rb', line 80 def url current.current_url end |
#use(driver) ⇒ Unmagic::Browser
Switch drivers, closing whatever's open.
34 35 36 37 |
# File 'lib/unmagic/browser/console/helpers.rb', line 34 def use(driver) Console.reset(driver) browser end |
#value(query = nil, **options) ⇒ String?
Returns what's in the field.
212 213 214 |
# File 'lib/unmagic/browser/console/helpers.rb', line 212 def value(query = nil, **) current.value(query, **) end |
#visit(url) ⇒ Unmagic::Browser::Session
60 61 62 |
# File 'lib/unmagic/browser/console/helpers.rb', line 60 def visit(url) current.visit(url) end |
#wait_for(**options) ⇒ Unmagic::Browser::Session
228 229 230 |
# File 'lib/unmagic/browser/console/helpers.rb', line 228 def wait_for(**) current.wait_for(**) end |
#within(query = nil, **options) { ... } ⇒ Object
Narrow every verb inside the block to one part of the page.
287 288 289 |
# File 'lib/unmagic/browser/console/helpers.rb', line 287 def within(query = nil, **, &block) current.within(query, **) { block.call } end |