lib/puppeteer/page.rb in puppeteer-ruby-0.40.5 vs lib/puppeteer/page.rb in puppeteer-ruby-0.40.6

- old
+ new

@@ -387,19 +387,32 @@ # @return [Array<Hash>] def cookies(*urls) @client.send_message('Network.getCookies', urls: (urls.empty? ? [url] : urls))['cookies'] end + # check if each cookie element has required fields ('name' and 'value') + private def assert_cookie_params(cookies, requires:) + return if cookies.all? do |cookie| + requires.all? { |field_name| cookie[field_name] || cookie[field_name.to_s] } + end + + raise ArgumentError.new("Each coookie must have #{requires.join(" and ")} attribute.") + end + def delete_cookie(*cookies) + assert_cookie_params(cookies, requires: %i(name)) + page_url = url starts_with_http = page_url.start_with?("http") cookies.each do |cookie| item = (starts_with_http ? { url: page_url } : {}).merge(cookie) @client.send_message("Network.deleteCookies", item) end end def set_cookie(*cookies) + assert_cookie_params(cookies, requires: %i(name value)) + page_url = url starts_with_http = page_url.start_with?("http") items = cookies.map do |cookie| (starts_with_http ? { url: page_url } : {}).merge(cookie).tap do |item| raise ArgumentError.new("Blank page can not have cookie \"#{item[:name]}\"") if item[:url] == "about:blank"