Show List
Dropdown
Here's a sample TestCafe test script that demonstrates working with a dropdown:
import { Selector } from 'testcafe';
fixture('Dropdown Test')
.page('https://www.example.com');
test('Select Option from Dropdown', async t => {
// Selector for the dropdown element
const dropdown = Selector('#dropdown');
// Selector for the options within the dropdown
const option1 = dropdown.find('option').withText('Option 1');
const option2 = dropdown.find('option').withText('Option 2');
// Select Option 1 from the dropdown
await t
.click(dropdown)
.click(option1);
// Assert that Option 1 is selected
await t.expect(dropdown.value).eql('option1');
// Select Option 2 from the dropdown
await t
.click(dropdown)
.click(option2);
// Assert that Option 2 is selected
await t.expect(dropdown.value).eql('option2');
});
In this test:
- We import the
Selector
function from TestCafe to select elements on the web page. - We define a fixture named 'Dropdown Test' and set the page to 'https://www.example.com'.
- Inside the test function:
- We define a selector for the dropdown element (
dropdown
) and selectors for the options within the dropdown (option1
andoption2
). Replace'#dropdown'
with the appropriate selector for your dropdown element. - We click on the dropdown element to open it.
- We click on the desired option within the dropdown.
- We assert that the selected option matches the expected value ('option1' or 'option2').
- We define a selector for the dropdown element (
Make sure to replace 'https://www.example.com'
with the URL of the website you want to test, and adjust the selectors accordingly based on the structure of the webpage you are testing. Additionally, replace 'Option 1'
and 'Option 2'
with the text of the options in your dropdown.
Leave a Comment