如何从字符串中输入单个文本单词以便稍后粘贴

问题描述 投票:0回答:2

我正在尝试从弹出窗口中读取一些数据,上面写着

“恭喜,您的参考号 (REF1234) 成功完成”

只需 REF1234 部分,然后将其输入到下一个屏幕上的另一个字段中。

export function addRefToThing() {    
  cy.get('[class="ref-success"]').find(':selected').contains('REF')
    .then(($clipboard) => {
      const copiedRef = $clipboard[0]._vClipboard.clipboardAction.text;

       // <<filler steps, moving around pages>> //

      webpage.RefNo().type(copiedRef)})

看起来它陷入了

-find :selected
,但实际上不确定它是否正在复制我想要的数据。

HTML:

<div class="notification-group" style="width: 300px; bottom: 0px; right: 0px;">
   <span>
      <div data-id="1" class="notification-wrapper" style="transition: all 300ms ease 0s;">
         <div class="notification-template notification success">
            <div class="notification-title">Successful</div>
            <div class="notification-content">Ref (REF123456789) created successfully</div>
         </div>
      </div>
   </span>
</div>
javascript testing automation cypress
2个回答
2
投票

尝试使用正则表达式来解析引用。

使用

/\(REF([0-9]+)\)/
你会得到两场比赛,

  • matches[0] 是整个“(REF123456789)””
  • matches[1] 是内部组“123456789”
cy.get('div.notification-content')
  .then($el => $el.text().match(/\(REF([0-9]+)\)/)[1])  // parsing step
  .then(ref => {
    webpage.RefNo().type(ref)
  })

在你的职能中,

export function addRefToRef() {    
  cy.get('[class="ref-success"]').find(':selected').contains('REF')
    .then(($clipboard) => {
      const copiedRef = $clipboard[0]._vClipboard.clipboardAction.text;
      const innerRef = copiedRef.match(/\(REF([0-9]+)\)/)[1];

      // <<filler steps, moving around pages>> //

      webpage.RefNo().type(innerRef)
    })
}

0
投票

您可以直接保存

notification-content
div 中的内部文本值并在以后使用。您不必使用剪贴板方法。

//Some code that will trigger the notification
cy.get('.notification-content')
  .should('be.visible')
  .invoke('text')
  .then((text) => {
    cy.log(text) //prints Ref (REF123456789) created successfully
  })

如果你想保存它然后在测试中使用它,你可以使用别名

.as
:

//Some code that will trigger the notification
cy.get('.notification-content')
  .should('be.visible')
  .invoke('text')
  .as('notificationText')
//Some other code
cy.get('@notificationText').then((notificationText) => {
  cy.log(notificationText) //prints Ref (REF123456789) created successfully
})

或者,如果您想断言内容:

  1. 完全匹配:
cy.get('.notification-content')
  .should('be.visible')
  .and('have.text', 'Ref (REF123456789) created successfully')
  1. 部分匹配
cy.get('.notification-content')
  .should('be.visible')
  .and('include.text', 'REF123456789')

如果您只想提取参考编号,那么您必须首先使用

split
获取第二个文本,然后使用
slice
删除左括号和右括号,如下所示:

//Some code that will trigger the notification      
cy.get('.notification-content').should('be.visible').invoke('text').as('notificationText')
//Some other code
cy.get('@notificationText').then((notificationText) => {
  cy.log(notificationText.split(' ')[1].slice(1,-1))  //REF123456789 
})
© www.soinside.com 2019 - 2024. All rights reserved.