From 1c7e5b70266d9dfb06da077219e575e2730b202c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jonne=20Ha=C3=9F?= <me@jhass.eu>
Date: Thu, 4 Dec 2014 22:15:31 +0100
Subject: [PATCH] Improve apend_to_publisher

- Use custom matcher that polls #value of the element
- Wait for the text field to become visible before entering text
---
 features/support/matchers.rb                | 31 +++++++++++++++++----
 features/support/publishing_cuke_helpers.rb |  4 +--
 2 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/features/support/matchers.rb b/features/support/matchers.rb
index 17ce581e41..71a4bb6007 100644
--- a/features/support/matchers.rb
+++ b/features/support/matchers.rb
@@ -1,11 +1,6 @@
 RSpec::Matchers.define :have_path do |expected|
   match do |actual|
-    start_time = Time.now
-    until actual.current_path == expected
-      return false if (Time.now-start_time) > Capybara.default_wait_time
-      sleep 0.05
-    end
-    true
+    await_condition { actual.current_path == expected }
   end
 
   failure_message_for_should do |actual|
@@ -15,3 +10,27 @@ RSpec::Matchers.define :have_path do |expected|
     "expected #{actual.inspect} to not have path #{expected.inspect} but it had"
   end
 end
+
+
+RSpec::Matchers.define :have_value do |expected|
+  match do |actual|
+    await_condition { actual.value && actual.value.include?(expected) }
+  end
+
+  failure_message_for_should do |actual|
+    "expected #{actual.inspect} to have value #{expected.inspect} but was #{actual.value.inspect}"
+  end
+  failure_message_for_should_not do |actual|
+    "expected #{actual.inspect} to not have value #{expected.inspect} but it had"
+  end
+end
+
+
+def await_condition &condition
+  start_time = Time.now
+  until condition.call
+    return false if (Time.now-start_time) > Capybara.default_wait_time
+    sleep 0.05
+  end
+  true
+end
diff --git a/features/support/publishing_cuke_helpers.rb b/features/support/publishing_cuke_helpers.rb
index 7c15d6ff41..a49f3101ce 100644
--- a/features/support/publishing_cuke_helpers.rb
+++ b/features/support/publishing_cuke_helpers.rb
@@ -4,11 +4,11 @@ module PublishingCukeHelpers
   end
 
   def append_to_publisher(txt, input_selector='#status_message_fake_text')
-    elem = find(input_selector, visible: false)
+    elem = find(input_selector)
     elem.native.send_keys(' ' + txt)
 
     # make sure the other text field got the new contents
-    expect(page).to have_xpath("//input|textarea/.[@id='status_message_text' and contains(@value, '#{txt}')]", visible: false)
+    expect(find("#status_message_text", visible: false)).to have_value txt
   end
 
   def upload_file_with_publisher(path)
-- 
GitLab