Permalink
Browse files

Implement Node#hover

  • Loading branch information...
1 parent 2342fc5 commit 47ac51e4dce88b24f47e35006ebe1a5ffca9f88c David Tengdin and Matthew Horan committed Feb 26, 2013
@@ -1,5 +1,5 @@
PATH
- remote: /Users/pivotal/workspace/capybara-webkit
+ remote: /home/mhoran/capybara-webkit
specs:
capybara-webkit (0.14.1)
capybara (~> 2.0, >= 2.0.2)
@@ -1,6 +1,6 @@
GIT
remote: git://github.com/jnicklas/capybara.git
- revision: 0c53168899fadee52caced605dd2d6f3e48e6ec4
+ revision: 458bb23a5cc17199aa0989fb0a0b6fb2617be74e
submodules: true
specs:
capybara (2.0.2)
@@ -13,7 +13,7 @@ GIT
nokogiri (~> 1.3)
PATH
- remote: /Users/pivotal/workspace/capybara-webkit
+ remote: /home/mhoran/capybara-webkit
specs:
capybara-webkit (0.14.1)
capybara (~> 2.0, >= 2.0.2)
@@ -67,6 +67,10 @@ def right_click
invoke("rightClick")
end
+ def hover
+ invoke("hover")
+ end
+
def drag_to(element)
invoke 'dragTo', element.native
end
View
@@ -1116,10 +1116,23 @@ def visit(url, driver=driver)
context "mouse app" do
let(:driver) do
driver_for_html(<<-HTML)
- <html><body>
+ <html>
+ <head>
+ <style type="text/css">
+ #hover { max-width: 30em; }
+ #hover span { line-height: 1.5; }
+ #hover span:hover + .hidden { display: block; }
+ .hidden { display: none; }
+ </style>
+ </head>
+ <body>
<div id="change">Change me</div>
<div id="mouseup">Push me</div>
<div id="mousedown">Release me</div>
+ <div id="hover">
+ <span>This really long paragraph has a lot of text and will wrap. This sentence ensures that we have four lines of text.</span>
+ <div class="hidden">Text that only shows on hover.</div>
+ </div>
<form action="/" method="GET">
<select id="change_select" name="change_select">
<option value="1" id="option-1" selected="selected">one</option>
@@ -1151,6 +1164,24 @@ def visit(url, driver=driver)
before { visit("/") }
+ it "hovers an element" do
+ driver.find_css("#hover").first.visible_text.should_not =~ /Text that only shows on hover/
+ driver.find_css("#hover span").first.hover
+ driver.find_css("#hover").first.visible_text.should =~ /Text that only shows on hover/
+ end
+
+ it "hovers an element off the screen" do
+ driver.resize_window(200, 200)
+ driver.evaluate_script(<<-JS)
+ var element = document.getElementById('hover');
+ element.style.position = 'absolute';
+ element.style.left = '200px';
+ JS
+ driver.find_css("#hover").first.visible_text.should_not =~ /Text that only shows on hover/
+ driver.find_css("#hover span").first.hover
+ driver.find_css("#hover").first.visible_text.should =~ /Text that only shows on hover/
+ end
+
it "clicks an element" do
driver.find_xpath("//a").first.click
driver.current_url =~ %r{/next$}
@@ -38,15 +38,15 @@ InvocationResult JavascriptInvocation::invoke(QWebFrame *frame) {
void JavascriptInvocation::leftClick(int x, int y) {
QPoint mousePos(x, y);
- JavascriptInvocation::mouseEvent(QEvent::MouseMove, mousePos, Qt::NoButton);
+ hover(mousePos);
JavascriptInvocation::mouseEvent(QEvent::MouseButtonPress, mousePos, Qt::LeftButton);
JavascriptInvocation::mouseEvent(QEvent::MouseButtonRelease, mousePos, Qt::LeftButton);
}
void JavascriptInvocation::rightClick(int x, int y) {
QPoint mousePos(x, y);
- JavascriptInvocation::mouseEvent(QEvent::MouseMove, mousePos, Qt::NoButton);
+ hover(mousePos);
JavascriptInvocation::mouseEvent(QEvent::MouseButtonPress, mousePos, Qt::RightButton);
}
@@ -92,3 +92,12 @@ QVariantMap JavascriptInvocation::clickPosition(QWebElement element, int left, i
return m;
}
+
+void JavascriptInvocation::hover(int absoluteX, int absoluteY) {
+ QPoint mousePos(absoluteX, absoluteY);
+ hover(mousePos);
+}
+
+void JavascriptInvocation::hover(const QPoint &mousePos) {
+ mouseEvent(QEvent::MouseMove, mousePos, Qt::NoButton);
+}
@@ -22,6 +22,7 @@ class JavascriptInvocation : public QObject {
Q_INVOKABLE void doubleClick(int x, int y);
Q_INVOKABLE bool clickTest(QWebElement element, int absoluteX, int absoluteY);
Q_INVOKABLE QVariantMap clickPosition(QWebElement element, int left, int top, int width, int height);
+ Q_INVOKABLE void hover(int absoluteX, int absoluteY);
QVariant getError();
void setError(QVariant error);
InvocationResult invoke(QWebFrame *);
@@ -32,5 +33,6 @@ class JavascriptInvocation : public QObject {
WebPage *m_page;
QVariant m_error;
void mouseEvent(QEvent::Type type, const QPoint & position, Qt::MouseButton button);
+ void hover(const QPoint &);
};
View
@@ -176,6 +176,15 @@ Capybara = {
this.click(index, CapybaraInvocation.rightClick);
},
+ hover: function (index) {
+ var node = this.nodes[index];
+ node.scrollIntoViewIfNeeded();
+
+ var pos = this.clickPosition(node);
+ if (pos)
+ CapybaraInvocation.hover(pos.absoluteX, pos.absoluteY);
+ },
+
trigger: function (index, eventName) {
var eventObject = document.createEvent("HTMLEvents");
eventObject.initEvent(eventName, true, true);

0 comments on commit 47ac51e

Please sign in to comment.