https://github.com/jdenen/mobilify
page-object method replacement based on how the instance is constructed
https://github.com/jdenen/mobilify
Last synced: 3 months ago
JSON representation
page-object method replacement based on how the instance is constructed
- Host: GitHub
- URL: https://github.com/jdenen/mobilify
- Owner: jdenen
- License: mit
- Created: 2013-12-11T04:15:50.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2014-11-18T18:53:52.000Z (over 10 years ago)
- Last Synced: 2025-01-21T22:45:08.462Z (5 months ago)
- Language: Ruby
- Homepage:
- Size: 383 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: changes.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Mobilify
[](http://badge.fury.io/rb/mobilify)Invoke one page-object method name but execute different definitions of that method based on the page-object's context.
We use it at [Manta](http://www.manta.com) to keep the number of test scripts low while testing against multiple versions of the same feature (legacy version, new desktop version, new mobile version, etc). I wrote about it [here](http://jdenen.github.io/2014/11/18/redefining-page-objects-at-runtime/).
## Usage
To contextualize your page objects, ```include Mobilify``` in the page class. For each method requiring a contextual replacement, create an element (or method) definition with your context prepended to the original's name (like `mobile_` or `legacy_`).```ruby
# method
text_field(:password, :id => "user-pw")
# replacement
text_field(:mobile_password, :id => "mobile-pw")
```Mobilify will replace called methods with their contextual counterparts if two conditions are met. First, a hash with a key-value pair of `:context => :{your_context}` is passed to the page object constructor. And second, the page object responds to `{your_context}_method`.
```ruby
# constructor
my_page = Page.new(@browser, :context => :mobile)
```Mobilify also supports multiple contexts per page-object instance, invoking the first applicable context it matches in a given array.
```ruby
# multiple context constructor
my_page = Page.new(@browser, :context => [:legacy, :mobile])
```#### Example
I want to test a login page, but my there are basically three versions of the same page in some cycle of development. There's legacy code, new responsive code at the desktop size, and new responsive code at the mobile size. All three have different identifiers for the login form.
I'll use Mobilify to write one script and test all browsers and environments.
```ruby
# login.rb
require "mobilify"class Login
include Mobilify
text_field(:user, :id => "email")
text_field(:mobile_user, :id => "xs-email")
text_field(:legacy_user, :id => "member-email")
text_field(:password, :id => "password")
text_field(:mobile_password, :id => "xs-password")
text_field(:legacy_password, :id => "member-password")
button(:submit, :id => "submit")
end
``````ruby
# login_spec.rb
require "rspec-given"
require "support/login"describe "logging in" do
Given(:login) { Login.new(@browser, :context => [:mobile, :legacy]) }
When { login.goto }
When { login.user = "[email protected]" }
When { login.password = "password123" }
When { login.submit }
Then { ... }
end
```## Installation
Add this line to your application's Gemfile:
gem 'mobilify'
And then execute:
$ bundle
Or install it yourself as:
$ gem install mobilify
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request## Questions, Comments, Concerns
Easiest place to reach me is Twitter, [@jpdenen](http://twitter.com/jpdenen)