1
0
Fork 0
mirror of https://github.com/thoughtbot/capybara-webkit synced 2023-03-27 23:22:28 -04:00

Instantiate errors in capybara-webkit

This commit is contained in:
Andrey Chernih 2013-05-21 15:58:46 +04:00
parent 7bf3c80031
commit 77973a47ae
7 changed files with 51 additions and 15 deletions

View file

@ -212,7 +212,7 @@ module Capybara::Webkit
if result.nil?
raise NoResponseError, "No response received from the server."
elsif result != 'ok'
raise JSON.load(read_response)
raise JsonError.new(read_response)
end
result

View file

@ -1,12 +1,5 @@
module Capybara::Webkit
module JsonError
def json_create(attributes)
new(attributes["message"])
end
end
class InvalidResponseError < StandardError
extend JsonError
end
class NoResponseError < StandardError
@ -16,10 +9,27 @@ module Capybara::Webkit
end
class ClickFailed < StandardError
extend JsonError
end
class TimeoutError < Timeout::Error
extend JsonError
end
class JsonError
def initialize(response)
error = JSON.parse response
@class_name = error['class']
@message = error['message']
end
def exception
error_class.new @message
end
private
def error_class
Capybara::Webkit.const_get @class_name
end
end
end

View file

@ -7,7 +7,8 @@ require 'base64'
describe Capybara::Webkit::Browser do
let(:browser) { Capybara::Webkit::Browser.new(Capybara::Webkit::Connection.new) }
let(:connection) { Capybara::Webkit::Connection.new }
let(:browser) { Capybara::Webkit::Browser.new(connection) }
let(:browser_ignore_ssl_err) do
Capybara::Webkit::Browser.new(Capybara::Webkit::Connection.new).tap do |browser|
browser.ignore_ssl_errors
@ -258,4 +259,18 @@ describe Capybara::Webkit::Browser do
expect { browser.visit("/") }.not_to raise_error(/empty response/)
end
describe '#command' do
context 'non-ok response' do
it 'raises an error of given class' do
error_json = '{"class": "ClickFailed"}'
connection.should_receive(:gets).ordered.and_return 'error'
connection.should_receive(:gets).ordered.and_return error_json.bytesize
connection.stub read: error_json
expect { browser.command 'blah', 'meh' }.to raise_error(Capybara::Webkit::ClickFailed)
end
end
end
end

11
spec/errors_spec.rb Normal file
View file

@ -0,0 +1,11 @@
require 'spec_helper'
describe Capybara::Webkit::JsonError do
let(:error) { described_class.new '{"class": "ClickFailed", "message": "Error clicking this element"}' }
subject { error.exception }
it { should be_an_instance_of Capybara::Webkit::ClickFailed }
its(:message) { should == 'Error clicking this element' }
end

View file

@ -16,9 +16,9 @@ QByteArray ErrorMessage::toString() {
QVariantMap map;
if (m_type.isNull())
map["json_class"] = "Capybara::Webkit::InvalidResponseError";
map["class"] = "InvalidResponseError";
else
map["json_class"] = m_type;
map["class"] = m_type;
map["message"] = m_message;

View file

@ -23,7 +23,7 @@ ErrorMessage *InvocationResult::errorMessage() {
QString message = error["message"].toString();
if (error["name"] == "Capybara.ClickFailed")
return new ErrorMessage("Capybara::Webkit::ClickFailed", message);
return new ErrorMessage("ClickFailed", message);
else
return new ErrorMessage(message);
}

View file

@ -60,7 +60,7 @@ void TimeoutCommand::commandTimeout() {
disconnect(m_command, SIGNAL(finished(Response *)), this, SLOT(commandFinished(Response *)));
m_manager->currentPage()->triggerAction(QWebPage::Stop);
QString message = QString("Request timed out after %1 second(s)").arg(m_manager->getTimeout());
finish(false, new ErrorMessage("Capybara::Webkit::TimeoutError", message));
finish(false, new ErrorMessage("TimeoutError", message));
}
void TimeoutCommand::commandFinished(Response *response) {