Skip to content

Commit

Permalink
Merge pull request #239 from rubycdp/bugfix/issue#238-methods-select-…
Browse files Browse the repository at this point in the history
…and-selected-raises-an-exception-when-fires-within-frame-scope

Bugfix: #238: Methods select and selected works properly within frame scope
  • Loading branch information
route authored May 10, 2022
2 parents b3045b1 + c715695 commit cdca4a1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 23 deletions.
20 changes: 12 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ a block with this page, after which the page is closed.
- `Ferrum::Context#has_target?` -> `Ferrum::Context#target?`
- We now start looking for Chrome first instead of Chromium, the order for checking binaries has changed
- Multiple methods are moved into `Utils`:
- Ferrum.with_attempts -> Ferrum::Utils::Attempt.with_retry
- Ferrum.started -> Ferrum::Utils::ElapsedTime.start
- Ferrum.elapsed_time -> Ferrum::Utils::ElapsedTime.elapsed_time
- Ferrum.monotonic_time -> Ferrum::Utils::ElapsedTime.monotonic_time
- Ferrum.timeout? -> Ferrum::Utils::ElapsedTime.timeout?
- Ferrum.windows? -> Ferrum::Utils::Platform.windows?
- Ferrum.mac? -> Ferrum::Utils::Platform.mac?
- Ferrum.mri? -> Ferrum::Utils::Platform.mri?
- `Ferrum.with_attempts` -> `Ferrum::Utils::Attempt.with_retry`
- `Ferrum.started` -> `Ferrum::Utils::ElapsedTime.start`
- `Ferrum.elapsed_time` -> `Ferrum::Utils::ElapsedTime.elapsed_time`
- `Ferrum.monotonic_time` -> `Ferrum::Utils::ElapsedTime.monotonic_time`
- `Ferrum.timeout?` -> `Ferrum::Utils::ElapsedTime.timeout?`
- `Ferrum.windows?` -> `Ferrum::Utils::Platform.windows?`
- `Ferrum.mac?` -> `Ferrum::Utils::Platform.mac?`
- `Ferrum.mri?` -> `Ferrum::Utils::Platform.mri?`

### Fixed

- `Ferrum::Node#selected`, `Ferrum::Node#select` to work in frame scope

## [0.11](https://github.com/rubycdp/ferrum/compare/v0.10.2...v0.11) - (Mar 11, 2021) ##

Expand Down
4 changes: 2 additions & 2 deletions lib/ferrum/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def selected
return Array.from(element).filter(option => option.selected);
}
JS
page.evaluate_func(function, self)
page.evaluate_func(function, self, on: self)
end

def select(*values, by: :value)
Expand All @@ -159,7 +159,7 @@ def select(*values, by: :value)
element.dispatchEvent(new Event('change', { bubbles: true }));
}
JS
page.evaluate_func(function, self, values.flatten, by)
page.evaluate_func(function, self, values.flatten, by, on: self)
end
end

Expand Down
10 changes: 10 additions & 0 deletions spec/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ module Ferrum
.to raise_exception(Ferrum::JavaScriptError, /Element is not a <select> element/)
end
end

it "returns selected options within frame" do
frame = browser.at_xpath("//iframe[@name='frame']").frame
expect(frame.at_xpath("//*[@id='select']").selected.map(&:text)).to eq(["One"])
end
end

describe "#select" do
Expand Down Expand Up @@ -193,6 +198,11 @@ module Ferrum
.to eq([""])
end
end

it "picks option within frame" do
frame = browser.at_xpath("//iframe[@name='frame']").frame
expect(frame.at_xpath("//*[@id='select']").select("Two", by: :text).selected.map(&:text)).to eq(["Two"])
end
end

context "when the element is not in the viewport" do
Expand Down
26 changes: 13 additions & 13 deletions spec/support/views/form.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

<h1>Form</h1>

<form action="/form" method="post" novalidate>

<p>
<label for="form_title">Title</label>
<select name="form[title]" id="form_title" class="title">
Expand Down Expand Up @@ -144,12 +142,12 @@
</p>

<p>
<label for="form_description">Description</label></br>
<label for="form_description">Description</label><br/>
<textarea name="form[description]" id="form_description">Descriptive text goes here</textarea>
<p>
</p>

<p>
<label for="form_newline">NewLine</label></br>
<label for="form_newline">NewLine</label><br/>
<textarea name="form[newline]" id="form_newline">

New line after and before textarea tag
Expand Down Expand Up @@ -298,13 +296,13 @@ New line after and before textarea tag

<p>
<span>First address<span>
<label for='address1_street'>Street</label>
<label for="address1_street">Street</label>
<input type="text" name="form[addresses][][street]" value="" id="address1_street">

<label for='address1_city'>City</label>
<label for="address1_city">City</label>
<input type="text" name="form[addresses][][city]" value="" id="address1_city">

<label for='address1_country'>Country</label>
<label for="address1_country">Country</label>
<select name="form[addresses][][country]" id="address1_country">
<option selected>France</option>
<option>Ukraine</option>
Expand All @@ -313,13 +311,13 @@ New line after and before textarea tag

<p>
<span>Second address<span>
<label for='address2_street'>Street</label>
<label for="address2_street">Street</label>
<input type="text" name="form[addresses][][street]" value="" id="address2_street">

<label for='address2_city'>City</label>
<label for="address2_city">City</label>
<input type="text" name="form[addresses][][city]" value="" id="address2_city">

<label for='address2_country'>Country</label>
<label for="address2_country">Country</label>
<select name="form[addresses][][country]" id="address2_country">
<option>France</option>
<option selected>Ukraine</option>
Expand Down Expand Up @@ -580,10 +578,10 @@ New line after and before textarea tag

<input type="hidden" name="form[data]" value="TWTW"/>
<p>
<button formaction='/redirect_307'>Go 307</button>
<button formaction="/redirect_307">Go 307</button>
</p>
<p>
<button formaction='/redirect_308'>Go 308</button>
<button formaction="/redirect_308">Go 308</button>
</p>
</form>

Expand Down Expand Up @@ -677,3 +675,5 @@ New line after and before textarea tag
<option value=""></option>
<option value="AU">Australia</option>
</select>

<iframe src="/ferrum/form_iframe" name="frame"></iframe>
8 changes: 8 additions & 0 deletions spec/support/views/form_iframe.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<body>
<select id="select">
<option>One</option>
<option>Two</option>
</select>
</body>
</html>

0 comments on commit cdca4a1

Please sign in to comment.