Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
Support Capybara 3 current_url/title being for top level browsing con…
Browse files Browse the repository at this point in the history
…text requirement
  • Loading branch information
twalpole committed Mar 21, 2018
1 parent 778ec7f commit 7b25bf9
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 9 deletions.
3 changes: 1 addition & 2 deletions gemfiles/master.gemfile
Expand Up @@ -2,8 +2,7 @@

source "https://rubygems.org"

gem "capybara", github: "jnicklas/capybara"
gem "capybara", github: "teamcapybara/capybara"
gem "puma"

gem 'byebug'
gemspec path: "../"
8 changes: 8 additions & 0 deletions lib/capybara/webkit/browser.rb
Expand Up @@ -28,6 +28,10 @@ def title
command("Title")
end

def frame_title
command("FrameTitle")
end

def find_xpath(query)
command("FindXpath", query).split(",")
end
Expand Down Expand Up @@ -81,6 +85,10 @@ def current_url
command("CurrentUrl")
end

def frame_url
command("FrameUrl")
end

def frame_focus(selector=nil)
if selector.respond_to?(:base)
selector.base.invoke("focus_frame")
Expand Down
20 changes: 18 additions & 2 deletions lib/capybara/webkit/driver.rb
Expand Up @@ -43,7 +43,15 @@ def allow_unknown_urls
end

def current_url
@browser.current_url
if Capybara::VERSION.to_f < 3.0
@browser.frame_url
else
@browser.current_url
end
end

def frame_url
@browser.frame_url
end

def visit(path)
Expand Down Expand Up @@ -73,7 +81,15 @@ def header(key, value)
end

def title
@browser.title
if Capybara::VERSION.to_f < 3.0
frame_title
else
@browser.title
end
end

def frame_title
@browser.frame_title
end

def execute_script(script, *args)
Expand Down
26 changes: 23 additions & 3 deletions spec/driver_spec.rb
Expand Up @@ -35,6 +35,7 @@ def url(path)
<<-HTML
<html>
<head>
<title>Main</title>
<style type="text/css">
#display_none { display: none }
</style>
Expand Down Expand Up @@ -172,7 +173,11 @@ def url(path)

it "returns the current URL" do
driver.within_frame("f") do
expect(driver.current_url).to eq driver_url(driver, "/iframe")
if Capybara::VERSION.to_f < 3.0
expect(driver.current_url).to eq driver_url(driver, "/iframe")
else
expect(driver.current_url).to eq driver_url(driver, "/")
end
end
end

Expand Down Expand Up @@ -210,9 +215,24 @@ def url(path)
end
end

it "returns the document title" do
if Capybara::VERSION.to_f < 3.0
it "returns the document title" do
driver.within_frame("f") do
expect(driver.title).to eq 'Title'
end
end
else
it "returns the top level browsing context text" do
driver.within_frame("f") do
expect(driver.title).to eq 'Main'
end
end
end

it "returns the title for the current frame" do
expect(driver.frame_title).to eq 'Main'
driver.within_frame("f") do
expect(driver.title).to eq "Title"
expect(driver.frame_title).to eq 'Title'
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions src/CommandFactory.cpp
Expand Up @@ -53,6 +53,8 @@
#include "SetUnknownUrlMode.h"
#include "AllowUrl.h"
#include "BlockUrl.h"
#include "FrameTitle.h"
#include "FrameUrl.h"

CommandFactory::CommandFactory(WebPageManager *manager, QObject *parent) : QObject(parent) {
m_manager = manager;
Expand Down
2 changes: 1 addition & 1 deletion src/CurrentUrl.cpp
Expand Up @@ -7,7 +7,7 @@ CurrentUrl::CurrentUrl(WebPageManager *manager, QStringList &arguments, QObject

void CurrentUrl::start() {
QStringList arguments;
QVariant result = page()->currentFrame()->evaluateJavaScript("window.location.toString()");
QVariant result = page()->mainFrame()->evaluateJavaScript("window.location.toString()");
QString url = result.toString();
finish(true, url);
}
Expand Down
11 changes: 11 additions & 0 deletions src/FrameTitle.cpp
@@ -0,0 +1,11 @@
#include "FrameTitle.h"
#include "SocketCommand.h"
#include "WebPage.h"
#include "WebPageManager.h"

FrameTitle::FrameTitle(WebPageManager *manager, QStringList &arguments, QObject *parent) : SocketCommand(manager, arguments, parent) {
}

void FrameTitle::start() {
finish(true, page()->currentFrame()->title());
}
10 changes: 10 additions & 0 deletions src/FrameTitle.h
@@ -0,0 +1,10 @@
#include "SocketCommand.h"

class FrameTitle : public SocketCommand {
Q_OBJECT

public:
FrameTitle(WebPageManager *, QStringList &arguments, QObject *parent = 0);
virtual void start();
};

13 changes: 13 additions & 0 deletions src/FrameUrl.cpp
@@ -0,0 +1,13 @@
#include "FrameUrl.h"
#include "SocketCommand.h"
#include "WebPage.h"
#include "WebPageManager.h"

FrameUrl::FrameUrl(WebPageManager *manager, QStringList &arguments, QObject *parent) : SocketCommand(manager, arguments, parent) {
}

void FrameUrl::start() {
QVariant result = page()->currentFrame()->evaluateJavaScript("window.location.toString()");
QString url = result.toString();
finish(true, url);
}
10 changes: 10 additions & 0 deletions src/FrameUrl.h
@@ -0,0 +1,10 @@
#include "SocketCommand.h"

class FrameUrl : public SocketCommand {
Q_OBJECT

public:
FrameUrl(WebPageManager *, QStringList &arguments, QObject *parent = 0);
virtual void start();
};

2 changes: 1 addition & 1 deletion src/Title.cpp
Expand Up @@ -7,5 +7,5 @@ Title::Title(WebPageManager *manager, QStringList &arguments, QObject *parent) :
}

void Title::start() {
finish(true, page()->currentFrame()->title());
finish(true, page()->mainFrame()->title());
}
2 changes: 2 additions & 0 deletions src/find_command.h
Expand Up @@ -55,3 +55,5 @@ CHECK_COMMAND(FindModal)
CHECK_COMMAND(SetUnknownUrlMode)
CHECK_COMMAND(AllowUrl)
CHECK_COMMAND(BlockUrl)
CHECK_COMMAND(FrameTitle)
CHECK_COMMAND(FrameUrl)
4 changes: 4 additions & 0 deletions src/webkit_server.pro
Expand Up @@ -10,6 +10,8 @@ PRECOMPILED_DIR = $${BUILD_DIR}
OBJECTS_DIR = $${BUILD_DIR}
MOC_DIR = $${BUILD_DIR}
HEADERS = \
FrameUrl.h \
FrameTitle.h \
BlockUrl.h \
AllowUrl.h \
SetUnknownUrlMode.h \
Expand Down Expand Up @@ -95,6 +97,8 @@ HEADERS = \
UnknownUrlHandler.h

SOURCES = \
FrameUrl.cpp \
FrameTitle.cpp \
BlockUrl.cpp \
AllowUrl.cpp \
SetUnknownUrlMode.cpp \
Expand Down

0 comments on commit 7b25bf9

Please sign in to comment.