lib/puppeteer/page.rb in puppeteer-ruby-0.0.13 vs lib/puppeteer/page.rb in puppeteer-ruby-0.0.14

- old
+ new

@@ -3,11 +3,11 @@ require_relative './page/screenshot_options' class Puppeteer::Page include Puppeteer::EventCallbackable include Puppeteer::IfPresent - using Puppeteer::AsyncAwaitBehavior + using Puppeteer::DefineAsyncMethod # @param {!Puppeteer.CDPSession} client # @param {!Puppeteer.Target} target # @param {boolean} ignoreHTTPSErrors # @param {?Puppeteer.Viewport} defaultViewport @@ -203,17 +203,12 @@ ensure @file_chooser_interceptors.delete(promise) end end - # @param timeout [Integer] - # @return [Future<Puppeteer::FileChooser>] - async def async_wait_for_file_chooser(timeout: nil) - wait_for_file_chooser(timeout: timeout) - end + define_async_method :async_wait_for_file_chooser - # /** # * @param {!{longitude: number, latitude: number, accuracy: (number|undefined)}} options # */ # async setGeolocation(options) { # const { longitude, latitude, accuracy = 0} = options; @@ -303,25 +298,31 @@ # @return {!Promise<?Puppeteer.ElementHandle>} def S(selector) main_frame.S(selector) end + define_async_method :async_S + # `$$()` in JavaScript. $ is not allowed to use as a method name in Ruby. # @param {string} selector # @return {!Promise<!Array<!Puppeteer.ElementHandle>>} def SS(selector) main_frame.SS(selector) end + define_async_method :async_SS + # @param {Function|string} pageFunction # @param {!Array<*>} args # @return {!Promise<!Puppeteer.JSHandle>} def evaluate_handle(page_function, *args) context = main_frame.execution_context context.evaluate_handle(page_function, *args) end + define_async_method :async_evaluate_handle + # @param {!Puppeteer.JSHandle} prototypeHandle # @return {!Promise<!Puppeteer.JSHandle>} def query_objects(prototype_handle) context = main_frame.execution_context context.query_objects(prototype_handle) @@ -333,41 +334,31 @@ # @return [Object] def Seval(selector, page_function, *args) main_frame.Seval(selector, page_function, *args) end - # `$eval()` in JavaScript. $ is not allowed to use as a method name in Ruby. - # @param selector [String] - # @param page_function [String] - # @return [Future] - async def async_Seval(selector, page_function, *args) - Seval(selector, page_function, *args) - end + define_async_method :async_Seval # `$$eval()` in JavaScript. $ is not allowed to use as a method name in Ruby. # @param selector [String] # @param page_function [String] # @return [Object] def SSeval(selector, page_function, *args) main_frame.SSeval(selector, page_function, *args) end - # `$$eval()` in JavaScript. $ is not allowed to use as a method name in Ruby. - # @param selector [String] - # @param page_function [String] - # @return [Future] - async def async_SSeval(selector, page_function, *args) - SSeval(selector, page_function, *args) - end + define_async_method :async_SSeval # `$x()` in JavaScript. $ is not allowed to use as a method name in Ruby. # @param {string} expression # @return {!Promise<!Array<!Puppeteer.ElementHandle>>} def Sx(expression) main_frame.Sx(expression) end + define_async_method :async_Sx + # /** # * @param {!Array<string>} urls # * @return {!Promise<!Array<Network.Cookie>>} # */ # async cookies(...urls) { @@ -691,16 +682,11 @@ # @param wait_until [string|nil] 'load' | 'domcontentloaded' | 'networkidle0' | 'networkidle2' private def wait_for_navigation(timeout: nil, wait_until: nil) main_frame.send(:wait_for_navigation, timeout: timeout, wait_until: wait_until) end - # @param timeout [number|nil] - # @param wait_until [string|nil] 'load' | 'domcontentloaded' | 'networkidle0' | 'networkidle2' - # @return [Future] - async def async_wait_for_navigation(timeout: nil, wait_until: nil) - wait_for_navigation(timeout: timeout, wait_until: wait_until) - end + define_async_method :async_wait_for_navigation # /** # * @param {(string|Function)} urlOrPredicate # * @param {!{timeout?: number}=} options # * @return {!Promise<!Puppeteer.Request>} @@ -829,10 +815,12 @@ # @return {!Promise<*>} def evaluate(page_function, *args) main_frame.evaluate(page_function, *args) end + define_async_method :async_evaluate + # /** # * @param {Function|string} pageFunction # * @param {!Array<*>} args # */ # async evaluateOnNewDocument(pageFunction, ...args) { @@ -980,20 +968,22 @@ # preferCSSPageSize # }); # return await helper.readProtocolStream(this._client, result.stream, path); # } - # @param {!{runBeforeUnload: (boolean|undefined)}=} options - def close - # assert(!!this._client._connection, 'Protocol error: Connection closed. Most likely the page has been closed.'); - # const runBeforeUnload = !!options.runBeforeUnload; - # if (runBeforeUnload) { - # await this._client.send('Page.close'); - # } else { - # await this._client._connection.send('Target.closeTarget', { targetId: this._target._targetId }); - # await this._target._isClosedPromise; - # } + # @param run_before_unload [Boolean] + def close(run_before_unload: false) + unless @client.connection + raise 'Protocol error: Connection closed. Most likely the page has been closed.' + end + + if run_before_unload + @client.send_message('Page.close') + else + @client.connection.send_message('Target.closeTarget', targetId: @target.target_id) + await @target.is_closed_promise + end end # @return [boolean] def closed? @closed @@ -1007,24 +997,19 @@ # @param click_count [Number] def click(selector, delay: nil, button: nil, click_count: nil) main_frame.click(selector, delay: delay, button: button, click_count: click_count) end - # @param selector [String] - # @param delay [Number] - # @param button [String] "left"|"right"|"middle" - # @param click_count [Number] - # @return [Future] - async def async_click(selector, delay: nil, button: nil, click_count: nil) - click(selector, delay: delay, button: button, click_count: click_count) - end + define_async_method :async_click # @param {string} selector def focus(selector) main_frame.focus(selector) end + define_async_method :async_focus + # @param {string} selector def hover(selector) main_frame.hover(selector) end @@ -1033,27 +1018,28 @@ # @return {!Promise<!Array<string>>} def select(selector, *values) main_frame.select(selector, *values) end + define_async_method :async_select + # @param selector [String] def tap(selector) main_frame.tap(selector) end - # @param selector [String] - async def async_tap(selector) - tap(selector) - end + define_async_method :async_tap # @param selector [String] # @param text [String] # @param delay [Number] def type_text(selector, text, delay: nil) main_frame.type_text(selector, text, delay: delay) end + define_async_method :async_type_text + # /** # * @param {(string|number|Function)} selectorOrFunctionOrTimeout # * @param {!{visible?: boolean, hidden?: boolean, timeout?: number, polling?: string|number}=} options # * @param {!Array<*>} args # * @return {!Promise<!Puppeteer.JSHandle>} @@ -1068,37 +1054,27 @@ # @param timeout [Integer] def wait_for_selector(selector, visible: nil, hidden: nil, timeout: nil) main_frame.wait_for_selector(selector, visible: visible, hidden: hidden, timeout: timeout) end - # @param selector [String] - # @param visible [Boolean] Wait for element visible (not 'display: none' nor 'visibility: hidden') on true. default to false. - # @param hidden [Boolean] Wait for element invisible ('display: none' nor 'visibility: hidden') on true. default to false. - # @param timeout [Integer] - async def async_wait_for_selector(selector, visible: nil, hidden: nil, timeout: nil) - wait_for_selector(selector, visible: visible, hidden: hidden, timeout: timeout) - end + define_async_method :async_wait_for_selector # @param xpath [String] # @param visible [Boolean] Wait for element visible (not 'display: none' nor 'visibility: hidden') on true. default to false. # @param hidden [Boolean] Wait for element invisible ('display: none' nor 'visibility: hidden') on true. default to false. # @param timeout [Integer] def wait_for_xpath(xpath, visible: nil, hidden: nil, timeout: nil) main_frame.wait_for_xpath(xpath, visible: visible, hidden: hidden, timeout: timeout) end - # @param xpath [String] - # @param visible [Boolean] Wait for element visible (not 'display: none' nor 'visibility: hidden') on true. default to false. - # @param hidden [Boolean] Wait for element invisible ('display: none' nor 'visibility: hidden') on true. default to false. - # @param timeout [Integer] - async def async_wait_for_xpath(xpath, visible: nil, hidden: nil, timeout: nil) - wait_for_xpath(xpath, visible: visible, hidden: hidden, timeout: timeout) - end + define_async_method :async_wait_for_xpath # @param {Function|string} pageFunction # @param {!{polling?: string|number, timeout?: number}=} options # @param {!Array<*>} args # @return {!Promise<!Puppeteer.JSHandle>} def wait_for_function(page_function, options = {}, *args) main_frame.wait_for_function(page_function, options, *args) end + + define_async_method :async_wait_for_function end