赛普拉斯(Cypress):每个循环的request()问题

时间:2020-09-07 08:03:02

标签: javascript node.js regex loops cypress

我正在与Cypress一起进行测试框架的开发,并且遇到了一个问题,我尝试执行一个each循环,而每次迭代都在一个request上执行一个<a> tag函数,然后断言响应中包含来自regex表达式的特定值。我可以看到它开始执行请求链接,但是显然响应的主体很大,以至于浏览器停止以可视方式显示Cypress UI冻结或似乎没有响应而运行。 (该测试可能仍在运行,但是Cypress UI似乎被冻结或非常矮胖。)

我尝试过寻找有关此问题的解决方案,但没有任何解决方案可以替代我的困境。我曾考虑过为每个循环使用a来遍历标签,但是我认为现在的情况会更慢,更糟。与赛普拉斯合作时,有没有人遇到过这个问题?

代码:功能测试,用于查看和验证列表的内容链接

const steamHeader = new SteamGlobalHeader(cy);

steamHeader.getActionSection().get('#language_dropdown')
            .within(() => {
                const langList = '/^schinese|tchinese|japanese|koreana'+
                '|thai|bulgarian|czech|danish|german|spanish|latam|greek'+
                '|french|italian|hungarian|dutch|norwegian|polish|portuguese'+
                '|brazilian|romanian|russian|finnish|swedish|turkish'+
                '|vietnamese|http://translation.steampowered.com$/';
                steamHeader.get('a[class="popup_menu_item tight"]').should('have.length', 28)
                .each(($a) => {
                    steamHeader.inspectRequestURL(steamHeader.get($a), 'href', 'body', langList);
                });

            });

inspectRequestURL()的函数(使用赛普拉斯的POM基础页的一部分)

inspectRequestURL(givenValue, propValue, requestSection, requestTarget){
        givenValue.then(($a) => {
            const prop = $a.prop(propValue);

            this.cy.request(prop).its(requestSection).should('include', requestTarget);
        })
    }

1 个答案:

答案 0 :(得分:0)

虽然这可能不是每个人的解决方案。有人建议我创建一个任务(cy.task()),并使用Node js中的代码来处理请求。对于我的请求,我使用axios包来处理get请求,如果内容与值以及状态码匹配,则返回一个布尔值。

编辑:但是,我仍然看到断言失败,原因是列表中途或结尾附近超时。几天后,我在Steam网站上再次进行了重新测试,现在我的测试运行可以在1.5分钟内顺利完成。 我确实创建了一种替代方法,可以一次对所有链接执行请求,而无需经历迭代。 (完成时间至少快了10秒。)

编辑:(旧方法): 在cypress / plugins / index.js中找到的任务:

const axios = require('axios').default;
 // use axios
module.exports = (on, config) => {
  // `on` is used to hook into various events Cypress emits
  // `config` is the resolved Cypress config
  on('task', {
    //Work on request and return, learn what gets returned
    getURLBodyResponseContains: ({href, target}) => {
        return axios.get(href)
          .then( (response) => {
            //console.log('target: '+ target);
            if (response.data.match(target)) {
              return { answer: true, status: response.status };
            } else {
              return { answer: false, status: response.status };
            }
          })
          .catch((error) => console.log(error));
          
          
    }
})

测试代码:

it.only('Test Lanuage list', () => {
        const steamHeader = new SteamGlobalHeader(cy);

        steamHeader.getActionItem('span', 'language').click();

        steamHeader.getActionSection().get('#language_dropdown')
            .within(() => {
                const langList = /^schinese|tchinese|japanese|koreana|thai|bulgarian|czech|danish|german|spanish|latam|greek|french|italian|hungarian|dutch|norwegian|polish|portuguese|brazilian|romanian|russian|finnish|swedish|turkish|vietnamese|Steam Translation Server/;
                steamHeader.get('a[class="popup_menu_item tight"]').as('languageLinks');
                steamHeader.get('@languageLinks').should('have.length', 28);
                
                steamHeader.get('@languageLinks').each(($a) => {
                    steamHeader.getCy().task('getURLBodyResponseContains', {href: $a.prop('href'), target: langList}, {timeout: 100000}).as('returnValue');
                    steamHeader.get('@returnValue').its('status').should('equal', 200);
                    steamHeader.get('@returnValue').its('answer').should('be.true');
                    
                });
            });

    });

新方法: 几天后,我重新测试了我的测试代码,现在我并没有频繁遇到超时问题。在试图找出替代方案时,我想出了一种对所有请求一次而不是一个接一个地执行请求的方法。完成所有请求后,将结果作为一个数组返回,然后执行forEach循环,包装对象的属性并运行一组断言。使用这种方法,我注意到测试时间快了10秒。

任务代码:

const axios = require('axios').default;
 // use axios
module.exports = (on, config) => {
  // `on` is used to hook into various events Cypress emits
  // `config` is the resolved Cypress config
  on('task', {
    //Work on request and return, learn what gets returned
    getURLBodyResponseContains: ({href, target}) => {
        return axios.get(href)
          .then( (response) => {
            //console.log('target: '+ target);
            if (response.data.match(target)) {
              return { answer: true, status: response.status };
            } else {
              return { answer: false, status: response.status };
            }
          })
          .catch((error) => console.log(error));  
    },

    getURLListBodyResponseContains: ({hrefList, target}) => {
     return Promise.all(hrefList.map((href) => {
// Would have wanted to reuse the task from above, but not sure you could reuse a  task within a task.
        return checkIfRequestContains(href, target);
      }))
        .then((result) => {
          return result;
        })
        .catch((error) => console.log(error));
    }
  })
}

function checkIfRequestContains(url, target){
  return axios.get(url)
  .then( (response) => {
    //console.log('target: '+ target);
    if (response.data.match(target)) {
      return { answer: true, status: response.status };
    } else {
      return { answer: false, status: response.status };
    }
  })
  .catch((error) => console.log(error)); 
}

测试代码:

it('Test Language list', () => {
        const steamHeader = new SteamGlobalHeader(cy);

        steamHeader.getActionItem('span', 'language').click();

        steamHeader.getActionSection().get('#language_dropdown')
            .within(() => {
                const langList = /^schinese|tchinese|japanese|koreana|thai|bulgarian|czech|danish|german|spanish|latam|greek|french|italian|hungarian|dutch|norwegian|polish|portuguese|brazilian|romanian|russian|finnish|swedish|turkish|vietnamese|Steam Translation Server/;
                steamHeader.get('a[class="popup_menu_item tight"]').as('languageLinks');
                steamHeader.get('@languageLinks').should('have.length', 28);

                //This using task on whole array.
                let urlList = [];
                steamHeader.get('@languageLinks').each(($a) => {
                    urlList.push($a.prop('href'));
                });
                steamHeader.getCy().task('getURLListBodyResponseContains', {hrefList: urlList, target: langList}, {timeout: 100000}).as('returnValue');
                steamHeader.get('@returnValue').then(($value) => {
                    console.log($value);
                    $value.forEach((val) => {
                        console.log(val);
                        steamHeader.getCy().wrap(val).its('status').should('eq', 200);
                        steamHeader.getCy().wrap(val).its('answer').should('be.true');
                    })
                }); 


                //This approach using each loop
               /* steamHeader.get('@languageLinks').each(($a) => {
                    steamHeader.getCy().task('getURLBodyResponseContains', {href: $a.prop('href'), target: langList}, {timeout: 100000}).as('returnValue');
                    steamHeader.get('@returnValue').its('status').should('equal', 200);
                    steamHeader.get('@returnValue').its('answer').should('be.true');
                    
                }); */
            });

    })