cypress: comparing information in 2 sites

238
August 17, 2019, at 07:10 AM

I'm starting with cypress and need to compare 2 different environments.

I did a script, but is not working properly.

My goal is:

1 - search for a specific selector value at 2 different environments.

2 - get it's value (in both env) , and then compare it if equal.

The below comparision work, but seems very poor code and it stop in first error assert and can't query reference selector, just text.

Any help is appreciated.

describe('Testing Page', function() {
  //urls i need to test
  var relative_urls = [
    '/products/test1',
    '/products/test2',
  ]
  relative_urls.forEach((url) =>  {
  //each url is compared here...
  var productInfo = [];
  //here goes the environments URL.
  var testURL = 'https://www.myurl.com' + url;
  var referenceURL = 'http://test.myurl.com' + url;
  it('Comparing data from url:' + url, function() {
    cy.visit(testURL)
    //get data from selector and add it to array
    cy.get('body').find(".myselector h1").should(($input) => {
      productInfo.push($input.val())
    })
    cy.get('body').find(".myselector h2").should(($input) => {
      productInfo.push($input.val())
    })
    //requesting second url
    cy.request(referenceURL)
    .its('body').should( ($input) => {
      for (var j=0;j<productInfo.length;j++) {
      //expect works, but compares to all site, and i need to search in  a specific selector. 
      //Also, when it gets the first error, it stops and do not search all elements  of array
            expect($input.includes(productInfo[j]), 'Notice: ' + productInfo[j]).to.be.true
          }
        }
      })
   })
 })
})
Answer 1

From reading the documentation, cy.request is really making a plain HTTP request without doing any parsing, which means you basically have to parse the body of the response yourself. cy.visit will actually get the DOM elements so you can use Cypress's query syntax to navigate the page.

I think once you have the element values from the first page you should just do cy.visit again and parse the second page.

EDIT: Apparently you can't use cy.visit cross-domain. In that case, maybe you could try parsing the response body into a DOM node like this:

var el = document.createElement( 'html' );
el.innerHTML = request.body // you can get the request object from cy.request;

Then use el.querySelector to navigate the DOM tree using CSS syntax.

Rent Charter Buses Company
READ ALSO
How can I reference an external javascriptfile in angular?

How can I reference an external javascriptfile in angular?

I am trying to create a sliding menu for my aplication in angular, I would like to reference a javascript fileI have referenced css files in the angular

143
How to properly use a ternary expression with @click event in Vue.js

How to properly use a ternary expression with @click event in Vue.js

I'm showing a list of release versions, but the part I'm stuck on is I want to be able to click the release version and show the job_execs for that versionI'm not sure how to do this other than using a ternary expression and binding it to click event

432
Is it possible to have Testcafe remember an h1 or a text from a previous page so I can match/compare it in another page?

Is it possible to have Testcafe remember an h1 or a text from a previous page so I can match/compare it in another page?

I am trying to do a smoke test on a web page and essentially if I can somehow get Testcafe to remember "Hello World" from page 1 and go to a User Profile to match that same element with the text "Hello World" then I can make it work for all the other cases I will have

140