Files
discourse/spec/system/topic_page_spec.rb
Joffrey JAFFEUX b6aad28ccf DEV: replace selenium driver with playwright (#31977)
This commit is replacing the system specs driver (selenium) by
Playwright: https://playwright.dev/

We are still using Capybara to write the specs but they will now be run
by Playwright. To achieve this we are using the non official ruby
driver: https://github.com/YusukeIwaki/capybara-playwright-driver

### Notable changes

- `CHROME_DEV_TOOLS` has been removed, it's not working well with
playwright use `pause_test` and inspect browser for now.

- `fill_in` is not generating key events in playwright, use `send_keys`
if you need this.

### New spec options

#### trace

Allows to capture a trace in a zip file which you can load at
https://trace.playwright.dev or locally through `npx playwright
show-trace /path/to/trace.zip`

_Example usage:_

```ruby
it "shows bar", trace: true do
  visit("/")

  find(".foo").click

  expect(page).to have_css(".bar")
end
```

#### video

Allows to capture a video of your spec.

_Example usage:_

```ruby
it "shows bar", video: true do
  visit("/")

  find(".foo").click

  expect(page).to have_css(".bar")
end
```

### New env variable

#### PLAYWRIGHT_SLOW_MO_MS

Allow to force playwright to wait DURATION (in ms) at each action.

_Example usage:_

```
PLAYWRIGHT_SLOW_MO_MS=1000 rspec foo_spec.rb
```

#### PLAYWRIGHT_HEADLESS

Allow to be in headless mode or not. Default will be headless.

_Example usage:_

```
PLAYWRIGHT_HEADLESS=0 rspec foo_spec.rb # will show the browser
```

### New helpers

#### with_logs

Allows to access the browser logs and check if something specific has
been logged.

_Example usage:_

```ruby
with_logs do |logger|
  # do something

  expect(logger.logs.map { |log| log[:message] }).to include("foo")
end
```

#### add_cookie

Allows to add a cookie on the browser session.

_Example usage:_

```ruby
add_cookie(name: "destination_url", value: "/new")
```

#### get_style

Get the property style value of an element.

_Example usage:_

```ruby
expect(get_style(find(".foo"), "height")).to eq("200px")
```

#### get_rgb_color

Get the rgb color of an element.

_Example usage:_

```ruby
expect(get_rgb_color(find("html"), "backgroundColor")).to eq("rgb(170, 51, 159)")
```
2025-05-06 10:44:14 +02:00

133 lines
3.7 KiB
Ruby

# frozen_string_literal: true
describe "Topic page", type: :system do
fab!(:topic)
fab!(:admin)
before { Fabricate(:post, topic: topic, cooked: <<~HTML) }
<h2 dir="ltr" id="toc-h2-testing" data-d-toc="toc-h2-testing" class="d-toc-post-heading">
<a name="toc-h2-testing" class="anchor" href="#toc-h2-testing">x</a>
Testing
</h2>
<p id="test-last-cooked-paragraph">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tempor.</p>
HTML
it "allows TOC anchor navigation" do
visit("/t/#{topic.slug}/#{topic.id}")
find("#toc-h2-testing .anchor", visible: :all).click
try_until_success do
expect(current_url).to match("/t/#{topic.slug}/#{topic.id}#toc-h2-testing")
end
end
context "with a subfolder setup" do
before { set_subfolder "/forum" }
it "allows TOC anchor navigation" do
visit("/forum/t/#{topic.slug}/#{topic.id}")
find("#toc-h2-testing .anchor", visible: :all).click
try_until_success do
expect(current_url).to match("/forum/t/#{topic.slug}/#{topic.id}#toc-h2-testing")
end
end
end
context "with a post containing a code block" do
before { Fabricate(:post, topic: topic, raw: <<~RAW) }
this a code block
```
echo "hello world"
```
RAW
it "includes the copy button" do
visit("/t/#{topic.slug}/#{topic.id}")
expect(".codeblock-button-wrapper").to be_present
end
end
context "with a gap" do
before do
post2 = Fabricate(:post, topic: topic, cooked: "post2")
post3 = Fabricate(:post, topic: topic, cooked: "post3")
post4 = Fabricate(:post, topic: topic, cooked: "post4")
PostDestroyer.new(Discourse.system_user, post2, context: "Automated testing").destroy
PostDestroyer.new(Discourse.system_user, post3, context: "Automated testing").destroy
sign_in admin
end
it "displays the gap to admins, and allows them to expand it" do
visit "/t/#{topic.slug}/#{topic.id}"
expect(page).to have_css(".topic-post", count: 2)
find(".post-stream .gap").click()
expect(page).to have_css(".topic-post", count: 4)
end
end
it "supports shift+a kbd shortcut to toggle admin menu" do
sign_in admin
visit("/t/#{topic.slug}/#{topic.id}")
expect(".toggle-admin-menu").to be_present
send_keys([:shift, "a"])
expect(page).to have_css(".topic-admin-menu-content")
send_keys([:shift, "a"])
expect(page).to have_no_css(".topic-admin-menu-content")
end
context "with End keyboard shortcut" do
fab!(:posts) { Fabricate.times(25, :post, topic: topic) }
it "loads last post" do
visit "/t/#{topic.slug}/#{topic.id}/1"
send_keys(:end)
expect(find("#post_#{topic.highest_post_number}")).to be_visible
end
end
context "when triple clicking to select a paragraph" do
it "select the last paragraph" do
visit "/t/#{topic.slug}/#{topic.id}/1"
paragraph = find("#test-last-cooked-paragraph")
page.driver.with_playwright_page do |pw_page|
paragraph.hover
rect = paragraph.native.bounding_box
x = rect["x"] + rect["width"] / 2
y = rect["y"] + rect["height"] / 2
pw_page.mouse.click(x, y, clickCount: 3)
end
# get the selected text in the browser
select_content = page.evaluate_script("window.getSelection().toString()")
# the browser is returning control characters among the whiter space in the end of the text
# this regex will work as a .rstrip on steroids and remove them
select_content.gsub!(/[\s\p{Cf}]+$/, "")
# compare the selected text with the last paragraph
expect(select_content).to eq(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tempor.",
)
end
end
end