DEV: Try until success for clipboard copies (#27986)

This commit is contained in:
Natalie Tay 2024-07-19 19:44:10 +08:00 committed by GitHub
parent b10b485572
commit 278ae6e5fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 26 additions and 16 deletions

View File

@ -35,7 +35,7 @@ RSpec.describe "Chat message - channel", type: :system do
channel_page.messages.copy_text(message_1)
expect(cdp.read_clipboard.chomp).to eq(message_1.message)
cdp.clipboard_has_text?(message_1.message, chomp: true)
expect(PageObjects::Components::Toasts.new).to have_success(I18n.t("js.chat.text_copied"))
end
end
@ -48,7 +48,7 @@ RSpec.describe "Chat message - channel", type: :system do
channel_page.messages.copy_link(message_1)
expect(cdp.read_clipboard).to include("/chat/c/-/#{channel_1.id}/#{message_1.id}")
cdp.clipboard_has_text?("/chat/c/-/#{channel_1.id}/#{message_1.id}", strict: false)
expect(PageObjects::Components::Toasts.new).to have_success(I18n.t("js.chat.link_copied"))
end
@ -57,7 +57,7 @@ RSpec.describe "Chat message - channel", type: :system do
channel_page.messages.copy_link(message_1)
expect(cdp.read_clipboard).to include("/chat/c/-/#{channel_1.id}/#{message_1.id}")
cdp.clipboard_has_text?("/chat/c/-/#{channel_1.id}/#{message_1.id}", strict: false)
expect(PageObjects::Components::Toasts.new).to have_success(I18n.t("js.chat.link_copied"))
end
@ -77,19 +77,21 @@ RSpec.describe "Chat message - channel", type: :system do
channel_page.messages.copy_link(thread_1.original_message)
expect(cdp.read_clipboard).to include(
cdp.clipboard_has_text?(
"/chat/c/-/#{channel_1.id}/#{thread_1.original_message.id}",
strict: false,
)
expect(PageObjects::Components::Toasts.new).to have_success(I18n.t("js.chat.link_copied"))
end
xit "[mobile] copies the link to the message", mobile: true do
it "[mobile] copies the link to the message", mobile: true do
chat_page.visit_channel(channel_1)
channel_page.messages.copy_link(thread_1.original_message)
expect(cdp.read_clipboard).to include(
cdp.clipboard_has_text?(
"/chat/c/-/#{channel_1.id}/#{thread_1.original_message.id}",
strict: false,
)
expect(PageObjects::Components::Toasts.new).to have_success(I18n.t("js.chat.link_copied"))
end

View File

@ -43,7 +43,7 @@ RSpec.describe "Chat message - thread", type: :system do
thread_page.messages.copy_text(thread_message_1)
expect(cdp.read_clipboard.chomp).to eq(thread_message_1.message)
cdp.clipboard_has_text?(thread_message_1.message)
expect(PageObjects::Components::Toasts.new).to have_success(I18n.t("js.chat.text_copied"))
end
end
@ -58,8 +58,9 @@ RSpec.describe "Chat message - thread", type: :system do
thread_page.messages.copy_link(thread_message_1)
expect(cdp.read_clipboard).to include(
cdp.clipboard_has_text?(
"/chat/c/-/#{channel_1.id}/t/#{thread_message_1.thread.id}/#{thread_message_1.id}",
strict: false,
)
expect(PageObjects::Components::Toasts.new).to have_success(I18n.t("js.chat.link_copied"))
end
@ -69,8 +70,9 @@ RSpec.describe "Chat message - thread", type: :system do
thread_page.messages.copy_link(thread_message_1)
expect(cdp.read_clipboard).to include(
cdp.clipboard_has_text?(
"/chat/c/-/#{channel_1.id}/t/#{thread_message_1.thread.id}/#{thread_message_1.id}",
strict: false,
)
expect(PageObjects::Components::Toasts.new).to have_success(I18n.t("js.chat.link_copied"))
end

View File

@ -23,9 +23,8 @@ RSpec.describe "Quoting chat message transcripts", type: :system do
expect(PageObjects::Components::Toasts.new).to have_success(
I18n.t("js.chat.quote.copy_success"),
)
clip_text = cdp.read_clipboard
expect(clip_text.chomp).to eq(generate_transcript(messages, current_user))
clip_text
cdp.clipboard_has_text?(generate_transcript(messages, current_user), chomp: true)
cdp.read_clipboard
end
def generate_transcript(messages, acting_user)

View File

@ -3,6 +3,8 @@
module PageObjects
class CDP
include Capybara::DSL
include SystemHelpers
include RSpec::Matchers
def allow_clipboard
cdp_params = {
@ -28,6 +30,13 @@ module PageObjects
page.evaluate_async_script("navigator.clipboard.readText().then(arguments[0])")
end
def clipboard_has_text?(text, chomp: false, strict: true)
try_until_success do
clipboard_text = chomp ? read_clipboard.chomp : read_clipboard
expect(clipboard_text).to strict ? eq(text) : include(text)
end
end
def with_network_disconnected
begin
page.driver.browser.network_conditions = { offline: true }

View File

@ -17,9 +17,7 @@ describe "Post menu", type: :system, js: true do
it "copies the absolute link to the post when clicked" do
topic_page.visit_topic(post.topic)
topic_page.click_post_action_button(post, :copy_link)
expect(cdp.read_clipboard).to eq(
post.full_url(share_url: true) + "?u=#{current_user.username}",
)
cdp.clipboard_has_text?(post.full_url(share_url: true) + "?u=#{current_user.username}")
end
end
end

View File

@ -20,7 +20,7 @@ describe "Post selection | Copy quote", type: :system do
select_text_range("#{topic_page.post_by_number_selector(1)} .cooked p", 0, 10)
topic_page.copy_quote_button.click
expect(cdp.read_clipboard.chomp).to eq(<<~QUOTE.chomp)
cdp.clipboard_has_text?(<<~QUOTE.chomp, chomp: true)
[quote=\"#{post.user.username}, post:1, topic:#{topic.id}\"]\nHello worl\n[/quote]\n
QUOTE
end