Dduvall has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/224949

Change subject: Expose current Environment instance for `page_url` interpolation
......................................................................

Expose current Environment instance for `page_url` interpolation

Modified `PageFactory` to define a singleton method called `env` on the
instantiated page object that returns a reference to the current
`Environment`, allowing for interpolated URLs that contain environment
information (e.g. username, browser name, etc.).

Change-Id: Id01631f77964c05bb42ef088e52424ee994ce9b6
---
M lib/mediawiki_selenium/page_factory.rb
M lib/mediawiki_selenium/support/modules/headless_helper.rb
M spec/page_factory_spec.rb
3 files changed, 38 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/selenium 
refs/changes/49/224949/1

diff --git a/lib/mediawiki_selenium/page_factory.rb 
b/lib/mediawiki_selenium/page_factory.rb
index e979678..2c002f8 100644
--- a/lib/mediawiki_selenium/page_factory.rb
+++ b/lib/mediawiki_selenium/page_factory.rb
@@ -11,12 +11,25 @@
     # `PageObject::PageFactory#on_page`. All page URLs are also qualified
     # using {Environment#wiki_url}.
     #
+    # Additionally, an instance of the current {Environment} is made available
+    # as `env` for interpolation of page URLs.
+    #
+    # @example Referencing the `env` in page URLs
+    #   class ArticlePage
+    #     page_url 'User:<%= env.user %>'
+    #   end
+    #
     # @see http://www.rubydoc.info/github/cheezy/page-object
     #
     def on_page(page_class, params = { using_params: {} }, visit = false)
       @browser = browser if visit || !defined?(@browser)
+      env = self
 
       super(page_class, params, false) do |page|
+        page.define_singleton_method(:env) do
+          env
+        end
+
         if page.respond_to?(:goto)
           wiki_url = method(:wiki_url)
 
diff --git a/lib/mediawiki_selenium/support/modules/headless_helper.rb 
b/lib/mediawiki_selenium/support/modules/headless_helper.rb
index a4a6509..87f0050 100644
--- a/lib/mediawiki_selenium/support/modules/headless_helper.rb
+++ b/lib/mediawiki_selenium/support/modules/headless_helper.rb
@@ -38,7 +38,8 @@
         @_display
       end
 
-      # Destroys the global headless display created by {create_display}.
+      # Destroys the global headless display created by
+      # {create_or_reuse_display}.
       #
       def destroy_display
         @_display.destroy if @_display
diff --git a/spec/page_factory_spec.rb b/spec/page_factory_spec.rb
index ce89098..120731a 100644
--- a/spec/page_factory_spec.rb
+++ b/spec/page_factory_spec.rb
@@ -47,8 +47,6 @@
 
       context 'when told to visit a page' do
         let(:visit) { true }
-        let(:config) { { mediawiki_url: 'http://an.example/wiki/' } }
-
         let(:page_object_platform) { double('PageObject::WatirPageObject') }
 
         before do
@@ -57,6 +55,8 @@
         end
 
         context 'where the page URL is defined' do
+          let(:config) { { mediawiki_url: 'http://an.example/wiki/' } }
+
           let(:page_class) do
             Class.new do
               include ::PageObject
@@ -64,15 +64,35 @@
             end
           end
 
-          it 'qualifies the path with the configured :mediawiki_url' do
+          before do
             expect_any_instance_of(page_class).to receive(:platform).
               and_return(page_object_platform)
+          end
 
+          it 'qualifies the path with the configured :mediawiki_url' do
             expect(page_object_platform).to receive(:navigate_to).
               with('http://an.example/wiki/Special:RandomPage')
 
             subject
           end
+
+          context 'and it contains ERb that references `env`' do
+            let(:config) { { mediawiki_url: 'http://an.example/wiki/', 
mediawiki_user: 'user1' } }
+
+            let(:page_class) do
+              Class.new do
+                include ::PageObject
+                page_url 'User:<%= env.user %>'
+              end
+            end
+
+            it 'successfully calls the `env` method that was added to the page 
object' do
+              expect(page_object_platform).to receive(:navigate_to).
+                with('http://an.example/wiki/User:user1')
+
+              subject
+            end
+          end
         end
 
         context 'where the page URL is undefined' do

-- 
To view, visit https://gerrit.wikimedia.org/r/224949
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id01631f77964c05bb42ef088e52424ee994ce9b6
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/selenium
Gerrit-Branch: master
Gerrit-Owner: Dduvall <dduv...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to