1

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 = 'nedo@go.br'
    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('team@nedo.hi')
    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

  • In your second it block, what does `cy.log(id)` print ? Is it undefined ? – Alapan Das Oct 14 '20 at 18:18
  • 1
    Sharing within the same context might meet your needs. See my answer here: https://stackoverflow.com/a/64359467/2744310 – GrayDwarf Oct 14 '20 at 18:37
  • @AlapanDas I updated the post with the cy.log (id) – Gianmarco Gagliardi Oct 14 '20 at 19:33
  • the problem is that I have to go from a tomorrow A to a domain B. If I put the second piece of code inside it gives me an error: y.visit() failed because you are attempting to visit a URL that is of a different origin. The new URL is considered a different origin because the following parts of the URL are different: > superdomain You may only cy.visit() same-origin URLs within a single test.@GrayDwarf – Gianmarco Gagliardi Oct 14 '20 at 20:44
  • Consider breaking these steps apart into different tests. Check out the cypress documentation regarding web security for this specific issue and possible workarounds. More Info: https://docs.cypress.io/guides/guides/web-security.html – GrayDwarf Oct 14 '20 at 23:14

1 Answers1

0

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 = 'nedo@go.br'
   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('team@nedo.hi')
      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')
            }
         })
      })
   })
})
Alapan Das
  • 17,144
  • 3
  • 29
  • 52
  • in this way the test does not give an error but the var ID is undefined. I should put everything in one usual block. But if I put everything in a usual block where from a domain A I end up in a domain B it gives me an error: failed because you are attempting to visit a URL that is of a different origin. I specify that I put "chromeWebSecurity" in cypress.json: false @AlapanDas – Gianmarco Gagliardi Oct 15 '20 at 08:13
  • cy.get() could not find a registered alias for: @id. You have not aliased anything yet. – Gianmarco Gagliardi Oct 15 '20 at 08:34
  • I have replaced the first It block with a before block. before block always runs the before any it blocks. Mind giving it a try. – Alapan Das Oct 15 '20 at 08:41
  • cy.get() could not find a registered alias for: @id. You have not aliased anything yet. – Gianmarco Gagliardi Oct 15 '20 at 09:28
  • Do you have more than one `it()` blocks or only one `it()` block and have you removed the id variable? – Alapan Das Oct 15 '20 at 09:37