1

I have custom JS function about some of page performance details Doing automation via ruby-rspec-capybara Having next code:

    visit page
    execute_async_script("function getPagePerformance(){if (window.performance && window.performance.timing) {
    let timing = window.performance.timing;
    let timing_obj = {};
    /*timing*/
    timing_obj.pageLoadingTime = timing.loadEventEnd - timing.navigationStart;
    timing_obj.dnsHandshake = timing.domainLookupEnd - timing.domainLookupStart;
    timing_obj.tcpConnect = timing.connectEnd - timing.connectStart;
    timing_obj.timeToReachServer = timing.responseStart - timing.requestStart;
    timing_obj.serverResponseTime = timing.responseEnd - timing.responseStart;
    timing_obj.totalNetworkLatency = timing.responseEnd - timing.fetchStart;
    timing_obj.domInteractive = performance.timing.domInteractive - performance.timing.navigationStart;return timing_obj;}}
getPagePerformance()")

I've tried it with all methods that ruby actually has:

execute_async_script
execute_script
evaluate_script

And there is no results in console But they are if im doing it manually In additional i've tried to set alerts or console.log inside function just to check if all lines are executed at all - all alerts are being displayed during test running

I've added waiting to make sure that page is loaded (just to exclude questions about this case)

my question is about why this function doesn't return values when test is run

z4elovek
  • 11
  • 4

1 Answers1

0

The call doesn't return anything because your script doesn't return anything.

Here's an example:

timing = execute_script(%{
  var t = window.performance.timing;
  return {
    pageLoadingTime: t.loadEventEnd - t.navigationStart,
    dnsHandshake: t.domainLookupEnd - t.domainLookupStart,
    tcpConnect: t.connectEnd - t.connectStart,
    timeToReachServer: t.responseStart - t.requestStart,
    serverResponseTime: t.responseEnd - t.responseStart,
    totalNetworkLatency: t.responseEnd - t.fetchStart,
    domInteractive: t.domInteractive - t.navigationStart
  };
})
Florent B.
  • 41,537
  • 7
  • 86
  • 101
  • Yep, thanks a lot. it helped and it works if use without wrapping in function: 'if (window.performance && window.performance.timing) { let timing = window.performance.timing; let timing_obj = {}; ... return timing_obj; }' – z4elovek Sep 14 '17 at 10:42