Javascript cypress:比较两个站点的信息

Javascript cypress:比较两个站点的信息,javascript,cypress,Javascript,Cypress,我从cypress开始,需要比较两种不同的环境 我做了一个脚本,但工作不正常 我的目标是: 1-在两种不同的环境中搜索特定的选择器值 2-获取它的值(在两个环境中),然后比较它是否相等 下面的比较工作正常,但代码似乎非常糟糕,它停止在第一个错误断言中,无法查询引用选择器,只能查询文本 感谢您的帮助 describe('Testing Page', function() { //urls i need to test var relative_urls = [ '/product

我从cypress开始,需要比较两种不同的环境

我做了一个脚本,但工作不正常

我的目标是:

1-在两种不同的环境中搜索特定的选择器值

2-获取它的值(在两个环境中),然后比较它是否相等

下面的比较工作正常,但代码似乎非常糟糕,它停止在第一个错误断言中,无法查询引用选择器,只能查询文本

感谢您的帮助

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
          }
        }
      })
   })
 })
})
description('Testing Page',function()){
//我需要测试的URL
变量相对URL=[
“/products/test1”,
“/products/test2”,
]
相对url.forEach((url)=>{
//这里比较每个url。。。
var productInfo=[];
//下面是环境URL。
var testURL='1〕https://www.myurl.com“+url;
var referenceURL='1〕http://test.myurl.com“+url;
它('比较url中的数据:'+url,函数(){
cy.visit(testURL)
//从选择器获取数据并将其添加到数组
cy.get('body').find('myselector h1').should($input)=>{
productInfo.push($input.val())
})
cy.get('body').find(.myselector h2”).should($input)=>{
productInfo.push($input.val())
})
//请求第二个url
cy.request(referenceURL)
.its('body')。应该($input)=>{

对于(var j=0;j,通过阅读文档,
cy.request
实际上是在不进行任何解析的情况下发出一个简单的HTTP请求,这意味着您基本上必须自己解析响应体。
cy.visit
将实际获取DOM元素,以便您可以使用Cypress的查询语法来导航页面

我认为,一旦从第一页获得了元素值,就应该再次执行
cy.visit
,并解析第二页

编辑:显然,您不能跨域使用
cy.visit
。在这种情况下,您可以尝试将响应正文解析为如下DOM节点:

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

然后使用
el.querySelector
使用CSS语法导航DOM树。

我尝试对不同的域执行cy.visit,但似乎不起作用。我的问题是,没有看到它是跨域的。我使用了另一种解决方案进行了编辑。