Skip to content
This repository
Browse code

create CurrentUrl command

  • Loading branch information...
commit caa86b72c4b2b2af2d9b05d20e44a0249045ae0f 1 parent eaec979
Marc Schwieterman authored halogenandtoast committed
2  lib/capybara/driver/webkit.rb
@@ -27,7 +27,7 @@ def initialize(app, options={})
27 27
   end
28 28
 
29 29
   def current_url
30  
-    browser.url
  30
+    browser.current_url
31 31
   end
32 32
 
33 33
   def requested_url
4  lib/capybara/driver/webkit/browser.rb
@@ -70,6 +70,10 @@ def requested_url
70 70
       command("RequestedUrl")
71 71
     end
72 72
 
  73
+    def current_url
  74
+      command("CurrentUrl")
  75
+    end
  76
+
73 77
     def frame_focus(frame_id_or_index=nil)
74 78
       if frame_id_or_index.is_a? Fixnum
75 79
         command("FrameFocus", "", frame_id_or_index.to_s)
1  src/CommandFactory.cpp
@@ -20,6 +20,7 @@
20 20
 #include "SetProxy.h"
21 21
 #include "ConsoleMessages.h"
22 22
 #include "RequestedUrl.h"
  23
+#include "CurrentUrl.h"
23 24
 
24 25
 CommandFactory::CommandFactory(WebPage *page, QObject *parent) : QObject(parent) {
25 26
   m_page = page;
71  src/CurrentUrl.cpp
... ...
@@ -0,0 +1,71 @@
  1
+#include "CurrentUrl.h"
  2
+#include "WebPage.h"
  3
+
  4
+CurrentUrl::CurrentUrl(WebPage *page, QObject *parent) : Command(page, parent) {
  5
+}
  6
+
  7
+/*
  8
+ * This CurrentUrl command attempts to produce a current_url value consistent
  9
+ * with that returned by the Selenium WebDriver Capybara driver.
  10
+ *
  11
+ * It does not currently return the correct value in the case of an iframe whose
  12
+ * source URL results in a redirect because the loading of the iframe does not
  13
+ * generate a history item. This is most likely a rare case and is consistent
  14
+ * with the current behavior of the capybara-webkit driver.
  15
+ *
  16
+ * The following two values are *not* affected by Javascript pushState.
  17
+ *
  18
+ * QWebFrame->url()
  19
+ * QWebHistoryItem.originalUrl()
  20
+ *
  21
+ * The following two values *are* affected by Javascript pushState.
  22
+ *
  23
+ * QWebFrame->requestedUrl()
  24
+ * QWebHistoryItem.url()
  25
+ *
  26
+ * In the cases that we have access to both the QWebFrame values and the
  27
+ * correct history item for that frame, we can compare the values and determine
  28
+ * if a redirect occurred and if pushState was used. The table below describes
  29
+ * the various combinations of URL values that are possible.
  30
+ *
  31
+ *   O -> originally requested URL
  32
+ *   R -> URL after redirection
  33
+ *   P -> URL set by pushState
  34
+ *   * -> denotes the desired URL value from the frame
  35
+ *
  36
+ *               frame               history
  37
+ * case          url   requestedUrl  url     originalUrl
  38
+ * -----------------------------------------------------------------
  39
+ * regular load    O     O*            O       O
  40
+ *
  41
+ * redirect w/o    R*    O             R       O
  42
+ * pushState
  43
+ *
  44
+ * pushState       O     P*            P       O
  45
+ * only
  46
+ *
  47
+ * redirect w/     R     P*            P       O
  48
+ * pushState
  49
+ *
  50
+ * Based on the above information, we only need to check for the case of a
  51
+ * redirect w/o pushState, in which case QWebFrame->url() will have the correct
  52
+ * current_url value. In all other cases QWebFrame->requestedUrl() is correct.
  53
+ */
  54
+void CurrentUrl::start(QStringList &arguments) {
  55
+  Q_UNUSED(arguments);
  56
+
  57
+  QUrl humanUrl = wasRedirectedAndNotModifiedByJavascript() ?
  58
+    page()->currentFrame()->url() : page()->currentFrame()->requestedUrl();
  59
+  QByteArray encodedBytes = humanUrl.toEncoded();
  60
+  QString urlString = QString(encodedBytes);
  61
+  emit finished(new Response(true, urlString));
  62
+}
  63
+
  64
+bool CurrentUrl::wasRegularLoad() {
  65
+  return page()->currentFrame()->url() == page()->currentFrame()->requestedUrl();
  66
+}
  67
+
  68
+bool CurrentUrl::wasRedirectedAndNotModifiedByJavascript() {
  69
+  return !wasRegularLoad() && page()->currentFrame()->url() == page()->history()->currentItem().url();
  70
+}
  71
+
16  src/CurrentUrl.h
... ...
@@ -0,0 +1,16 @@
  1
+#include "Command.h"
  2
+
  3
+class WebPage;
  4
+
  5
+class CurrentUrl : public Command {
  6
+  Q_OBJECT
  7
+
  8
+  public:
  9
+    CurrentUrl(WebPage *page, QObject *parent = 0);
  10
+    virtual void start(QStringList &arguments);
  11
+
  12
+  private:
  13
+    bool wasRegularLoad();
  14
+    bool wasRedirectedAndNotModifiedByJavascript();
  15
+};
  16
+
1  src/find_command.h
@@ -24,3 +24,4 @@ CHECK_COMMAND(Headers)
24 24
 CHECK_COMMAND(SetProxy)
25 25
 CHECK_COMMAND(ConsoleMessages)
26 26
 CHECK_COMMAND(RequestedUrl)
  27
+CHECK_COMMAND(CurrentUrl)
2  src/webkit_server.pro
@@ -2,6 +2,7 @@ TEMPLATE = app
2 2
 TARGET = webkit_server
3 3
 DESTDIR = .
4 4
 HEADERS = \
  5
+  CurrentUrl.h \
5 6
   RequestedUrl.h \
6 7
   ConsoleMessages.h \
7 8
   WebPage.h \
@@ -35,6 +36,7 @@ HEADERS = \
35 36
   SetProxy.h \
36 37
 
37 38
 SOURCES = \
  39
+  CurrentUrl.cpp \
38 40
   RequestedUrl.cpp \
39 41
   ConsoleMessages.cpp \
40 42
   main.cpp \

0 notes on commit caa86b7

Please sign in to comment.
Something went wrong with that request. Please try again.