I am trying to create a login function that I can use with mocha testing and selenium-webdriver for unit testing, since I have a bunch of things to do that start with "user logs in and..."
Below is my best shot, but when I run it, the console just logs
Already registered user logs in and sees homepage
1) should work (it's red text)
Login an existing user
and then nothing happens and I have to control-C out of the process. My code was working before I went for this modular login implementation, so I'm sure that's where my problem is. Is there something I am missing here? I am also open to general critiques of how I went about making the login function, since I don't have that much experience with webdriver.
Below is my code. It's in one js file.
var test = require('selenium-webdriver/testing'),
chai = require('chai');
chai.use(require('chai-string'));
chai.use(require('chai-as-promised'));
var expect = chai.expect,
webdriver = require('selenium-webdriver'),
By = webdriver.By;
// I want this to be a modular login function I can use multiple places
function login(driver) {
test.describe('Login an existing user', function() {
test.it('should work', function() {
this.timeout(0);
var email = 'myemail@test.com';
var password = 'password';
driver.get('http://localhost:9000');
driver.getTitle().then(function(title) {
expect(title).to.equal('my title');
})
.then(function() {
// do login stuff and click login button
});
.then(function() {
return driver;
});
});
});
}
//an example of when I would use the login function
test.describe('Already registered user logs in and sees homepage', function() {
test.it('should work', function() {
this.timeout(0);
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();
driver = login(driver)
.then(function() {
driver.findElement(By.xpath("relevant xpath")).click();
})
})
})