Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

javascript - How do I use an array I've created within my Test file

So I have created a js array for a list of postcodes. The array is as below in the code: -

//postcode.js file var postcode = [ "b28 8ND", "b49 6BD", "b28 0ST", "b31 4SU", "B92 9AH", ];

What I need to do is in my test randomly select a postcode for this js file to enter into a text field when running my automation tests. How do I go about doing this? An example would be much appreciated as I can't find much on the internet & I'm quite new to TestCafe & javascript. Below is what I have in my test file: -

//test.js file .click(page.create.withAttribute('mattooltip', 'Create job'))

At this point I need to randomly select 1 of the postcodes from the postcode.js file

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As far as I understood you want to pick a random element from your array

var arr = ['a', 'b', 'c', 'd'];
let randomIndex = Math.floor(Math.random() * arr.length );
alert(arr[randomIndex])

If i am wrong and this is not what you want, please edit your post and explain your question a little bit better


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...