2

Forgive a beginners question. I am learning JS to help with my Cypress automation skills - this may be a bad solution and I'm happy to learn a better way.

Given a json file versions.json

{
"cypress": "1.1.0",
"playwright": "1.1.0",
"seleniumJava": "1.1.0",
"geb": "1.1.0",
"postman": "1.1.0"
}

I need to iterate through each url, visit the relevant release page and get the latest tool version.

My specific question is, how do I pass the tool from the tools array so I can interpolate it into here:

let version = data.{tool}version;...to allow cypress to know which tool version to get?

   //env vars to get the relevant urls here
    const urls = [cypressBaseUrl, playwrightBaseUrl, seleniumJavaBaseUrl, gebBaseUrl, postmanBaseUrl]
    const tools = ["cypress", "playwright", "seleniumJava", "geb", "postman"]

    it("Gets the latest release versions", () => {
        urls.forEach(function (url) {
            tools.forEach(function (tool) {
                cy.readFile('cypress/fixtures/versions.json').then((data) => {
                    let version = data.{tool}version; //how do I pass each tool here?
                    let updated
                    cy.visit(url)
                    cy.get('div.nuxt-content')//refactor to work for all tools
                        .first('h2')
                        .then(txt => {
                         const versionTxt = txt.find("h2").text()//get latest version
                    versionTxt = version 
                    cy.writeFile('cypress/fixtures/versions.json', {
                                tool: versionTxt
                            })
                        })
                })
            })
        })
    })
})

1 Answer 1

1

if you're trying to access version values from versions.json and tools contains the keys in versions.json, you can use array notation to pull out the values:

tools.forEach(function (tool) {
  cy.readFile('cypress/fixtures/versions.json').then((data) => {
    let version = data[tool] //will hold whatever value `versions.json` has for the given `tool` key

    cy.visit(url)
    cy.get('div.nuxt-content')
      .first('h2')
      .then(txt => {
        const versionTxt = txt.find("h2").text() //get latest version
        versionTxt = version 
        cy.writeFile('cypress/fixtures/versions.json', {
          tool: versionTxt
        })
      })
  })
})
// ...

1

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.