本文介绍了如何使用在测试文件中创建的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我为邮政编码列表创建了一个js数组.数组如下代码所示:-

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

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

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

我需要做的是在我的测试中,为该js文件随机选择一个邮政编码,以便在运行自动化测试时将其输入文本字段.我该怎么做呢?一个例子将不胜感激,因为我在互联网上找不到很多东西.我是TestCafe&的新手. javascript.以下是测试文件中的内容:-

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'))

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

这时,我需要从postcode.js文件中随机选择其中一个邮政编码

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

推荐答案

由于邮政编码"是一个数组,因此可以生成如下所示的随机索引:

Since "postcode" is an array, you can generate a random index as shown below:

var s = 55;
var random = function() {
   s = Math.sin(s) * 10000;
   return s - Math.floor(s);
};
//...
var postIndex = Math.floor(random() * postcode.length);
var currentPost = postcode[postIndex];

例如:

import { Selector } from 'testcafe';

fixture `Getting Started`
    .page `http://devexpress.github.io/testcafe/example`;

const postcode = [
    "b28 8ND", 
    "b49 6BD", 
    "b28 0ST", 
    "b31 4SU",
    "B92 9AH",
];

var s = 55
var random = function() {
    s = Math.sin(s) * 10000;
    return s - Math.floor(s);
};

test('My first test', async t => {

    var postIndex = Math.floor(random() * postcode.length);
    var currentPost = postcode[postIndex];

    console.log(currentPost)

    await t        
        .typeText('#developer-name', currentPost);
});

这篇关于如何使用在测试文件中创建的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 01:26