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

Implement wait_for_selector #236

Open
wants to merge 13 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Added

- `Ferrum::Browser#wait_for_css`: returns instance of `Ferrum::Node` that's matched by provided css selector.
- `Ferrum::Browser#wait_for_xpath`: returns instance of `Ferrum::Node` that's matched by provided XPath selector.
- Alias `Ferrum::Frame#content=` to `Ferrum::Frame#set_content`
- Alias `Ferrum::Node#propery` to `Ferrum::Node#[]`
- Implement `Ferrum::Network#blacklist=` and `Ferrum::Network#whitelist=`
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,26 @@ browser.go_to("https://google.com/")
browser.body # => '<html itemscope="" itemtype="http://schema.org/WebPage" lang="ru"><head>...
```

#### wait_for_css : `Node`

```ruby
browser.wait_for_css("body", timeout: 3000, interval: 100) # => [Node]
```
* css `String` - CSS selector for a target element.
* options `Hash`
* `:timeout` (Integer) - timeout for the selector-waiting in milliseconds, 3000 ms by default.
* `:interval` (Integer) - interval that used to compute attempts for the selector-waiting (max attempts = timeout / interval), 100 by default.

#### wait_for_xpath : `Node`

```ruby
browser.wait_for_xpath("//body", timeout: 3000, interval: 100) # => [Node]
```

* xpath `String` - XPath selector for a target element.
* options `Hash`
* `:timeout` (Integer) - timeout for the selector-waiting in milliseconds, 3000 ms by default.
* `:interval` (Integer) - interval that used to compute attempts for the selector-waiting (max attempts = timeout / interval), 100 by default.

## Screenshots

Expand Down
2 changes: 1 addition & 1 deletion lib/ferrum/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Browser
evaluate evaluate_on evaluate_async execute evaluate_func
add_script_tag add_style_tag bypass_csp
on goto position position=
playback_rate playback_rate=] => :page
playback_rate playback_rate= wait_for_css wait_for_xpath] => :page
delegate %i[default_user_agent] => :process

attr_reader :client, :process, :contexts, :logger, :js_errors, :pending_connection_errors,
Expand Down
52 changes: 52 additions & 0 deletions lib/ferrum/frame/dom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ def at_xpath(selector, within: nil)
evaluate_func(expr, selector, within)
end

def wait_for_xpath(xpath, **options)
expr = <<~JS
function(selector) {
return document.evaluate(selector, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
JS

wait_for_selector(xpath, expr, **options)
end

def css(selector, within: nil)
expr = <<~JS
function(selector, within) {
Expand All @@ -86,6 +96,48 @@ def at_css(selector, within: nil)

evaluate_func(expr, selector, within)
end

def wait_for_css(css, **options)
expr = <<~JS
function(selector) {
return document.querySelector(selector);
}
JS

wait_for_selector(css, expr, **options)
end

private

def wait_for_selector(selector, find_element_expression, timeout: 3000, interval: 100)
expr = <<~JS
function(selector, findElementExpression, timeout, interval) {
var attempts = 0;
var max = timeout / interval;
var findElement = new Function(function(expression) {
return "{ return " + expression + " };";
}(findElementExpression))();
function waitForElement(resolve, reject) {
if (attempts > ((max < 1) ? 1 : max)) {
return reject(new Error("Not found element match the selector: " + selector));
}
var element = findElement(selector);
if (element !== null) {
return resolve(element);
}
setTimeout(function () {
waitForElement(resolve, reject);
}, interval);
attempts++;
}
return new Promise(function (resolve, reject) {
waitForElement(resolve, reject);
});
}
JS

evaluate_func(expr, selector, find_element_expression, timeout, interval, awaitPromise: true)
end
end
end
end
4 changes: 2 additions & 2 deletions lib/ferrum/frame/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def execute(expression, *args)
true
end

def evaluate_func(expression, *args, on: nil)
call(expression: expression, arguments: args, on: on)
def evaluate_func(expression, *args, on: nil, **options)
call(expression: expression, arguments: args, on: on, **options)
end

def evaluate_on(node:, expression:, by_value: true, wait: 0)
Expand Down
2 changes: 1 addition & 1 deletion lib/ferrum/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def reset
delegate %i[at_css at_xpath css xpath
current_url current_title url title body doctype content=
execution_id evaluate evaluate_on evaluate_async execute evaluate_func
add_script_tag add_style_tag] => :main_frame
add_script_tag add_style_tag wait_for_css wait_for_xpath] => :main_frame

include Animation
include Screenshot
Expand Down
106 changes: 106 additions & 0 deletions spec/browser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,112 @@ module Ferrum
expect(browser.evaluate("window.last_hashchange")).to eq("#foo")
end

context "wait_for_css" do
before do
browser.go_to("/ferrum/with_js")
end

it "waits for provided css selector" do
expect(browser.wait_for_css("div#wait_for_selector")).to be_kind_of(Ferrum::Node)
end

it "waits for provided css of hidden element" do
expect(browser.wait_for_css("div#wait_for_hidden_selector")).to be_kind_of(Ferrum::Node)
end

it "raises error when default timeout exceed" do
expect do
browser.wait_for_css("div#not_existed_element")
end.to raise_error(Ferrum::JavaScriptError, /Not found element match the selector/)
end

it "raises error when timeout exceed" do
expect do
browser.wait_for_css("div#wait_for_selector", timeout: 800)
end.to raise_error(Ferrum::JavaScriptError, /Not found element match the selector/)
end

it "raises error when provided invalid css selector" do
expect do
browser.wait_for_css("//div[@id='wait_for_selector']")
end.to raise_error(Ferrum::JavaScriptError, /Failed to execute 'querySelector' on 'Document'/)
end

it "waits less than provided timeout when node found" do
Timeout.timeout(1) do
expect(browser.wait_for_css("div#wait_for_selector", timeout: 2000)).to be_kind_of(Ferrum::Node)
end
end

it "waits for selector within frame" do
browser.execute <<-JS
setTimeout(function(){
document.body.innerHTML += "<iframe src='about:blank' name='frame'>";
var iframeDocument = document.querySelector("iframe[name='frame']").contentWindow.document;
var content = "<html><body><div id='wait_for_selector_within_frame'></div></body></html>";
iframeDocument.open("text/html", "replace");
iframeDocument.write(content);
iframeDocument.close();
}, 900);
JS
frame = browser.wait_for_css("iframe[name='frame']").frame
expect(frame.wait_for_css("div#wait_for_selector_within_frame")).to be_kind_of(Ferrum::Node)
end
end

context "wait_for_xpath" do
before do
browser.go_to("/ferrum/with_js")
end

it "waits for provided xpath selector" do
expect(browser.wait_for_xpath("//div[@id='wait_for_selector']")).to be_kind_of(Ferrum::Node)
end

it "waits for provided xpath of hidden element" do
expect(browser.wait_for_xpath("//div[@id='wait_for_hidden_selector']")).to be_kind_of(Ferrum::Node)
end

it "raises error when default timeout exceed" do
expect do
browser.wait_for_xpath("//div[@id='not_existed_element']")
end.to raise_error(Ferrum::JavaScriptError, /Not found element match the selector/)
end

it "raises error when timeout exceed" do
expect do
browser.wait_for_xpath("//div[@id='wait_for_selector']", timeout: 800)
end.to raise_error(Ferrum::JavaScriptError, /Not found element match the selector/)
end

it "raises error when provided invalid xpath selector" do
expect do
browser.wait_for_xpath("div#wait_for_selector")
end.to raise_error(Ferrum::JavaScriptError, /Failed to execute 'evaluate' on 'Document'/)
end

it "waits less than provided timeout when node found" do
Timeout.timeout(1) do
expect(browser.wait_for_xpath("//div[@id='wait_for_selector']", timeout: 2000)).to be_kind_of(Ferrum::Node)
end
end

it "waits for xpath within frame" do
browser.execute <<-JS
setTimeout(function(){
document.body.innerHTML += "<iframe src='about:blank' name='frame'>";
var iframeDocument = document.querySelector("iframe[name='frame']").contentWindow.document;
var content = "<html><body><div id='wait_for_selector_within_frame'></div></body></html>";
iframeDocument.open("text/html", "replace");
iframeDocument.write(content);
iframeDocument.close();
}, 900);
JS
frame = browser.wait_for_xpath("//iframe[@name='frame']").frame
expect(frame.wait_for_xpath("//div[@id='wait_for_selector_within_frame']")).to be_kind_of(Ferrum::Node)
end
end

context "current_url" do
it "supports whitespace characters" do
browser.go_to("/ferrum/arbitrary_path/200/foo%20bar%20baz")
Expand Down
2 changes: 1 addition & 1 deletion spec/network/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Network
%r{/ferrum/jquery.min.js$} => File.size("#{PROJECT_ROOT}/spec/support/public/jquery-1.11.3.min.js"),
%r{/ferrum/jquery-ui.min.js$} => File.size("#{PROJECT_ROOT}/spec/support/public/jquery-ui-1.11.4.min.js"),
%r{/ferrum/test.js$} => File.size("#{PROJECT_ROOT}/spec/support/public/test.js"),
%r{/ferrum/with_js$} => 2343
%r{/ferrum/with_js$} => 2919
}

resources_size.each do |resource, size|
Expand Down
17 changes: 17 additions & 0 deletions spec/support/views/with_js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@
display: inline;
}
</style>
<script>
$(document).ready(function(){
setTimeout(function(){
const div = document.createElement('div');
div.setAttribute('id', 'wait_for_selector');
document.body.appendChild(div);
}, 900);
});
$(document).ready(function(){
setTimeout(function(){
const div = document.createElement('div');
div.setAttribute('id', 'wait_for_hidden_selector');
div.setAttribute('style', 'display:none;');
document.body.appendChild(div);
}, 900);
});
</script>
</head>

<body>
Expand Down