I'm using webdriver.io with mocha.js and I need to create some action for many times, and I dont want to duplicate my code, so I want to create custom function and call to the function in each mocha test (it)...
for an example:
describe('Register', function () {
it('Login', function (done) {
client
.url('http://exmaple.site.com)
.setValue('input[name="username"]', login.username)
.setValue('input[name="password"]', login.password)
.call(done);
}
it('Login and logout', function (done) {
client
.url('http://exmaple.site.com)
.setValue('input[name="username"]', login.username)
.setValue('input[name="password"]', login.password)
.click('#logout')
.call(done);
}
}
So like you can see here Im duplicate my login codes...
There is any way to create function like login and call it in the test (it):
function login(){
client
.setValue('input[name="username"]', login.username)
.setValue('input[name="password"]', login.password)
}
thanks.
I'm not really sure what your intentions with the login/logout, but here's a generic custom command, webdriver.io custom command
client.addCommand('goGetTitle', function() {
return client
.url('https://www.yahoo.com/')
.getTitle()
});
describe('Test', function() {
it('should have gotten the title', function(done) {
client.goGetTitle().then(function(title) {
console.log('title', title);
})
.call(done);
});
});
try this
function login(){
return client
.setValue('input[name="username"]', login.username)
.setValue('input[name="password"]', login.password)
}
describe('Register', function () {
it('Login', function (done) {
client
.url('http://exmaple.site.com)
.then( login )
.call(done);
}
it('Login and logout', function (done) {
client
.url('http://exmaple.site.com)
.then(login)
.click('#logout')
.call(done);
}
}
basically you will replace your repeated login by .then(login). Because login returning client promise, everything works as excepted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With