-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhanced support for page object model #414
Enhanced support for page object model #414
Conversation
Quite a large feature. If you don't mind could you explain the differences between this and the existing implementation? |
Definitely - the existing feature allows for a page object that is more like a custom command except with special namespacing under client.api.page. This feature adds to that by allowing you to abstract the page, making it more similar to other page object libraries. The goal is to make for more readable and maintainable tests. For comparison, here are the existing page object demo tests rewritten with the new feature: tests/google.js: module.exports = {
tags: ['google'],
'Demo test Google' : function (client) {
var google = client.page.google();
google.goTo() // if a url is provided on page object, will navigate there by default
.assert.title('Google')
.assert.visible('searchBar') // selectors live in page object, not hardcoded in tests
.setValue('searchBar', 'nightwatch')
.submit() // logic of waiting, clicking, then pause is encapsulated in page object
.assert.containsText('results', 'Night Watch');
client.end();
}
}; pages/google.js: var googleMixins = {
submit: function() {
this.waitForElementVisible('submitButton', 1000)
.click('submitButton');
this.api.pause(1000);
return this; // For chaining to page object
}
};
module.exports = {
url: 'http://google.com',
elements: {
searchBar: { selector: 'input[type=text]' },
submitButton: { selector: 'button[name=btnG]' },
results: { selector: '#main' }
},
mixins: [googleMixins]
}; Key differences are:
For other features not illustrated in this example, check out the examples in the PR. There I use a more complicated site IMDB to better demonstrate some cool features: Sections: Ability to share common selectors and functions across page objects: For reference, here's another page object library (written in Ruby) that I think does a nice job at providing a simple interface but also supports more advanced page object needs like sections: Note also this feature is behind a feature flag |
Thanks, I'll look into it. |
Hey @beatfactor - have you had a chance to take a look? What do you think? |
I had a brief look but haven't reviewed everything yet. One question would On Friday, April 17, 2015, Stephanie Madison [email protected]
|
It's not compatible probably because of this feature:
But, isn't this a little bit confusing? nightwatch already has a page object feature, a |
I put this behind a toggle more as a safeguard to ensure no backward compatibility issues. Here [1] we add methods previously just on client.api (element commands, assertions, etc) to the page object instance only if the toggle is set. Otherwise if someone happens to have a method called |
Oh and we can definitely change the name to @maxgalbu's point. I'm not sure I like the term "new" but what about Also @beatfactor I'm aware of some style inconsistencies since I posted this - I was going to wait to fix those until you've decided if you want to move forward with this but LMK if you want me to address those sooner. |
I'm thinking if there's any way to merge this feature into the existing On Friday, April 17, 2015, Stephanie Madison [email protected]
|
OK, I can think of a few ways offhand but not sure they are ideal. I'll check out what you did for assertions, thanks for the tip. |
I ended up adding the condition: It may actually be enough to just check that it's an object as the nightwatch documentation says that the page object should be a (non-instantiated) class. But I threw in the check for elements or sections attributes to be safe. |
Hey @beatfactor since this is such a big change I thought it would be helpful to draft what the documentation would look like after this change, so you can see better how it gets used: nightwatchjs/nightwatch-docs#4 I'll start working on tests for this as well. Would definitely love to hear any feedback you have like if you think this is something you would consider adding to Nightwatch. |
c63b654
to
09127f9
Compare
Added tests. I also rebased as my branch was stale. |
In the latest I added jsdoc markup and also a small refactor to With the additional documentation + tests, I'm definitely happy with the feature as it stands.. If there's anything else I can do to aid your review please let me know. |
Sounds great, I'll integrate this soon. On Thu, Apr 30, 2015 at 12:04 AM, Stephanie Madison <
|
Great news! With this latest I made a change to accommodate calling assert module assertions on page objects. LMK if you want me to squash commits before merging in.. |
Yeah, that would be nice On Thursday, April 30, 2015, Stephanie Madison [email protected]
|
6d6c5bf
to
0f0cb18
Compare
Cool all set |
@sknopf would it be ok if I merge this in the next major version, i.e. 0.7? I'm currently working on an another major new feature (new assertion library) and I think it would be nice to also include this as a headliner. Besides, this is a large enough enhancement to increase the version number. |
Yeah definitely. I look forward to the new assertion library.. Also FYI I've been thinking about an improvement that would replace some logic here [1] where you can actually nest elements inside a section instead of combining css selectors of an element and its parent section. That would allow the user to combine xpath and css which they can't do currently. As I understand the elementIdElement endpoint [2] it seems like that should be doable but I still need to test and implement it. If I can get that working do you want me to add the diff here or wait for a later release? |
Yeah, that sounds like a nice improvement.
|
@beatfactor I got the nested elements working in this last commit so you can now combine xpath and css in sections. One thing to note - I create some new protocol actions as well as a useRecurse function. As it is, these are exposed on the client - though maybe they're better off to keep internal to Nightwatch. Anyway let me know your thoughts. |
@sknopf if you want to have a look at the new assertion library it's in the |
The new library is so cool! It's also really nice that it eliminates the need to write a lot of custom assertions that are variants of more common assertions (with negation, waiting, etc). FYI some small things I noticed running the google.js test:
What's the best way for me to incorporate this into the page object? Should I merge it in and update the PR? It looks like it should be pretty simple to add this new library on page objects. Also I was thinking since the common test example is Google I should change the IMDB example I have for page object to Google. I will work on that as well. [1] An error occurred while running the tests: |
Glad you like it. Yeah, I guess that will work for updating the page object branch. I'll look at that problem with the css assertion and update the google example soon. I also created a pull request if you have more comments you can leave them there: https://github.com/beatfactor/nightwatch/pull/456. |
…Elements Selenium protocols
9b79629
to
7714456
Compare
Remove old Google page object example test
Add expect.section to page objects and sections which is an alias for expect.element
@beatfactor just pushed the latest which merges in the Chai assertion branch and gets it working with page objects. I also swapped out the IMDB demo test for a Google test. |
Great, but jshint is failing. |
Hm, I think jshint doesn't like the chai assertion syntax: Fails on assertions like |
From what I saw it was something about a semicolon, but I only had a brief On Wednesday, May 20, 2015, Stephanie Madison [email protected]
|
Ah yes I fixed that - you will see though in the latest travis build it's On Wed, May 20, 2015 at 5:52 PM, Andrei Rusu [email protected]
|
This adds support for enhanced page object model. It doesn't yet include tests or updated documentation, as I wanted to first get feedback.
A couple notable features:
We've been using this for a few weeks internally at Twitter now and it's been working great. Though I'm sure there are some kinks to still get sorted out - one I am aware of supporting custom client commands and assertions whose first argument is not a selector.
@beatfactor let me know what you think.