无法循环 cucumber.js 示例表中的所有行

问题描述 投票:0回答:1

在我的自动化测试中,我打算自动回答测验,并在每个页面(questionNum)完成每个问题后单击“下一步”按钮进入下一页。但是,只有第一页正确运行并导航到下一页,但在第二页上,它不执行任何操作。我认为在“When”块中,示例中的每一行都会自动读取,而不需要编写额外的循环。另外,示例中有些数据是N/A,我也写了一个条件来过滤掉它们。 这是我的功能文件

When User answer question <questionNum> by <answer1> and <answer2> and <answer3> and <answer4> and <answer5> and click Next button to the next question
         Examples:
         | questionNum | answer1 | answer2 | answer3 | answer4 | answer5 |
         |      1      |    1    |    2    |    3    |    4    |    5    |
         |      2      |    1    |    2    |   N/A   |   N/A   |   N/A   |

这是我的条件函数和步骤定义文件

//Condition function
async function answerQuestion(questionNum, ...answers) {
    await World.driver.sleep(1500);
    for (let i = 0; i < answers.length; i++) {
        const ans = answers[i];
        if (ans !== 'N/A') {
            const xpath=`(//input[@name='row-radio-buttons-group'])[${6*i + ans}]`;
            const element = await World.driver.findElement(By.xpath(xpath));
            // Scroll to the element
            await World.driver.executeScript("arguments[0].scrollIntoView();", element);
            // Wait for the element to be clickable
            await World.driver.wait(until.elementLocated(By.xpath(xpath)), 15000); // Wait for element to be located
            await World.driver.wait(async () => {
                try {
                    await element.click();
                    return true;
                } catch (e) {
                    return false;
                }
            }, 10000); // Wait for element to be clickable and click on it

        }
    }
    await World.driver.sleep(5000);

//Step_Definition
When('User answer question {int} by {int} and {int} and {int} and {int} and {int} and click Next button to the next question', async (questionNum, answer1, answer2, answer3, answer4, answer5) => {
    await answerQuestion(questionNum, answer1, answer2, answer3, answer4, answer5);
    try{
        const nextButtonXpath = `//div[@id='root']/div/main/div[2]/div/div[2]/div/div/div[3]/div/button${questionNum === 1 ? "[2]" : "[3]"}`;
        await World.driver.findElement(By.xpath(nextButtonXpath),5000).click();

    } catch(error){
        console.error('Error occurred while clicking the Next button', error.message);
        throw error;
    } 
    
});

错误输出是

? When User answer question 2 by 1 and 2 and N/A and N/A and N/A and click Next button to the next question
       Undefined. Implement with the following snippet:
       
         When('User answer question {int} by {int} and {int} and N\\/A and N\\/A and N\\/A and click Next button to the next question', function (int, int2, int3) {
         // When('User answer question {int} by {int} and {float} and N\\/A and N\\/A and N\\/A and click Next button to the next question', function (int, int2, float) {
         // When('User answer question {int} by {float} and {int} and N\\/A and N\\/A and N\\/A and click Next button to the next question', function (int, float, int2) {
         // When('User answer question {int} by {float} and {float} and N\\/A and N\\/A and N\\/A and click Next button to the next question', function (int, float, float2) {
         // When('User answer question {float} by {int} and {int} and N\\/A and N\\/A and N\\/A and click Next button to the next question', function (float, int, int2) {
         // When('User answer question {float} by {int} and {float} and N\\/A and N\\/A and N\\/A and click Next button to the next question', function (float, int, float2) {
         // When('User answer question {float} by {float} and {int} and N\\/A and N\\/A and N\\/A and click Next button to the next question', function (float, float2, int) {
         // When('User answer question {float} by {float} and {float} and N\\/A and N\\/A and N\\/A and click Next button to the next question', function (float, float2, float3) {
           // Write code here that turns the phrase above into concrete actions
           return 'pending';
         });

有人可以给我一些指导吗?

javascript automated-tests cucumber gherkin cucumberjs
1个回答
0
投票

循环停止,因为不再有步骤定义的命中。 N/A 未被 {int}“识别”。 在这种情况下,最好定义自己的参数类型,因为否则正则表达式中会有很多重复(其中包含所有的 and )

类似这样的东西(未测试):

defineParameterType({
  name: "intorna",
  regexp: /(\d+|N\/A)/,
  transformer(s) {
        return (s);
  },
});

然后将步骤定义中的 {int} 替换为 {intorna}

© www.soinside.com 2019 - 2024. All rights reserved.