在赛普拉斯中,如何测试一个属性等于多个值之一?

时间:2019-06-13 16:33:55

标签: javascript cypress

我们正在赛普拉斯的网站上进行一些测试。在Windows上的Chrome中,所有测试均通过。但是,在Mac上,某些元素的宽度比Windows宽2px。我了解情况并非如此,我们将对此进行单独调查,但是我想知道的是,您如何测试某个测试的值应在值范围内?

这是Windows中通过的测试:

public void StopImapIdle(bool clientDisconnect) {
        ImapToken.Cancel();
        try {
            Task.Factory.StartNew(() => {
                ImapThreadInfo.Item2?.Join();
            });
            ImapToken.Dispose();

            if (!clientDisconnect) {
                return;
            }

            if (IdleClient.IsConnected && IdleClient.IsIdle) {
                while (true) {
                    if (!IdleClient.IsIdle) {
                        BotLogger.Log("Idling has been stopped.", LogLevels.Trace);
                        break;
                    }
                    BotLogger.Log("Waiting for idle client to stop idling...", LogLevels.Trace);
                }
            }

            lock (IdleClient.SyncRoot) {
                //Error here
                IdleClient.Disconnect(true);
                BotLogger.Log("Imap client has been disconnected.", LogLevels.Trace);
            }
        }
        catch (NullReferenceException) {
            BotLogger.Log("There is no thread with the specified uniqueID", LogLevels.Warn);
        }
        IsAccountLoaded = false;
    }

此版本在Mac上通过。唯一的变化是宽度为private void ImapIdleLoop() { while (!IsCancellationRequested) { Timeout = new CancellationTokenSource(new TimeSpan(0, 9, 0)); try { SetTimeoutSource(Timeout); if (IdleClient.Capabilities.HasFlag(ImapCapabilities.Idle)) { lock (IdleClient.SyncRoot) { IdleClient.Idle(Timeout.Token, CancellationToken); } } else { lock (IdleClient.SyncRoot) { IdleClient.NoOp(CancellationToken); } WaitHandle.WaitAny(new[] { Timeout.Token.WaitHandle, CancellationToken.WaitHandle }); } } catch (OperationCanceledException) { // This means that idle.CancellationToken was cancelled, not the DoneToken nor the timeout. break; } catch (ImapProtocolException) { // The IMAP server sent garbage in a response and the ImapClient was unable to deal with it. // This should never happen in practice, but it's probably still a good idea to handle it. // // Note: an ImapProtocolException almost always results in the ImapClient getting disconnected. IsAccountLoaded = false; break; } catch (ImapCommandException) { // The IMAP server responded with "NO" or "BAD" to either the IDLE command or the NOOP command. // This should never happen... but again, we're catching it for the sake of completeness. break; } catch (SocketException) { } catch (ServiceNotConnectedException) { } catch (IOException) { } finally { // We're about to Dispose() the timeout source, so set it to null. SetTimeoutSource(null); } Timeout?.Dispose(); } } 而不是it('checks Headline container, width & horizontal spacing', () => { cy.get('[data-cy=headline]') .should('have.css', 'width', '569px') })

571px

鉴于实际的569px测试是针对字符串,那么您如何测试宽度是否为两个字符串之一?还是实际值范围?

2 个答案:

答案 0 :(得分:2)

您可以使用closeTo。看起来像这样:

it('checks Headline container, width & horizontal spacing', () => {
  cy.get('[data-cy=headline]')
    .then($element => {
    expect($element.width())
      .closeTo(569, 2)
    })
})

closeTo的第一个数字是希望的数字,第二个数字是空白。例如:如果您有.closeTo(600, 150),则实际数字应该在450到750之间。因此,这不是一个非常具体的检查。

答案 1 :(得分:0)

我认为我为自己的特定用例找到了更好的答案,尽管我确实很喜欢有一个interface IVisitor { void visit(Base b); } 方法可用。

这是我解决问题的方法:

closeTo()

我使用了正则表达式来匹配字符串 it('checks Headline container, width & horizontal spacing', () => { cy.get('[data-cy=headline]') .should('have.css', 'width').and('match', /^(569|571)px/) }) 569px

这使我们能够继续使用571px进行测试,同时确保该值与我们期望的两个特定大小之一匹配。