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 #82

Open
route opened this issue May 20, 2020 · 4 comments · May be fixed by #236
Open

Implement wait_for_selector #82

route opened this issue May 20, 2020 · 4 comments · May be fixed by #236
Assignees
Labels
enhancement New feature or request

Comments

@route
Copy link
Member

route commented May 20, 2020

https://github.com/puppeteer/puppeteer/blob/master/docs/api.md#pagewaitforselectorselector-options

@Daniel-ltw
Copy link

      sleep(@browser.slowmo) if slowmoable && @browser.slowmo > 0

Is this part of addressing this?
Was thinking of just doing a sleep on slowmo define time, we can just do an interval loop to wait on a specific element on the page to show.

Because of this sleep, my current test run seems to take too long to complete.

@Daniel-ltw
Copy link

This is my hacky attempt in my own spec_helper.rb. Rather than just using slowmo.

def wait_for(css_element, visibility: :all)
  @staled_retries ||= 120
  element = first(css_element, visible: visibility, wait: 0.3)
  warn_log "Found #{css_element}"
  yield(element) if block_given?
  element
rescue Capybara::ElementNotFound, Capybara::ExpectationNotMet => e
  warn_log "#{css_element} stale for: #{@staled_retries}"
  if @staled_retries.zero?
    page_path = save_page
    screenshot_path = save_screenshot
    debug_log "Page at #{page_path}"
    debug_log "Screenshot at #{screenshot_path}"
    raise e
  else
    @staled_retries -= 1
    sleep(0.2)
    retry
  end
end

@shreeve
Copy link

shreeve commented Mar 21, 2024

I've been using this... seems like something like this should be in Ferrum.

module Ferrum
  class Frame
    module DOM

      #
      # Finds a node using XPath or a CSS path selector, with waiting.
      #
      # @param [String] selector
      #   The XPath or CSS path selector.
      #
      # @param [Integer] init
      #   How long we should wait before starting to look.
      #
      # @param [Integer] wait
      #   How long we should wait for node to appear.
      #
      # @param [Integer] step
      #   How long to wait between checking.
      #
      # @return [Node, nil]
      #   The matching node.
      #
      # @example
      #   browser.with("a[aria-label='CA']", init:1, wait:5, step:0.2) # => Node
      #
      def with(selector, init:nil, wait:1, step:0.1)
        sleep(init) if init
        meth = selector.start_with?("/") ? :at_xpath : :at_css
        until node = send(meth, selector) rescue nil
          (wait -= step) > 0 ? sleep(step) : break
        end
        node
      end
    end
  end
end

I use it like this:

site.with("#iux-identifier-user-id", init: 2, wait:8, step: 0.5).focus.type(user)

@shreeve
Copy link

shreeve commented Mar 21, 2024

This is quite a bit simpler than #236, granted this one is just pure Ruby version and doesn't use js timeouts, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants