简体   繁体   English

赛普拉斯比较两个字符串

[英]Cypress compare two strings

I have an h1 tag in HTML that contains the actual time (year, month, day and time) updated each 6 seconds and it works.我在 HTML 中有一个 h1 标签,其中包含每 6 秒更新一次的实际时间(年、月、日和时间),它可以工作。 I need to check that after six seconds the time is changed.我需要检查六秒钟后时间是否改变。 The problem is that I tried the following code:问题是我尝试了以下代码:

  var t1;
  cy.get("h1").should(($d) => {
    t1 = $d.text();
    });
    cy.wait(600);
    cy.get("h1").should(($d) => {
        const t2 = $d.text();
        expect(t2).not.equal(t1);
    })

But the first variable (t1) is always an empty string "", while the second one take the time.但是第一个变量(t1)总是一个空字符串“”,而第二个变量需要时间。 The test passes but for the wrong reason ("" is different from "2021-11-09 10:52:33") How can I take also the first time?测试通过但原因错误(“”与“2021-11-09 10:52:33”不同)我如何才能第一次参加?

Update更新
I tried to show only the first timestamp, and it works with this code:我试图只显示第一个时间戳,它适用于以下代码:

  cy.get("h1").should(($d) => {
    const t1 = $d.text();
    expect(t1).not.equal("");
    })

But I need to compare it with the second one, not with the empty string.但我需要将它与第二个进行比较,而不是与空字符串进行比较。 I think it is because Cypress is async, but I don't know how to connect the second part.我认为这是因为 Cypress 是异步的,但我不知道如何连接第二部分。

I solved the problem using var for each timestamp and not const.我为每个时间戳使用 var 而不是 const 解决了这个问题。 With var it can take both timestamp and compare them:使用 var 它可以同时获取时间戳并进行比较:

  var t1;
  var t2;
  cy.get("h1").should(($d) => {
    t1 = $d.text();
    expect(t1).not.equal("");
    })
    
    cy.wait(6000);
    cy.get("h1").should(($d) => {
        t2 = $d.text();
        expect(t2).not.equal(t1);
    })

It works only if I check for the first timestamp that is different from "", I don't really know why, but it's okay because in this way I verify that the timestamp works.它仅在我检查与“”不同的第一个时间戳时才有效,我真的不知道为什么,但没关系,因为通过这种方式我验证了时间戳是否有效。 In addition 6 seconds are 6000 and not 600 ms.另外 6 秒是 6000 而不是 600 毫秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM