*我需要知道所有這些列出的資料都是按 XXXX 數字排序的
uj5u.com熱心網友回復:
執行此操作的步驟是
- 獲取選項元素
- 將文本提取為陣列
- 切掉數字部分
- 將成員與排序版本進行比較
cy.get('.MuiAutocomplete-root').click() // open the options list
cy.get('.MuiAutocomplete-option')
.should('have.length.gt', 1) // wait for loading options
.then($options => { // 1. get the option elements
const texts = [...$options].map(option => option.innerText) // 2. extract the text as an array
const numbers = texts.map((text) => (text.split('-')[0].trim())) // 3. slice off the number part
expect(numbers).to.have.ordered.members([...numbers].sort()) // 4. compare members
// to sorted version
})
不要使用 .sort()
這會給你錯誤的答案,例如
const strings =['b', 'a', 'c']
cy.wrap(strings).should('equal', strings.sort()) // passes, but it's not sorted!
因為strings.sort()更改了原始陣列,然后將其與自身進行比較,所以即使原始串列未排序,也會始終通過。
uj5u.com熱心網友回復:
我建議使用chai-sorted庫來避免復雜性和錯誤。
安裝
npm install chai-sorted
or
yarn add -D chai-sorted
測驗是
chai.use(require("chai-sorted"))
cy.get('.MuiAutocomplete-option')
.should('have.length.gt', 1)
.then($options => [...$options].map(option => option.innerText))
.should('be.sorted')
uj5u.com熱心網友回復:
你可以做這樣的事情。由于我不確定下拉串列項的選擇器,因此我只添加了一個占位符selector,并使用有效的選擇器對其進行編輯。
如果您在不同的陣列上操作,您可以使用sort(),這就是我在這里所做的。我們在這里創建兩組相同的陣列,strings_orig并且strings_will_apply_sort. 我們在使用for的地方保持strings_orig不變。最后我們將兩者進行比較。sort()strings_will_apply_sort
cy.get('selector').then(($elements) => {
var strings_orig = $elements.map(($el) => $el.text())
var strings_will_apply_sort = $elements.map(($el) => $el.text())
expect(strings_orig).to.equal(strings_will_apply_sort.sort())
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/489007.html
