簡體   English   中英

Javascript-使用隨機用戶登錄

[英]Javascript - Login with randomic user

我正在使用黃瓜和量角器進行自動化測試項目,我想使用Javascript與隨機用戶一起登錄我的網站。 我可以打開網頁,並在登錄表單上插入值以獲取元素(相關字段),然后使用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');

我的用戶寫在這樣一個單獨的文件中:

Al,40402342
John,23492893
Jack,39820812

如何隨機插入用戶名和“ 相關 ”密碼?

先感謝您

  1. 使用fs.readFileSync()讀取文件
  2. 使用String.split在換行符處拆分讀取的字符串
  3. 使用Math.floorMath.random插入隨機的用戶/密碼對

讀取文件:

const fs = require('fs');   
const path = require('path'); 
const userFile = fs.readFileSync(path.resolve('file.txt'), {encoding: 'utf8'});

從您的輸入文件創建一個數組

const users = userFile.split('\n');

生成一個隨機數並檢索用戶/密碼組合

const randomUser = () => {
    const number = Math.floor(Math.random() * Math.floor(users.length));
    const user = users[number].split(',');

    return {username: user[0], password: user[1]}
}

在測試中調用該函數:

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