I'm working on an automated test project with cucumber and protractor, and I want to login on my site with random users using Javascript. I can open my webpage and insert value on login form getting the element (related fields) and sending the values to the form with sendKeys() method:
browser.get('url of the website');
let username_field = element(by.css('.myUserNameField'));
let username_password = element(by.css('.myPasswordField'));
username_field.sendKeys('UsernameRandomicThatIhaveToInsert');
username_password.sendKeys('PasswordForTheRelatedUser');
My users are written in a seperate file like this:
Al,40402342
John,23492893
Jack,39820812
How can I insert randomly the username and the "related" password?
Thank you in advance
fs.readFileSync()
String.split
Math.floor
and Math.random
Read the file:
const fs = require('fs');
const path = require('path');
const userFile = fs.readFileSync(path.resolve('file.txt'), {encoding: 'utf8'});
Create an array from your input file
const users = userFile.split('\n');
Generate a random number and retrieve the user/password combination
const randomUser = () => {
const number = Math.floor(Math.random() * Math.floor(users.length));
const user = users[number].split(',');
return {username: user[0], password: user[1]}
}
Call the function in your test:
const user = randomUser();
username_field.sendKeys(user.username);
username_password.sendKeys(user.password);