0

In my automated test, I intend to automatically answer quiz and click the "next" button to proceed to the next page after each question is done in each page (questionNum). However, only the first page runs correctly and navigates to the next page, but on the second page, it doesn't perform any operations. I thought that within the "When" block, each row in the examples would be automatically read without needing to write an additional loop. Addtionally, some data in the example is N/A, I also wrote a condition to filter them out. Here is my Feature file

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   |

Here is my condition function and step definition file

//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;
    } 
    
});

The Error Output is

? 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';
         });

Can someone please give me some guidance?

0

2 Answers 2

0

The looping stops because there is no hit with a step definition any more. The N/A is not 'recognized' by {int}. In this case it is best to define a custom parameter type, because otherwise there will be much repetition in the regex (with all the ands in it)

Something like this (not tested):

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

and then replacing the {int}'s in your step definition by {intorna}

The alternative would be writing a regexp in your step definition instead of using cucumber expressions, which would look like (short version):

When(/^User answer question (\d+) by (\d+|N\/A) and (\d+|N\/A)$/, async (questionNum, answer1, answer2)

but you see, this is the point where the repetion of the capture groups starts. That's why the parameter type is more convenient.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your response. However, I encountered another issue when using regex, resulting in the error ' ✖ When User answer question 1 by 1 and 2 and 3 and 4 and 5 and click Next button to the next question Error: function timed out, ensure the promise resolves within 60000 milliseconds' Additionally, it is unable to automatic answer selection on the first page or proceed by clicking the Next button.
Indeed that is another issue. The originial question was a matching issue, this is probably an issue concerning the logic of your step definition code and of the html of the site. You'll have to look in the cypress ui what causes the problem and/or post another question with te relevant information.
Maybe you should post your regex so that we can help. Since the original version of it worked properly, there must be something with the processing of your new matching expression. Can you show the code?
0

I recommend 3 points here.

  1. Cucumber do not recommend 'and' keyword in one cucumber step. 'and' can be used for the continuation steps. Recommend to use how cucumber recommends.
  2. Replace {int} with {string}. I see ans variable being concatenated with xpath in function- answerQuestion. At the end its a string. Hence, try with {string} in step implementation instead of {int}
  3. As you are handling function answerQuestion with var args, recommend to define your data in data table format which cucumber supprts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.