rubyselenium-webdrivercapybarapage-load-time

Ruby-Capybara Page load Wait [ To set up a method in Ruby-Capybara-Selenium-Cucumber embeded framework for a web Automation]


I have to calculate the Webpage-Rendering time on each navigation with in the Automation framework created with embeded[Ruby-capybara-selenium-cucumber]. some Webpages are Ajax developed.

#Xpath
SIGNIN_BTN = "//div/button[contains(.,'Sign In')]"    
VERIFY_BTN = "//div/button[contains(.,'NEXTBUTTON')]"

t1 = Time.now    
      visit "http://google.com"    
t2 = Time.now   
pageloadtime1 = t2-t1  
puts pageloadtime1 

t3 = Time.now  
find(:xpath, SIGNIN_BTN).click  
t4 = Time.now  
pageloadtime2 = t4-t3
puts pageloadtime2 

t5 = Time.now   
find(:xpath, VERIFY_BTN ).click  
t6 = Time.now  
pageloadtime3 = t6-t5
puts pageloadtime3

But as per logic, it doesn't give a complete page-rendering time,
instead at a click instantly the time is given as output before the
page-rendering/DOM is completed.


Solution

  • This works for me.

    calculating with Ruby and Jscript - document.readyState, Create a generic method and call it.

    SIGNIN_BTN = "//div/button[contains(.,'Sign In')]" 
    NEXT_BTN = "//div/button[contains(.,'NEXTBUTTON')]"
    
    visit "https://gmail.com/"  
    timewait = pagetime_time  
    puts "LAUNCH GMAIL | PAGELOAD TIME :#{timewait}"  
    
    find(:xpath, SIGNIN_BTN).click  
    timewait = pagetime_time  
    puts "SIGN IN GMAIL | PAGELOAD TIME :#{timewait}"  
    
    find(:xpath, NEXT_BTN ).click  
    timewait = pagetime_time  
    puts "NEXT GMAIL | PAGELOAD TIME :#{timewait}"  
    
    def pagetime_time  
      time1 = Time.now  
      timeloop = 0  
      while timeloop < 500  
        if (Capybara.current_session.driver.execute_script('var browserState = document.readyState; return browserState;') == 'complete')  
          time2 = Time.now  
          break  
        end  
        timeloop + 1  
      end  
      timeWait = (time2 - time1) * 1000  
      return timeWait  
    end