我是 stackoverflow 的新手,所以很抱歉,如果這有點到處都是,但我似乎無法在網上找到任何簡單的答案,這似乎是一項很小的任務!:(
我正在嘗試從彈出視窗中復制一些資料,上面寫著“恭喜,您的參考 (REF1234) 已成功完成”(只是 REF1234 部分)并將其復制粘貼到下一個螢屏上的另一個欄位中......這是我到目前為止所擁有的,希望對某人有意義......
export function addRefToThing() {
cy.get('[]').find(':selected').contains('REF').then(($clipboard) => {
const copiedRef = $clipboard[0]._vClipboard.clipboardAction.text;
// <<filler steps, moving around pages>> //
webpage.RefNo().type(copiedRef)})
這幾乎是我能夠得到并拉出我的頭發......看起來它被趕上了-find :selected但實際上不確定它甚至復制我想要的資料......
我對 cypress 還很陌生,所以事實證明這很令人困惑,如果有人有關于這種請求的任何好的培訓材料,那就太棒了!提前致謝!
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>
uj5u.com熱心網友回復:
嘗試使用正則運算式決議 ref。
使用/\(REF([0-9] )\)/你得到兩場比賽,
- 匹配[0]是整個“(REF123456789)”“
- 匹配 [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('[]').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)
})
}
uj5u.com熱心網友回復:
您可以直接從notification-contentdiv 中保存內部文本值并稍后使用。您不必使用剪貼板方法。
//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
})
如果您想保存它然后在測驗中稍后使用它,您可以使用 alias .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
})
或者,如果您想斷言內容:
- 完全符合:
cy.get('.notification-content')
.should('be.visible')
.and('have.text', 'Ref (REF123456789) created successfully')
- 部分匹配
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
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/341085.html
標籤:javascript 测试 自动化 柏 复制粘贴
