简体   繁体   English

Javascript-使用随机用户登录

[英]Javascript - Login with randomic user

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. 我正在使用黄瓜和量角器进行自动化测试项目,我想使用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: 我可以打开网页,并在登录表单上插入值以获取元素(相关字段),然后使用sendKeys()方法将值发送到表单:

 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 先感谢您

  1. Read the file using fs.readFileSync() 使用fs.readFileSync()读取文件
  2. Split the read string at newlines using String.split 使用String.split在换行符处拆分读取的字符串
  3. Insert a random user/password pair using Math.floor and Math.random 使用Math.floorMath.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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM