From 5ea44bc4eefdba1cd262bcd5a39f5eb0f79caad0 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Fri, 9 Sep 2022 20:37:50 -0700 Subject: [PATCH] puppeteer_tests: Convert now-deprecated waitForXPath, $x methods. Signed-off-by: Anders Kaseorg --- frontend_tests/puppeteer_lib/common.ts | 6 +++--- frontend_tests/puppeteer_tests/admin.ts | 6 +++--- frontend_tests/puppeteer_tests/compose.ts | 17 ++++++----------- .../puppeteer_tests/custom-profile.ts | 8 ++++---- frontend_tests/puppeteer_tests/mention.ts | 4 ++-- frontend_tests/puppeteer_tests/navigation.ts | 4 ++-- frontend_tests/puppeteer_tests/settings.ts | 8 ++++---- frontend_tests/puppeteer_tests/stream_create.ts | 2 +- 8 files changed, 25 insertions(+), 30 deletions(-) diff --git a/frontend_tests/puppeteer_lib/common.ts b/frontend_tests/puppeteer_lib/common.ts index b30c830b9d..db4287bcb0 100644 --- a/frontend_tests/puppeteer_lib/common.ts +++ b/frontend_tests/puppeteer_lib/common.ts @@ -499,12 +499,12 @@ class CommonUtils { ): Promise { console.log(`Looking in ${field_selector} to select ${str}, ${item}`); await this.clear_and_type(page, field_selector, str); - const entry = await page.waitForXPath( - `//*[@class="typeahead dropdown-menu" and contains(@style, "display: block")]//li[contains(normalize-space(), "${item}")]//a`, + const entry = await page.waitForSelector( + `xpath///*[@class="typeahead dropdown-menu" and contains(@style, "display: block")]//li[contains(normalize-space(), "${item}")]//a`, {visible: true}, ); assert.ok(entry); - await (entry as ElementHandle).hover(); + await entry.hover(); await page.evaluate((entry) => { if (!(entry instanceof HTMLElement)) { throw new TypeError("expected HTMLElement"); diff --git a/frontend_tests/puppeteer_tests/admin.ts b/frontend_tests/puppeteer_tests/admin.ts index db72b350aa..c672846598 100644 --- a/frontend_tests/puppeteer_tests/admin.ts +++ b/frontend_tests/puppeteer_tests/admin.ts @@ -42,12 +42,12 @@ async function test_change_new_stream_notifications_setting(page: Page): Promise ).val() === "rome", ); - const rome_in_dropdown = await page.waitForXPath( - '//*[@id="realm_notifications_stream_id_widget"]//*[@class="dropdown-list-body"]/li[1]', + const rome_in_dropdown = await page.waitForSelector( + 'xpath///*[@id="realm_notifications_stream_id_widget"]//*[@class="dropdown-list-body"]/li[1]', {visible: true}, ); assert.ok(rome_in_dropdown); - await (rome_in_dropdown as ElementHandle).click(); + await rome_in_dropdown.click(); await submit_notifications_stream_settings(page); diff --git a/frontend_tests/puppeteer_tests/compose.ts b/frontend_tests/puppeteer_tests/compose.ts index ea3b194c9a..5b3710cf73 100644 --- a/frontend_tests/puppeteer_tests/compose.ts +++ b/frontend_tests/puppeteer_tests/compose.ts @@ -17,12 +17,8 @@ async function close_compose_box(page: Page): Promise { await page.waitForSelector("#compose-textarea", {hidden: true}); } -function get_message_xpath(text: string): string { - return `//p[text()='${text}']`; -} - -function get_last_element(array: T[]): T { - return array.slice(-1)[0]; +function get_message_selector(text: string): string { + return `xpath///p[text()='${text}'][last()]`; } async function test_send_messages(page: Page): Promise { @@ -59,9 +55,8 @@ async function test_keyboard_shortcuts(page: Page): Promise { } async function test_reply_by_click_prepopulates_stream_topic_names(page: Page): Promise { - const stream_message_xpath = get_message_xpath("Compose stream reply test"); - await page.waitForXPath(stream_message_xpath, {visible: true}); - const stream_message = get_last_element(await page.$x(stream_message_xpath)); + const stream_message_selector = get_message_selector("Compose stream reply test"); + const stream_message = await page.waitForSelector(stream_message_selector, {visible: true}); // we chose only the last element make sure we don't click on any duplicates. await (stream_message as ElementHandle).click(); await common.check_form_contents(page, "#send_message_form", { @@ -75,8 +70,8 @@ async function test_reply_by_click_prepopulates_stream_topic_names(page: Page): async function test_reply_by_click_prepopulates_private_message_recipient( page: Page, ): Promise { - const private_message = get_last_element( - await page.$x(get_message_xpath("Compose private message reply test")), + const private_message = await page.$( + get_message_selector("Compose private message reply test"), ); await (private_message as ElementHandle).click(); await page.waitForSelector("#private_message_recipient", {visible: true}); diff --git a/frontend_tests/puppeteer_tests/custom-profile.ts b/frontend_tests/puppeteer_tests/custom-profile.ts index 337f86d64b..e7bee6df6b 100644 --- a/frontend_tests/puppeteer_tests/custom-profile.ts +++ b/frontend_tests/puppeteer_tests/custom-profile.ts @@ -26,8 +26,8 @@ async function test_add_new_profile_field(page: Page): Promise { await page.click("#dialog_widget_modal .dialog_submit_button"); await common.wait_for_micromodal_to_close(page); - await page.waitForXPath( - '//*[@id="admin_profile_fields_table"]//tr[last()]/td[normalize-space()="Teams"]', + await page.waitForSelector( + 'xpath///*[@id="admin_profile_fields_table"]//tr[last()]/td[normalize-space()="Teams"]', ); assert.strictEqual( await common.get_text_from_selector(page, `${profile_field_row} span.profile_field_type`), @@ -52,8 +52,8 @@ async function test_edit_profile_field(page: Page): Promise { await page.click("#dialog_widget_modal .dialog_submit_button"); await common.wait_for_micromodal_to_close(page); - await page.waitForXPath( - '//*[@id="admin_profile_fields_table"]//tr[last()]/td[normalize-space()="team"]', + await page.waitForSelector( + 'xpath///*[@id="admin_profile_fields_table"]//tr[last()]/td[normalize-space()="team"]', ); assert.strictEqual( await common.get_text_from_selector(page, `${profile_field_row} span.profile_field_type`), diff --git a/frontend_tests/puppeteer_tests/mention.ts b/frontend_tests/puppeteer_tests/mention.ts index e3c6798dfd..161c5d06b3 100644 --- a/frontend_tests/puppeteer_tests/mention.ts +++ b/frontend_tests/puppeteer_tests/mention.ts @@ -29,8 +29,8 @@ async function test_mention(page: Page): Promise { assert.ok(stream_size > threshold); await page.click("#compose-send-button"); - await page.waitForXPath( - '//*[@class="compose-all-everyone-msg" and contains(text(), "Are you sure you want to mention all")]', + await page.waitForSelector( + 'xpath///*[@class="compose-all-everyone-msg" and contains(text(), "Are you sure you want to mention all")]', ); await page.click(".compose-all-everyone-confirm"); await page.waitForSelector(".compose-all-everyone-msg", {hidden: true}); diff --git a/frontend_tests/puppeteer_tests/navigation.ts b/frontend_tests/puppeteer_tests/navigation.ts index 9d68af85df..057667f35f 100644 --- a/frontend_tests/puppeteer_tests/navigation.ts +++ b/frontend_tests/puppeteer_tests/navigation.ts @@ -108,8 +108,8 @@ async function navigation_tests(page: Page): Promise { await test_reload_hash(page); // Verify that we're narrowed to the target stream - await page.waitForXPath( - '//*[@id="message_view_header"]//*[@class="stream" and normalize-space()="Verona"]', + await page.waitForSelector( + 'xpath///*[@id="message_view_header"]//*[@class="stream" and normalize-space()="Verona"]', ); } diff --git a/frontend_tests/puppeteer_tests/settings.ts b/frontend_tests/puppeteer_tests/settings.ts index 204ff3eba1..2d0105b7d3 100644 --- a/frontend_tests/puppeteer_tests/settings.ts +++ b/frontend_tests/puppeteer_tests/settings.ts @@ -177,8 +177,8 @@ async function test_edit_bot_form(page: Page): Promise { // The form gets closed on saving. So, assert it's closed by waiting for it to be hidden. await page.waitForSelector("#edit_bot_modal", {hidden: true}); - await page.waitForXPath( - `//*[@class="btn open_edit_bot_form" and @data-email="${bot1_email}"]/ancestor::*[@class="details"]/*[@class="name" and text()="Bot one"]`, + await page.waitForSelector( + `xpath///*[@class="btn open_edit_bot_form" and @data-email="${bot1_email}"]/ancestor::*[@class="details"]/*[@class="name" and text()="Bot one"]`, ); await common.wait_for_micromodal_to_close(page); @@ -221,8 +221,8 @@ async function test_invalid_edit_bot_form(page: Page): Promise { cancel_button_selector, ); await page.click(cancel_button_selector); - await page.waitForXPath( - `//*[@class="btn open_edit_bot_form" and @data-email="${bot1_email}"]/ancestor::*[@class="details"]/*[@class="name" and text()="Bot one"]`, + await page.waitForSelector( + `xpath///*[@class="btn open_edit_bot_form" and @data-email="${bot1_email}"]/ancestor::*[@class="details"]/*[@class="name" and text()="Bot one"]`, ); await common.wait_for_micromodal_to_close(page); diff --git a/frontend_tests/puppeteer_tests/stream_create.ts b/frontend_tests/puppeteer_tests/stream_create.ts index ab431dbc1c..a101f45739 100644 --- a/frontend_tests/puppeteer_tests/stream_create.ts +++ b/frontend_tests/puppeteer_tests/stream_create.ts @@ -78,7 +78,7 @@ async function test_user_filter_ui(page: Page): Promise { } async function create_stream(page: Page): Promise { - await page.waitForXPath('//*[text()="Create stream"]', {visible: true}); + await page.waitForSelector('xpath///*[text()="Create stream"]', {visible: true}); await common.fill_form(page, "form#stream_creation_form", { stream_name: "Puppeteer", stream_description: "Everything Puppeteer",