Class: PhantomMenace::Browser

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

Overview

PhantomMenace::Browser

Creates a Browser that you can interact with. Exposes methods like how you would find a real browser.

Example

browser = PhantomMenace::Browser.new
browser.goto "http://facebook.com"

Look at the API for more methods.

Instance Method Summary collapse

Instance Method Details

#find(sel) ⇒ Object

Uses jQuery’s selector method. ‘$(sel)`



38
39
40
41
42
43
44
45
46
# File 'lib/phantom_menace/browser.rb', line 38

def find(sel)
  options = {
    command: "find",
    data: { selector: sel }
  }
  post(options)["ret"].map do |node|
    PhantomMenace::Element.new(node)
  end
end


27
28
29
30
31
32
33
34
35
# File 'lib/phantom_menace/browser.rb', line 27

def find_all_links
  options = {
    command: "find",
    data: { selector: "a" }
  }
  post(options)["ret"].map do |node|
    PhantomMenace::Element.new(node)
  end
end

#goto(url) ⇒ Object

Navigates to the specified URL.



19
20
21
22
23
24
25
# File 'lib/phantom_menace/browser.rb', line 19

def goto(url)
  options = {
    command: "goto",
    data: { url: url }
  }
  post(options)
end

#render(filename = nil) ⇒ Object

Renders the loaded page in the specified path, if path is not given, defaults to the current directory.



51
52
53
54
55
56
57
58
# File 'lib/phantom_menace/browser.rb', line 51

def render(filename = nil)
  filename ||= File.expand_path("../", __FILE__) + "/screenshot.png"
  options = {
    command: "render",
    data: { filename: filename }
  }
  post(options)
end