Cypress problems take value and compare it. Scope variable

283 views Asked by At

I have this HTML structure:

<tr id="post-7053" class="iedit author-other level-0 post-7053 type-poi status-publish hentry webmapp_category-corbezzolo" data-id="7053">
   <th scope="row" class="check-column">
      <label class="screen-reader-text" for="cb-select-7053">
      Seleziona 594         </label>
      <input id="cb-select-7053" type="checkbox" name="post[]" value="7053">
      <div class="locked-indicator">
         <span class="locked-indicator-icon" aria-hidden="true"></span>
         <span class="screen-reader-text">
         “594” è bloccato               </span>
      </div>
   </th>
   <td class="5da0bb937bd9f column-5da0bb937bd9f has-row-actions column-primary column-postid" data-colname="ID">7053

I have to take the value of an ID and compare it on another site:

enter image description here

i have to get the first table id i managed to get it with this cypress command:

  id = cy.get('tbody#the-list td').first().invoke('val')

only that when I go to compare the value of the variable id. it never enters the if branch. While if I put a value like 7156 or other it enters the if branch and makes the comparison.

below the test code:

describe('Registration', () => {
    const email = '[email protected]'
    const password = 'pedhu'
    var id

  it('create new Nedo', () => {
      cy.visit('https://test.nedo/wp-admin')
      cy.get('input[name=log]').type(email)
      cy.get('input[name=pwd]').type(password)
      cy.get('input#wp-submit').click()

      cy.visit('https://test.nedo/edit.php?post_type=nedo')

      id = cy.get('tbody#the-list td').first().invoke('val')

   })

   it('id', () => {
    cy.visit('https://nedostaging.z.hu/login')
    cy.get('input[name=email]').type('[email protected]')
    cy.get('input[name=password]').type('nedo')
    cy.get('button').contains('Login').click()
    cy.get('#hometable > tbody > tr > td:nth-child(4)').each(($e, index, $list) => {
        const text = $e.text()
        cy.log(id)
        if (text.includes(id)) {//if I put a number instead of id it works
            assert.strictEqual(text, '{"id":'+id+'}', 'id nedo ok')
        }

    })

  })

cy.log(id):

enter image description here

1

There are 1 answers

5
Alapan Das On

For handling same-origin policies, you can write "chromeWebSecurity": false in your cypress.json file. But this will only work with the chrome browser.

describe('Registration', () => {
   const email = '[email protected]'
   const password = 'pedhu'

   before(() => {
      cy.visit('https://test.nedo/wp-admin')
      cy.get('input[name=log]').type(email)
      cy.get('input[name=pwd]').type(password)
      cy.get('input#wp-submit').click()
      cy.visit('https://test.nedo/edit.php?post_type=nedo')
      cy.get('tbody#the-list td').first().invoke('val').as('id')
   })
  
   it('id', () => {
      cy.visit('https://nedostaging.z.hu/login')
      cy.get('input[name=email]').type('[email protected]')
      cy.get('input[name=password]').type('nedo')
      cy.get('button').contains('Login').click()
      cy.get('@id').then((id) => {
         cy.get('#hometable > tbody > tr > td:nth-child(4)').each(($e, index, $list) => {
            const text = $e.text()
            cy.log(id)
            if (text.includes(id)) { //if I put a number instead of id it works
               assert.strictEqual(text, '{"id":' + id + '}', 'id nedo ok')
            }
         })
      })
   })
})