Thanks for the reply, Nicolas! And sorry, I am taking long to respond as well.
Here is the exact code for the examples:
test(`Verify Sort by Grade`, async ({ given, and, when, then }) => {
const { girl_position_wrapper, sort_by_select, sort_by_grade_option, sort_by_wrapper } = test_data.selectors;
const girl_position_selector = girl_position_wrapper.selector;
let character_affection_pairs = [];
given(/^I open the game with (.*)$/, async (character_affection_pair_json) => {
test_browser.logger.setTestName('Verify Sort by Grade');
test_browser.logger.setStepName(`I open the game with ${character_affection_pair_json}`);
const state_management_instructions = { hero_girls: [] };
character_affection_pairs = JSON.parse(character_affection_pair_json)[skin.domain];
for(const [id, affection_grade ] of character_affection_pairs) {
state_management_instructions.hero_girls.push({ id, affection_grade });
}
await test_helper.setUserState(skin.product_id, test_product_data, state_management_instructions);
await test_browser.loadUrl('', 'hh_game');
});
andLogsIn(and, test_browser);
andOpensCollection(and, test_browser);
givenIOpenFiltersPanel(given, test_browser);
when('I sort the girl list by Grade', async () => {
test_browser.logger.setStepName(`I sort the girl list by Grade`);
await test_browser.waitForElementToAppear(sort_by_select);
await test_browser.click(sort_by_select);
await test_browser.tryBrowserScrollingIntoView(sort_by_wrapper, sort_by_grade_option, 200);
await test_browser.click(sort_by_grade_option);
const first_girl = {
...girl_position_wrapper,
selector: girl_position_wrapper.selector.replace('girl_position', '1')
};
await test_browser.waitForElementAttributeToChange(first_girl, 'id_girl');
});
then('the girls with the most affection grades unlocked are at the top of the list', async () => {
test_browser.logger.setStepName(`the girls with the most affection grades unlocked are at the top of the list`);
const sorted_affection_pairs = character_affection_pairs.sort((first_pair, second_pair) => {
return first_pair[1] >= second_pair[1] ? -1 : 1;
});
for(let character_position = 1; character_position <= sorted_affection_pairs.length; character_position++) {
const current_character_wrapper = getCurrentGirl(girl_position_wrapper, character_position, girl_position_selector);
const is_displayed = await test_browser.isDisplayed(current_character_wrapper);
expect(is_displayed).toBeTruthy();
const id_girl = await test_browser.getElementAttribute(current_character_wrapper, 'id_girl');
expect(+id_girl).toEqual(sorted_affection_pairs[character_position - 1][0]);
}
test_browser.logger.cleanLogs();
});
});
And another test:
test(`Verify Sort by Name`, async ({ given, and, when, then }) => {
const { character_name_position_wrapper, sort_by_select, sort_by_name_option, sort_by_wrapper, girl_position_wrapper } = test_data.selectors;
const character_position_selector = character_name_position_wrapper.selector;
let character_ids = [];
given(/^I open the game with (.*)$/, async (character_ids_json) => {
test_browser.logger.setTestName('Verify Sort by Name');
test_browser.logger.setStepName(`I open the game with ${character_ids_json}`);
const state_management_instructions = { hero_girls: [] };
character_ids = JSON.parse(character_ids_json)[skin.domain];
for(const id of character_ids) {
state_management_instructions.hero_girls.push({ id });
}
await test_helper.setUserState(skin.product_id, test_product_data, state_management_instructions);
await test_browser.loadUrl('', 'hh_game');
});
andLogsIn(and, test_browser);
andOpensCollection(and, test_browser);
givenIOpenFiltersPanel(given, test_browser);
when('I sort the girl list by Name', async () => {
test_browser.logger.setStepName(`I sort the girl list by Name`);
await test_browser.waitForElementToAppear(sort_by_select);
await test_browser.click(sort_by_select);
await test_browser.tryBrowserScrollingIntoView(sort_by_wrapper, sort_by_name_option);
await test_browser.click(sort_by_name_option);
if(skin.domain !== 'game3') {
const first_girl = {
...girl_position_wrapper,
selector: girl_position_wrapper.selector.replace('girl_position', '2')
};
await test_browser.waitForElementAttributeToChange(first_girl, 'id_girl');
}
});
then('the girl list is sorted alphabetically', async () => {
test_browser.logger.setStepName(`the girl list is sorted alphabetically`);
const character_names = [];
for(let character_position = 1; character_position <= character_ids.length; character_position++) {
const current_character_wrapper = getCurrentGirl(character_name_position_wrapper, character_position, character_position_selector);
const is_displayed = await test_browser.isDisplayed(current_character_wrapper);
expect(is_displayed).toBeTruthy();
const character_name = await test_browser.getElementText(current_character_wrapper);
character_names.push(character_name);
}
const sorted_character_names = [...character_names].sort();
expect(character_names).toEqual(sorted_character_names);
test_browser.logger.cleanLogs();
});
});
The code examples are from automation tests with javascript and jest.