Search code examples
rubygoogle-chromeseleniumwait

Selenium in ruby/chrome, Selenium wait will not function


No matter what I try to do, the browser tries to run the test too fast before it has a chance to find the element that I am looking for. If I put in a simple "sleep 2", it has a chance for the drop down menu to drop and load and successfully find the element. But I am wanting to learn to use the Selenium Wait command. I have tried numerous combinations of the below and looked all over the web for documentation or perhaps examples. I have found plenty of firefox and people say that some of the things below worked perfectly in firefox, but for me and my project team mates, we can not get any of the waits, implicit or explicit, to pause long enough for it to detect the element. The element does not exist until the drop down menu is fully dropped, then it can detect it. It doesn't take 2 seconds but it seems that none of my wait commands will actually make it wait. Like I said, I have tried numerous different things and almost all of them are below. If anyone can help guide me, i would appreciate it. Here is some of the code I have tried:

def setup
    @driver = Selenium::WebDriver.for :chrome
    @driver.get "https://website.herokuapp.com/"
    @wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
    # @driver = Selenium::WebDriver.for:chrome
    # @driver.manage.timeouts.implicit_wait = 30
    # @wait = Selenium::WebDriver::Wait.new(:timeout => 15)
    # @wait = Selenium::WebDriver::Wait.new(:timeout => 10)
    # @driver.manage.timeouts.implicit_wait = 10
    # @wait = Selenium::WebDriver::Wait.new(timeout: 10)
    @driver.manage.window.maximize()
    # @driver.navigate.to("https://website.herokuapp.com/")

end

def test_user_name_is_present
    login()

    @driver.find_element(:class, "navbar-toggle").click()
    # user = @driver.find_element(:class, "dropdown-toggle").text
    # @wait.until{@driver.find_element(:class, "dropdown-toggle")}
    @driver.find_element(:class, "dropdown-toggle")
    @wait.until { @driver.find_element(:class => "dropdown-toggle") }
    user = @driver.find_element(:class, "dropdown-toggle").text
    assert_equal(true, user.include?('HEATHER'), "no user") 
  end 

Solution

  • I'm more familiar with JavaScript or Java bindings.

    But what about:

    def test_user_name_is_present
        login()
    
        @driver.find_element(:class, "navbar-toggle").click()
        @wait.until { @driver.find_element(:class => "dropdown-toggle").displayed? }
        user = @driver.find_element(:class, "dropdown-toggle").text
        assert_equal(true, user.include?('HEATHER'), "no user") 
      end