最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add query param in Cypress (JS) - Stack Overflow

programmeradmin1浏览0评论

I have to ask for help here. I'm still learning Cypress and trying to add query param to each URL and validate that some modal appears on the page when query param is added. So the way it looks:

urls = [ "/en/link1/", "/en/link2/", "/en/link3/", ]

queryParam = [ "?param1", "?param2", "?param3", ]

I need to write code that would do for me

  • /en/link1/?param1
  • /en/link2/?param1
  • /en/link3/?param1
  • checkCondition()
  • /en/link1/?param2
  • /en/link2/?param2
  • /en/link3/?param2
  • checkCondition()

and so on...

Could you please advise me how to do this? I'd rly appreciate any advice/link to material. Thanks a lot and happy coding!

SOLUTION (kudos to @Alapan Das)

describe("My test", () => {
  urls.forEach(url => {
    params.forEach(param => {
      it(`should check smthng`, () => {
        cy.visit(url + param)
        cy.checkCondition()
      })
    })
  })

I have to ask for help here. I'm still learning Cypress and trying to add query param to each URL and validate that some modal appears on the page when query param is added. So the way it looks:

urls = [ "/en/link1/", "/en/link2/", "/en/link3/", ]

queryParam = [ "?param1", "?param2", "?param3", ]

I need to write code that would do for me

  • /en/link1/?param1
  • /en/link2/?param1
  • /en/link3/?param1
  • checkCondition()
  • /en/link1/?param2
  • /en/link2/?param2
  • /en/link3/?param2
  • checkCondition()

and so on...

Could you please advise me how to do this? I'd rly appreciate any advice/link to material. Thanks a lot and happy coding!

SOLUTION (kudos to @Alapan Das)

describe("My test", () => {
  urls.forEach(url => {
    params.forEach(param => {
      it(`should check smthng`, () => {
        cy.visit(url + param)
        cy.checkCondition()
      })
    })
  })
Share Improve this question edited May 25, 2022 at 12:28 Prokrastinosaurus asked May 25, 2022 at 10:56 ProkrastinosaurusProkrastinosaurus 891 silver badge6 bronze badges 1
  • Instead of editing your question with the solution, you should mark Alapan's answer as the solution. – agoff Commented May 25, 2022 at 14:06
Add a ment  | 

2 Answers 2

Reset to default 3

You can use cy.request version taking options as argument: https://docs.cypress.io/api/mands/request#Options

cy.request({
  url: "/en/link1",
  qs: {
    "param2": "test"
 }
})

You can use nested ForEach Loops to iterate over both arrays and make the binations like this:

const urls = ['/en/link1/', '/en/link2/', '/en/link3/']

const params = ['?param1', '?param2', '?param3']

urls.forEach((url) => {
  params.forEach((param) => {
    console.log(url + param)
  })
  checkCondition()
})

Upon execution:

发布评论

评论列表(0)

  1. 暂无评论