0
0
Selenium Pythontesting~20 mins

Performance metrics collection in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Performance Metrics Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Extracting page load time using Selenium and Performance API

You want to measure the page load time of a website using Selenium in Python. Which code snippet correctly extracts the loadEventEnd and navigationStart timing values and calculates the load time in milliseconds?

Selenium Python
from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://example.com')

# Your code here to get load time

load_time = driver.execute_script('return window.performance.timing.loadEventEnd - window.performance.timing.navigationStart;')
print(load_time)
driver.quit()
Aload_time = driver.execute_script('return window.performance.timing.domComplete - window.performance.timing.domLoading;')
Bload_time = driver.execute_script('return window.performance.timing.loadEventEnd - window.performance.timing.navigationStart;')
Cload_time = driver.execute_script('return window.performance.timing.responseEnd - window.performance.timing.requestStart;')
Dload_time = driver.execute_script('return window.performance.timing.fetchStart - window.performance.timing.navigationStart;')
Attempts:
2 left
💡 Hint

Look for the difference between the event when the page finishes loading and when navigation started.

🧠 Conceptual
intermediate
1:30remaining
Understanding Navigation Timing API properties

Which of the following window.performance.timing properties represents the moment when the browser starts fetching the document?

AfetchStart
BnavigationStart
CdomLoading
DresponseStart
Attempts:
2 left
💡 Hint

Think about when the browser actually begins to request the document from the server.

📝 Syntax
advanced
2:30remaining
Correctly retrieving First Contentful Paint (FCP) metric

You want to get the First Contentful Paint (FCP) metric using Selenium's execute_script method. Which code snippet correctly retrieves FCP from the Performance API?

Selenium Python
fcp = driver.execute_script('''
  const perfEntries = performance.getEntriesByType('paint');
  for (const entry of perfEntries) {
    if (entry.name === 'first-contentful-paint') {
      return entry.startTime;
    }
  }
  return -1;
''')
Afcp = driver.execute_script('return performance.getEntriesByType("paint")[0].startTime;')
Bfcp = driver.execute_script('return performance.getEntriesByName("first-contentful-paint")[0].startTime;')
Cfcp = driver.execute_script('return performance.timing.firstContentfulPaint;')
Dfcp = driver.execute_script('return performance.getEntriesByType("paint").find(e => e.name === "first-contentful-paint").startTime;')
Attempts:
2 left
💡 Hint

Use getEntriesByType('paint') and find the entry with name first-contentful-paint.

🔧 Debug
advanced
2:00remaining
Diagnosing error when accessing Performance API in Selenium

You run this code to get navigation timing but get an error: JavascriptException: window.performance is undefined. What is the most likely cause?

Selenium Python
timing = driver.execute_script('return window.performance.timing')
AThe page has not fully loaded before the script runs, so <code>window.performance</code> is not available yet.
BThe browser driver does not support the Performance API.
CThe URL loaded is a blank page or about:blank, which has no performance data.
DYou need to enable browser performance logging in Selenium options before accessing <code>window.performance</code>.
Attempts:
2 left
💡 Hint

Check what page is loaded before accessing performance data.

optimization
expert
3:00remaining
Efficiently collecting multiple performance metrics in one script

You want to collect loadEventEnd, domContentLoadedEventEnd, and first-contentful-paint metrics in a single Selenium execute_script call. Which code snippet correctly returns all three as a dictionary?

A
return {
  loadEventEnd: performance.timing.loadEventEnd - performance.timing.navigationStart,
  domContentLoadedEventEnd: performance.timing.domContentLoadedEventEnd - performance.timing.navigationStart,
  fcp: performance.getEntriesByType('paint').filter(e =&gt; e.name === 'first-contentful-paint')[0].startTime
}
B
return {
  loadEventEnd: window.performance.timing.loadEventEnd,
  domContentLoadedEventEnd: window.performance.timing.domContentLoadedEventEnd,
  fcp: window.performance.getEntriesByName('first-contentful-paint')[0].startTime
}
C
return {
  loadEventEnd: performance.timing.loadEventEnd,
  domContentLoadedEventEnd: performance.timing.domContentLoadedEventEnd,
  fcp: performance.getEntriesByType('paint').find(e =&gt; e.name === 'first-contentful-paint').startTime
}
D
return {
  loadEventEnd: performance.timing.loadEventEnd,
  domContentLoadedEventEnd: performance.timing.domContentLoadedEventEnd,
  fcp: performance.getEntriesByType('paint')[0].startTime
}
Attempts:
2 left
💡 Hint

Calculate relative times from navigationStart and correctly find the FCP entry.