Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop using contains for queries which use labels to match #387

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions integration_test/cases/browser/fill_in_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ defmodule Wallaby.Integration.Browser.FillInTest do
end
end

test "fill_in fields with labels by exact text", %{page: page} do
password = "$uper$ecret"

page
|> fill_in(Query.text_field("Password"), with: password)
|> fill_in(Query.text_field("Confirm Password"), with: password)

assert find(page, Query.css("#password_field")) |> has_value?(password)
assert find(page, Query.css("#confirm_password_field")) |> has_value?(password)
end

test "escapes quotes", %{page: page} do
assert fill_in(page, Query.text_field("I'm a text field"), with: "Stuff")
end
Expand Down
5 changes: 5 additions & 0 deletions integration_test/support/pages/forms.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ <h1>Forms</h1>
<label for="name_field">Name</label>
<input id="name_field" type="text" name="name" value="">
<input id="email_field" type="email" name="email" value="">

<label for="password_field">Password</label>
<input id="password_field" type="password" name="password" value="">

<label for="confirm_password_field">Confirm Password</label>
<input id="confirm_password_field" type="password" name="confirm_password" value="">

<label for="file_field">File</label>
<input id="file_field" type="file" name="file_input">

Expand Down
10 changes: 5 additions & 5 deletions lib/wallaby/query/xpath.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule Wallaby.Query.XPath do
Match any radio buttons
"""
def radio_button(query) do
~s{.//input[./@type = 'radio'][(((./@id = "#{query}" or ./@name = "#{query}") or ./@placeholder = "#{query}") or ./@id = //label[contains(normalize-space(string(.)), "#{query}")]/@for)] | .//label[contains(normalize-space(string(.)), "#{query}")]//.//input[./@type = "radio"]}
~s{.//input[./@type = 'radio'][(((./@id = "#{query}" or ./@name = "#{query}") or ./@placeholder = "#{query}") or ./@id = //label[normalize-space(string(text())) = "#{query}"]/@for)] | .//label[normalize-space(string(text())) = "#{query}"]//.//input[./@type = "radio"]}
end

@doc """
Expand All @@ -40,21 +40,21 @@ defmodule Wallaby.Query.XPath do
`hidden`, or `file`.
"""
def fillable_field(query) when is_binary(query) do
~s{.//*[self::input | self::textarea][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'radio' or ./@type = 'checkbox' or ./@type = 'hidden' or ./@type = 'file')][(((./@id = "#{query}" or ./@name = "#{query}") or ./@placeholder = "#{query}") or ./@id = //label[contains(normalize-space(string(.)), "#{query}")]/@for)] | .//label[contains(normalize-space(string(.)), "#{query}")]//.//*[self::input | self::textarea][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'radio' or ./@type = 'checkbox' or ./@type = 'hidden' or ./@type = 'file')]}
~s{.//*[self::input | self::textarea][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'radio' or ./@type = 'checkbox' or ./@type = 'hidden' or ./@type = 'file')][(((./@id = "#{query}" or ./@name = "#{query}") or ./@placeholder = "#{query}") or ./@id = //label[normalize-space(string(text())) = "#{query}"]/@for)] | .//label[normalize-space(string(text())) = "#{query}"]//.//*[self::input | self::textarea][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'radio' or ./@type = 'checkbox' or ./@type = 'hidden' or ./@type = 'file')]}
end

@doc """
Match any checkboxes
"""
def checkbox(query) do
~s{.//input[./@type = 'checkbox'][(((./@id = "#{query}" or ./@name = "#{query}") or ./@placeholder = "#{query}") or ./@id = //label[contains(normalize-space(string(.)), "#{query}")]/@for)] | .//label[contains(normalize-space(string(.)), "#{query}")]//.//input[./@type = "checkbox"]}
~s{.//input[./@type = 'checkbox'][(((./@id = "#{query}" or ./@name = "#{query}") or ./@placeholder = "#{query}") or ./@id = //label[normalize-space(string(text())) = "#{query}"]/@for)] | .//label[normalize-space(string(text())) = "#{query}"]//.//input[./@type = "checkbox"]}
end

@doc """
Match any `select` by name, id, or label.
"""
def select(query) do
~s{.//select[(((./@id = "#{query}" or ./@name = "#{query}")) or ./@name = //label[contains(normalize-space(string(.)), "#{query}")]/@for or ./@id = //label[contains(normalize-space(string(.)), "#{query}")]/@for)] | .//label[contains(normalize-space(string(.)), "#{query}")]//.//select}
~s{.//select[(((./@id = "#{query}" or ./@name = "#{query}")) or ./@name = //label[normalize-space(string(text())) = "#{query}"]/@for or ./@id = //label[normalize-space(string(text())) = "#{query}"]/@for)] | .//label[normalize-space(string(text())) = "#{query}"]//.//select}
end

@doc """
Expand All @@ -68,7 +68,7 @@ defmodule Wallaby.Query.XPath do
Matches any file field by name, id, or label
"""
def file_field(query) do
~s{.//input[./@type = 'file'][(((./@id = "#{query}" or ./@name = "#{query}")) or ./@id = //label[contains(normalize-space(string(.)), "#{query}")]/@for)] | .//label[contains(normalize-space(string(.)), "#{query}")]//.//input[./@type = 'file']}
~s{.//input[./@type = 'file'][(((./@id = "#{query}" or ./@name = "#{query}")) or ./@id = //label[normalize-space(string(text())) = "#{query}"]/@for)] | .//label[normalize-space(string(text())) = "#{query}"]//.//input[./@type = 'file']}
end

@doc """
Expand Down