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 .authentication() #52

Closed
wants to merge 20 commits into from
Closed
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
12 changes: 12 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ Saves a screenshot of the current page to the specified `path`. Useful for debug
#### .useragent(useragent)
Set the `useragent` used by PhantomJS. You have to set the useragent before calling `.goto()`.

#### .authentication(user, password)
Set the `user` and `password` for accessing a web page using basic authentication. Be sure to set it before calling `.goto(url)`.

```js
new Nightmare()
.authentication('myUserName','myPassword')
.goto('http://httpbin.org/basic-auth/myUserName/myPassword')
.run(function( err, nightmare){
console.log("done");
});
```

#### .viewport(width, height)
Set the `width` and `height` of the viewport, useful for screenshotting. Weirdly, you have to set the viewport before calling `.goto()`.

Expand Down
16 changes: 16 additions & 0 deletions lib/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,22 @@ exports.on = function( eventType, callback, done ){
this.page.set(pageEvent, callback, done);
}

/*
* Sets up basic authentication.
*
* @param {String} user
* @param {Function} password
*/

exports.authentication = function(user, password, done) {
var self = this;
this.page.get('settings', function( settings ){
settings.userName = user;
settings.password = password;
self.page.set('settings', settings, done);
});
};

/**
* Set the useragent.
*
Expand Down
12 changes: 12 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,18 @@ describe('Nightmare', function(){
})
.run(done);
});

it('should set authentication', function(done) {
new Nightmare()
.authentication('my','auth')
.goto('http://httpbin.org/basic-auth/my/auth')
.evaluate(function(){
return document.body.innerHTML;
}, function( data ){
data.length.should.be.above(0);
})
.run(done);
});

it('should set viewport', function(done) {
var size = { width : 400, height: 1000 };
Expand Down