Created
February 12, 2016 01:26
-
-
Save synth/2721f83480360b38c398 to your computer and use it in GitHub Desktop.
Ecmabara - A foolish attempt to reimplement capybara-webkit in pure js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Ecmabara | |
def self.included(base_class) | |
base_class.class_eval do | |
def self.new | |
super.extend Page | |
end | |
end | |
end | |
module Page | |
def page | |
@page ||= JsPage.new(super) | |
end | |
end | |
class JsPage | |
attr_reader :page, :scopes | |
delegate :set_rack_session, :visit, :evaluate_script, :execute_script, to: :page | |
delegate :allow_url, :block_unknown_urls, to: :og_driver | |
def initialize(page) | |
@page = page | |
@scopes = [] | |
end | |
def driver | |
self | |
end | |
def og_driver | |
page.driver | |
end | |
def click_on(selector) | |
page.execute_script(%Q($(#{scoped_selector(selector)}).trigger('click'))) | |
# rescue | |
# page.execute_script(%Q($('#{jquery_selector} a:contains(#{selector})').trigger('click'))) | |
end | |
def within(selector, &block) | |
scopes << selector | |
yield | |
scopes.pop | |
end | |
def find(selector) | |
JsNode.new(selector, self) | |
end | |
def inspect | |
to_s | |
end | |
def to_s | |
page.body.to_s | |
end | |
def scoped_selector(selector) | |
s = self.scopes.join(" ") | |
s << " " | |
s << selector | |
return %Q($('#{s}')) | |
end | |
end | |
class JsNode | |
attr_reader :selector, :page | |
def initialize(selector, page) | |
@selector = selector | |
@page = page | |
end | |
def to_s | |
page.evaluate_script(%Q(#{scoped_selector}.html();)) | |
end | |
def inspect | |
to_s | |
end | |
def [](key) | |
page.evaluate_script(%Q(#{scoped_selector}.prop('#{key}');)) | |
end | |
def text | |
page.evaluate_script(%Q(#{scoped_selector}.html())) | |
end | |
def scoped_selector | |
s = page.scopes.join(" ") | |
s << " " | |
s << selector | |
return %Q($('#{s}')) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment