Skip to content
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

Add .if_exists()/.if_visible() (implements #266) #267

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,18 @@ Get the URL of the current page.

Determines if a selector is visible, or not, on the page. Returns a boolean.

#### .ifVisible(selector, fn)

Determines if a selector is visible, or not, on the page. Runs a function if it is.

#### .exists(selector)

Determines if the selector exists, or not, on the page. Returns a boolean.

#### .ifExists(selector, fn)

Determines if the selector exists, or not, on the page. Runs a function if it does.

#### .count(selector)

Counts the number of `selector` on the page. Returns a number.
Expand Down
37 changes: 37 additions & 0 deletions lib/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,20 @@ exports.exists = function(selector) {
});
};

/**
* Determine if the selector exists, at least once, on the page and execute function if it does
* @param {string} [selector]
* @param {function} fn
*/
exports.ifExists = function(selector, fn) {
debug('.ifExists()', selector);
return this.count(selector).then(function(count) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should just use the existing .exists() action rather than re-implementing it.

if (count > 0) {
return fn();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should pass the resolution value of the previous Promise to fn.

For example

...
    .return('foobar')
    .ifExists('selectorThatExists', function(res) {
        console.log(res); // Should log 'foobar'
    })

};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make the indentation consistent, as I am neurotic about that.

});
};

/**
* Get the HTML for the page, or optionally for a selector.
* @param {string} [selector]
Expand Down Expand Up @@ -1240,6 +1254,29 @@ exports.visible = function(selector) {
});
};

/**
* Determines if an element is visible and execute function if it is.
* @param {string} selector - The selector to find the visibility of.
* @param {function} fn
*/
exports.ifVisible = function(selector, fn) {
debug('.ifVisible()', selector);
return this
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, you should utilize the existing .visible() action.

.__evaluate(function visible(selector) {
if (window.jQuery) {
return jQuery(selector).is(':visible');
} else {
var elem = document.querySelector(selector);
return elem && (elem.offsetWidth > 0 && elem.offsetHeight > 0);
}
}, selector)
.then(function(vis) {
if (vis) {
return fn();
};
});
};

/**
* Log the output from either a previous chain method,
* or a string the user passed in.
Expand Down
64 changes: 64 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,38 @@ function evaluation(bool) {
.be.false();
});

it('should verify an element exists and execute callback', function() {
var horseman = new Horseman({
timeout: defaultTimeout,
injectJquery: bool
});
var str = 'yo';
return horseman
.open(serverUrl)
.ifExists('a', function(){
return str;
})
.close()
.should.eventually
.equal(str);
});

it('should verify an element does not exists and not execute callback', function() {
var horseman = new Horseman({
timeout: defaultTimeout,
injectJquery: bool
});
var str = 'yo';
return horseman
.open(serverUrl)
.ifExists('yeti', function(){
return str;
})
.close()
.should.eventually
.be.undefined();
});

it('should count the number of selectors', function() {
var horseman = new Horseman({
timeout: defaultTimeout,
Expand Down Expand Up @@ -575,6 +607,38 @@ function evaluation(bool) {
.be.false();
});

it('should determine that an element is visible and execute callback', function() {
var horseman = new Horseman({
timeout: defaultTimeout,
injectJquery: bool
});
var str = 'yo';
return horseman
.open(serverUrl)
.ifVisible('a', function(){
return str;
})
.close()
.should.eventually
.equal(str);
});

it('should determine that an element is not visible and not execute callback', function() {
var horseman = new Horseman({
timeout: defaultTimeout,
injectJquery: bool
});
var str = 'yo';
return horseman
.open(serverUrl)
.ifVisible('.login-popup', function(){
return str;
})
.close()
.should.eventually
.be.undefined();
});

it('should evaluate javascript', function() {
var horseman = new Horseman({
timeout: defaultTimeout,
Expand Down