【问题标题】:How do I write json value using variable in cypressHow do I write json value using variable in cypress
【发布时间】:2022-12-27 14:17:12
【问题描述】:
    it ('check link', () => {
        cy.visit(/)

        var link1_value
        var link2_value
        var datasheet_value

        cy.get('[class="classname"]').then(($link1) => {
            if ($link1.find('[data-type="link1"]').length>0){
                cy.get('[data-type="link1"]').invoke('text').as('link1')
                cy.get('@link1').then((link1) => {
                    link1_value = 'Link1:' + link1

                })
            }
            else {
                link1_value= '0'
            }

  
                }) //there's another one like this for link2

                cy.writeFile('txt.json', { link1: link1_value ,link2: link2_value })
})

        

The code above does not work because link1_value does not have any data. How should I add the value so that it will show in the json file?

【问题讨论】:

    标签: json cypress fixtures test-fixture


    【解决方案1】:

    To start with, wrap the cy.writeFile() in a .then().

    it('check link', () => {
      cy.visit('/')
    
      let link1_value
      let link2_value
      let datasheet_value
    
      cy.get('[class="classname"]').then(($link1) => {
        if ($link1.find('[data-type="link1"]').length > 0) {
          cy.get('[data-type="link1"]').invoke('text').as('link1')
          cy.get('@link1').then((link1) => {
            link1_value = 'Link1:' + link1
          })
        }
        else {
          link1_value = '0'
        }
      }) 
      .then(() => {       // delays the writeFile until after above code finishes
    
        cy.writeFile('txt.json', { link1: link1_value, link2: link2_value })
      })
    })
    

    You can also try with the cypress-if package

    it('check link', () => {
      cy.visit('/')
    
      let link1_value
      let link2_value
      let datasheet_value
    
      cy.get('[class="classname"]')
        .find('[data-type="link1"]')
        .if()
        .then($link1 => link1_value = $link1.text())
        .else()
        .then(() => link1_value = '0')
        .finally(() => {
          cy.writeFile('txt.json', { link1: link1_value, link2: link2_value })
        })
    })
    

    Without the variables (passing results down the chain)

    it('check link', () => {
      cy.visit('/')
    
      cy.get('[class="classname"]')
        .find('[data-type="link1"]')
        .if()
        .then($link1 => $link1.text())
        .else()
        .then(() => '0')
        .finally(link1_value => {
          cy.writeFile('txt.json', { link1: link1_value, link2: link2_value })
        })
    })
    

    With the 2nd link

    const getLink = (link) => {
      return cy.get('[class="classname"]')
        .find(`[data-type="${link}"]`)   // backticks to insert "link" parameter
        .if()
        .then($link => $link.text())
        .else()
        .then(() => '0')
    }
    
    it('check link', () => {
      cy.visit('/')
    
      getLink('link1')
        .then(link1_value => {
          getLink('link2')
            .then(link2_value => {
              cy.writeFile('txt.json', { 
                link1: link1_value, 
                link2: link2_value 
              })
            }) 
        })
    })
    

    【讨论】:

    • Hi Fody! Great answer! However, I want to make sure that my data will be: [ { "link1": "link1_value", "link2": "link1_value", }, { "link1": "link1_value2", "link2": "link1_value2", } ]. In my spec file, I will be using "forEach" because I need to run this for 100+ links.
    • Wow, then it might need a better way to make the getLink() calls - I'll see how that can be changed. How many objects in the array - just two? Each object has all 100 links?
    • Your solution actually worked! The only problem that I have is that when I append the results to the json file by using {flag:"a+"}, the formatting is not correct. It shows {link1 result}{link 2 result} instead of {link1 result},{link2 result}. See that there's no comma. Nonetheless, there are easy workarounds here but I was hoping to fix how i append the results in json format.
    猜你喜欢
    • 2022-11-20
    • 2022-01-04
    • 2022-12-01
    • 2022-12-27
    • 2022-12-01
    • 2022-12-01
    • 2021-11-18
    • 2022-12-01
    • 2022-12-01
    相关资源
    最近更新 更多