使用 Cypress 驗證靜態下拉選單
Cypress 使用其內建命令來處理靜態下拉選單。對於靜態下拉選單,元素的標籤名稱應為<select>,其子元素的標籤名稱應為<option>。
使用的命令是 select()。此命令需要與提供標籤名稱為 select 的 DOM 元素的命令連結。下面列出了 select 命令的各種用法:
select(value) - 帶有引數 value 的 select() 命令選擇具有該值的選項。與 select() 連結時,get 方法應具有靜態下拉選單的 css 選擇器。
cy.get('select').select('value1')
select(text) - 帶有引數 text 的 select() 命令選擇具有該文字內容的選項。與 select() 連結時,get 方法應具有靜態下拉選單的 css 選擇器。
cy.get('select').select('text')
select('Value1', 'Value2') - 帶有引數的 select() 命令選擇具有這些值或文字內容的選項陣列。與 select() 連結時,get 方法應具有靜態下拉選單的 css 選擇器。
cy.get('select').select(['Tutorialspoint', 'Cypress'])
select({ force: true }) - 將選項作為引數的 select() 命令更改靜態下拉選單的預設行為。可以有三種類型的選項:log、force 和 timeout,它們的預設值分別為 true、false 和 defaultCommandTimeout(4000 毫秒)。
cy.get('select').select('Cypress', { force: true})
Cypress 使用 force 選項與隱藏元素互動,然後強制內部從下拉選單中選擇一個選項。
我們可以在 Cypress 中使用 select() 命令應用斷言。
語法
cy.get('select').select('Cypress').should('have.value', 'Cypress')
示例
使用 select() 的程式碼實現。
describe('Tutorialspoint Test', function () { // test case it('Test Case2', function (){ cy.visit("https://tutorialspoint.tw/selenium /selenium_automation_practice.htm"); // checking by values cy.get('input[type="checkbox"]') .check(['Manual Tester','Automation Tester']); // selecting a value from static dropdown cy.get('select[name="continents"]').select('Europe') // asserting the option selected .should('have.text', 'Europe') }); });
廣告