Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs Cypress无法通过它查找内部文本';肯定有_Reactjs_Cypress - Fatal编程技术网

Reactjs Cypress无法通过它查找内部文本';肯定有

Reactjs Cypress无法通过它查找内部文本';肯定有,reactjs,cypress,Reactjs,Cypress,我正在测试一个错误横幅,它在屏幕上和屏幕下都有动画。我测试它是否可见,这是有效的。但我似乎无法测试它的内容 这里是元素 const errorStr = error && `ERROR: ${ error.message } (${ error.code })`; const warningStr = warning && `NOTIFICATION: ${ warning }.`; <Transition in={ !!(error || war

我正在测试一个错误横幅,它在屏幕上和屏幕下都有动画。我测试它是否可见,这是有效的。但我似乎无法测试它的内容

这里是元素

  const errorStr = error && `ERROR: ${ error.message } (${ error.code })`;
  const warningStr = warning && `NOTIFICATION: ${ warning }.`;

<Transition in={ !!(error || warning) } timeout={ 0 }>{ state =>
  <Wrapper style={ transitionStyles[state] } data-testid={ 'ErrorBanner' }>
    {/*<div data-testid={'BannerError'} children={ errorStr }/>*/ }
    <div data-testid={ 'BannerError' } data-error={ error?.code }>{ errorStr }</div>
    <div data-testid={ 'BannerNotification' } children={ warningStr }/>
  </Wrapper>
}</Transition>
您会注意到,在
然后
块中,我记录
el
el[0]

当我展开第一个属性(
>0:div
)时,innerHTML和textContent属性都显示我期望的文本。当我点击正在传递的关于它可见的断言时也是如此

但是测试失败,因为
预期“”为[我想要的任何文本]
。如图所示,打印
el[0]
返回时是一个空div

我确实尝试过
cy.contains
cy.getByTestId(message).contains
但都不起作用,所以我尝试了这个更明确的方法


怎么了?

我想问题可能是将值保存到
常量文本中。
如果您尝试:

cy.wrap(el[0].textContent).should('eq', message)
如果不起作用,试试这个(对我有用):

如果有帮助,请告诉我

.then()
更改为
.should()
以允许Cypress重试
expect()
。控制台日志有延迟计算,显示的值可能在转换之后(可能有一个图标告诉您这一点)。
  cy.getByTestId('BannerError').invoke('text').then((errorText) => {
     cy.wrap(errorText).should('eq', message)
  });