In this tutorial, we are going to show you how to use a data file in your test.
So right now, in the “elementActions.test” we are entering our username and/or password.
However, we hard coded them into our test here. Instead of this, we can use a data file that will contain the username and password.
So, let us create a folder in the root of our project called “data”.
And in that folder, we're going to create a file call "loginData.js".
module.exports = {
userName: 'Julia',
password: 'Password',
}
We can then go into our test and import the “loginData”.
Now we can say, instead of "Julia", "loginData.userName" and it will enter "Julia" and we do the same for our assert
.
Then for our password we are changing it to "loginData.password", and we are going to change it for our assert
as well.
Let us remove this it.only
.
internetPage = require('../../pages/internet.page')
loginData = require('../../data/logindata')
describe(' Test element actions', function () {
it('should click element', () => {
browser.url('/')
internetPage.clickOnLink()
expect(browser.getUrl()).equals('http://the-internet.herokuapp.com/abtest')
})
it('should get Text', () => {
browser.url('/')
expect(internetPage.getSpecificElementText(1)).equals('A/B Testing')
})
it('should click checkbox', () => {
internetPage.clickLink(6)
internetPage.clickCheckbox(1)
expect(internetPage.checkboxes(1).isSelected()).equals(true)
})
it('should uncheck checkox', () => {
internetPage.clickCheckbox(1)
expect(internetPage.checkboxes(1).isSelected()).equals(false)
})
it('should enter username', () => {
browser.url('${browser.options.baseUrl}/login')
internetPage.enterUsername(loginData.userName)
assert.equal(loginData.userName, internetPage.username.getValue())
})
it('should enter password', () => {
browser.url('${browser.options.baseUrl}/login')
internetPage.enterPassword(loginData.password)
assert.equal(loginData.password, internetPage.password.getValue())
})
it('should clear Value', () => {
internetPage.username.click()
internetPage.username.clearValue()
assert.equal('', internetPage.username.getValue())
})
})
Let us now run our test
npm run test -- --spec ./test/elementActions.test.js
And it enters "Julia" and "Password" and so our test passed.
This is a way that you can use data files so that all tests in your suite are able to access it.
And it saves you the trouble of having to change out all the data, if it is that, for example, your password changed, or you want to use a different username. It is all in one place.
Quiz
The quiz for this chapter can be found in section 6.5