mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
added evaluate_script method to all javascipt drivers for evaluating javascript
This commit is contained in:
parent
bcff9d7eda
commit
7867084063
3 changed files with 30 additions and 15 deletions
|
@ -5,7 +5,7 @@ class Capybara::Driver::Culerity
|
|||
def text
|
||||
node.text
|
||||
end
|
||||
|
||||
|
||||
def [](name)
|
||||
value = if name.to_sym == :class
|
||||
node.class_name
|
||||
|
@ -18,28 +18,28 @@ class Capybara::Driver::Culerity
|
|||
def set(value)
|
||||
node.set(value)
|
||||
end
|
||||
|
||||
|
||||
def select(option)
|
||||
node.select(option)
|
||||
end
|
||||
|
||||
|
||||
def click
|
||||
node.click
|
||||
end
|
||||
|
||||
|
||||
def drag_to(element)
|
||||
node.fire_event('mousedown')
|
||||
element.node.fire_event('mousemove')
|
||||
element.node.fire_event('mouseup')
|
||||
end
|
||||
|
||||
|
||||
def tag_name
|
||||
# FIXME: this might be the dumbest way ever of getting the tag name
|
||||
# there has to be something better...
|
||||
node.to_xml[/^\s*<([a-z0-9\-\:]+)/, 1]
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
attr_reader :app, :rack_server
|
||||
|
||||
def self.server
|
||||
|
@ -57,25 +57,29 @@ class Capybara::Driver::Culerity
|
|||
@rack_server = Capybara::Server.new(@app)
|
||||
@rack_server.boot
|
||||
end
|
||||
|
||||
|
||||
def visit(path)
|
||||
browser.goto(url(path))
|
||||
end
|
||||
|
||||
|
||||
def body
|
||||
browser.html
|
||||
end
|
||||
|
||||
|
||||
def find(selector)
|
||||
browser.elements_by_xpath(selector).map { |node| Node.new(self, node) }
|
||||
end
|
||||
|
||||
def evaluate_script(script)
|
||||
browser.execute_script "#{script}"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def url(path)
|
||||
rack_server.url(path)
|
||||
end
|
||||
|
||||
|
||||
def browser
|
||||
unless @_browser
|
||||
@_browser = ::Culerity::RemoteBrowserProxy.new self.class.server, {:browser => :firefox, :log_level => :off}
|
||||
|
|
|
@ -84,6 +84,10 @@ class Capybara::Driver::Selenium
|
|||
driver.find_elements(:xpath, selector).map { |node| Node.new(self, node) }
|
||||
end
|
||||
|
||||
def evaluate_script(script)
|
||||
driver.execute_script "return #{script}"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def url(path)
|
||||
|
|
|
@ -37,7 +37,7 @@ shared_examples_for 'driver' do
|
|||
@driver.find('//a')[0].text.should == 'labore'
|
||||
@driver.find('//a')[1].text.should == 'ullamco'
|
||||
end
|
||||
|
||||
|
||||
it "should extract node attributes" do
|
||||
@driver.find('//a')[0][:href].should == '/with_simple_html'
|
||||
@driver.find('//a')[0][:class].should == 'simple'
|
||||
|
@ -51,7 +51,7 @@ shared_examples_for 'driver' do
|
|||
@driver.find('//input').first.set('gorilla')
|
||||
@driver.find('//input').first.value.should == 'gorilla'
|
||||
end
|
||||
|
||||
|
||||
it "should extract node tag name" do
|
||||
@driver.find('//a')[0].tag_name.should == 'a'
|
||||
@driver.find('//a')[1].tag_name.should == 'a'
|
||||
|
@ -63,20 +63,27 @@ shared_examples_for 'driver' do
|
|||
end
|
||||
|
||||
shared_examples_for "driver with javascript support" do
|
||||
before { @driver.visit('/with_js') }
|
||||
|
||||
describe '#find' do
|
||||
it "should find dynamically changed nodes" do
|
||||
@driver.visit('/with_js')
|
||||
@driver.find('//p').first.text.should == 'I changed it'
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
describe '#drag_to' do
|
||||
it "should drag and drop an object" do
|
||||
@driver.visit('/with_js')
|
||||
draggable = @driver.find('//div[@id="drag"]').first
|
||||
droppable = @driver.find('//div[@id="drop"]').first
|
||||
draggable.drag_to(droppable)
|
||||
@driver.find('//div[contains(., "Dropped!")]').should_not be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "#evaluate_script" do
|
||||
it "should return the value of the executed script" do
|
||||
@driver.evaluate_script('1+1').should == 2
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue