0
    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?

Boafo
  • 80
  • 5
Pb An
  • 25
  • 4

1 Answers1

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 
          })
        }) 
    })
})
Fody
  • 23,754
  • 3
  • 20
  • 37
  • 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. – Pb An Oct 31 '22 at 08:09
  • 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? – Fody Oct 31 '22 at 08:22
  • 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. – Pb An Nov 01 '22 at 08:29