Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
How to reference a JS array value and interpolate into JSON

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
                            })
                        })
                })
            })
        })
    })
})
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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
        })
      })
  })
})
// ...

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda