The Sauce Labs Cookbook

Sauce Headless

Front End Performance Testing

Insights

External Resources

More Info


Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

 

Info
titleExtended Debugging Required

This feature is only available for customers that are executing their tests on Chrome with Extended Debugging enabled. Click here to learn more

Tip
titleCustom Commands are in beta.

The feature is currently in beta and some commands might not work as expected. Please at your own risk.

As part of our effort to help our customers improve the quality and coverage of their tests, we have released a series new custom commads. 

Intercepting Network Requests

Allows modifying any request made by the browser. The customer can blacklist, modify or redirect these for testing needs.

Usecases 

To enable networking capabilities for tests and generate custom log files, use these Sauce Labs options for the Selenium JavaScript Executor and our extended debugging features. You can find more examples of this feature written in Node.js, Python, and Java on GitHub.

You must have Extended Debugging enabled to use these commands.

Include Page
_chrome_only
_chrome_only

See the following sections for more information:

Table of Contents
maxLevel3

Test Environment Tools

Test Environment Tools simulate network conditions and manipulate network requests to analyze and improve test performance. 

Intercept Network Requests

Allows modification of requests made by the browser. You can alter these as your tests require:

  • Blacklist requests to 3rd party vendors
  • Modify requests to REST API (Mock REST API response)
  • Redirect certail certain parts of the app 
  • Insert /or change headers
JavaScript Executor Command
Required
Request ParametersSample Code

sauce:intercept + redirect

Request Parameters

url: a string that matches

outgoing request url, wildcards

an outgoing request URL. Wildcards are supported,

e.g. 

for example https://saucelabs.com/rest/v1/* would match all v1 rest API requests

redirect | respond: action on request
  • redirect: string representing an absolute url that the original request gets redirected to
  • respond:object containing:

    .

    redirect is a string representing an absolute URL where the original request is redirected. In the example, this is "https://google.com"

    Code Block
    languagepy
    driver.execute_script('sauce:intercept', {
        "url": "https://saucelabs.com",
        "redirect": "https://google.com"
    })

    sauce:intercept + response

    url: a string that matches an outgoing request URL. Wildcards are supported, for example https://saucelabs.com/rest/v1/* would match all v1 rest API requests.

    response: an object that represents the response to send when the requested URL is intercepted. There are three options:

      • statusCode: number representing the request status (200)
      • headers: list of key/value pairs that is set as header variables, for example Cookie: "foo=bar"
      • rawResponse: actual response payload as a string
    Code Block
    languagepy
    titlePython example
    driver.execute_script(
    '
    "sauce:intercept
    '
    ", {
    	"url": "http://sampleapp.appspot.com/api/todos",
            "response": {
                "headers": {
                    "x-custom-header": "foobar"
                },
                "body": [{
                    "title": "Hello",
                    "order": 1,
                    "completed": false,
                    "url": "
    https
    http://
    saucelabs.com",
    todo-backend-express.herokuapp.com/15727"
        
    "redirect"
            }]
            }
        })
    
    sauce:intercept+ error

    url: a string that matches an outgoing request URL. Wildcards are supported, for example https://saucelabs.com/rest/v1/* would match all v1 rest API requests.

    error: error values are:

    • 'Failed'
    • 'Aborted'
    • 'TimedOut'
    • 'AccessDenied'
    • 'ConnectionClosed'
    • 'ConnectionReset' 
    • 'ConnectionRefused'
    • 'ConnectionAborted'
    • 'ConnectionFailed'
    • 'NameNotResolved'
    • 'InternetDisconnected'
    • 'AddressUnreachable'
    Code Block
    languagepy
    driver.execute_script("sauce:intercept", {
    	"url": "https://
    browserstack
    saucecon.com/wp-content/uploads/2017/07/SauceCon-hero-img-100-2.jpg",
    
    })
        error: "Failed"}


    Network Intercept Example

    Code Block
    languagepy
    titlePython example
    driver.execute_script('sauce:intercept', {
        "url": "https://saucelabs.com",
        "redirect": "https://

    ...

    google.com"
    })
    # or
    driver.execute_script('sauce:intercept', {
        "url": "https://saucelabs.com/res7/v1/build/32hmv4j32v4j2j3v4j24v232vj4j",
        "

    ...

    response": {
            "statusCode": 200,
            "headers": {
                "etag": 3244224432,
                "Cookie: "foo=bar",
                ...
            },
            "rawResponse": "{\"username\":\"..."
        }
    })

    Throttle

    ...

    MethodPath TemplateJavaScript Executor
    POST/session/{session id}/sauce/ondemand/throttlesauce:throttle

    ...

    CPU Capabilities

    Mobile devices have less CPU power than most desktops and laptops (or a VM's default configuration). You can use CPU Throttling to simulate how your app will run on slower systems, helping you identify possible performance issues.

    JavaScript Executor CommandRequest ParametersExample
    sauce:throttleCPU

    ratea number defining the amount of slowdown (e.g. 2 ~ 2x slowdown, your CPU will be 2 times slower than usual)

    Code Block
    languagepy
    driver.execute_script('sauce:throttleCPU', {
    	"rate": 4
    })

    Throttle Network Capabilities

    With network conditioning you can test your site on a variety of network connections, including Edge, 3G, and even offline.

    ...

    You can throttle the data throughput, including the maximum download and upload throughput

    ...

    , and use latency manipulation to enforce a minimum delay in connection round-trip time (RTT).

    ...

    • session id
    JavaScript Executor CommandRequest ParametersExample
    sauce:throttleNetwork

    condition:

    ...

    a string or object representing

    ...

    • upload
    • download
    • latency

    ...

    • null

    Example

    browser network conditions.

    Code Block
    languagepy

    ...

    driver.execute_script('sauce:

    ...

    throttleNetwork', {
    	"condition": "GPRS"
    })
    # or
    driver.execute_script('sauce:

    ...

    throttleNetwork', {
    	"condition": {
    		"download": 1000,
    		"upload": 500,
    		"latency": 40
        }
    })

    Network Timing

    MethodPath Template
    POST/session/{session id}/log

    By using a custom sauce log type called "timing" a user can receive page metrics on network timings defined by the PerformanceTiming. All timings are relative to "navigationStart" for simplification purposes (absolute timings as described in the API are useless for the cusomer for testing purposes).

    ...

    • session id

    ...

    • type: string - value has to be "sauce:timing"

    ...

    Anchor
    valid_profiles
    valid_profiles

    Strings

    Profile NameDownload Speed (kb/s)Upload Speed (kb/s)Round Trip Time (ms)
    offline000
    GPRS5020500
    Regular 2G25050300
    Good 2G450150150
    Regular 3G750250100
    Good 3G1Mb/s75040
    Regular 4G4Mb/s3Mb/s20ms
    DSL2Mb/s1Mb/s5ms
    WiFi30Mb/s15Mb/s2ms
    online
    No Restrictions
    No Restrictions
    No Restrictions

    Anchor
    custom_objects
    custom_objects

    Custom objects

    You can create custom network conditions with objects. You must define the download speed (in bytes/second), upload speed (in bytes/second), and latency (in milliseconds) for the custom condition, as shown in the example.

    Extended Debugging Tools

    Extended Debugging Tools provide additional logs to analyze test performance. 

    Network Log

    The Sauce Labs network log records all network requests being made by the page currently open in the browser.

    Log TypeResponse
    sauce:network
    Code Block
    titleSample response
    [{
        "

    ...

    url": 

    ...

    "http://saucelabs.com/beta/dashboard",
        "

    ...

    statusCode": 

    ...

    200,
        "

    ...

    method": 

    ...

    "GET",
        "

    ...

    requestHeaders": 

    ...

    {
        

    ...

     

    ...

       ...
     

    ...

     

    ...

      },
        "

    ...

    responseHeaders": 

    ...

    {
        

    ...

     

    ...

       ...
     

    ...

     

    ...

      },
        "

    ...

    timing": 

    ...

    {
            "

    ...

    blocked": -1,
            "

    ...

    connect": 

    ...

    -1,
            "

    ...

    dns": 

    ...

    -1,
            "

    ...

    receive": 

    ...

    0,
            "

    ...

    send": 

    ...

    0,
        

    ...

     

    ...

       

    ...

    "

    ...

    ssl": 

    ...

    -1,
        

    ...

     

    ...

       

    ...

    "

    ...

    wait": 

    ...

    0
        

    ...

    }
    }, {
       

    ...

     ...
    }]

    Application Metrics

    MethodPath Template
    POST/session/{session id}/log

    By using a custom sauce log type called "metrics" a user can receive application related metrics. All timings metrics are similar to the timings log relative to the page load time.

    ...

    • session id

    ...

    • type: string - value has to be "sauce:metrics"

    ...

    Object with the format of the following sample response:

    Performance Log

    The Sauce Labs performance log contains performance-related metrics.

    Log TypeResponse
    sauce:performance
    Code Block
    titleSample response
    {
      

    ...

    "

    ...

    load": 

    ...

    1083,
      

    ...

    "

    ...

    speedIndex": 

    ...

    905,
      

    ...

    "

    ...

    firstInteractive": 

    ...

    1073,
      

    ...

    "

    ...

    firstVisualChange": 

    ...

    576,
      

    ...

    "

    ...

    lastVisualChange": 

    ...

    1243,
      

    ...

    "

    ...

    firstMeaningfulPaint": 

    ...

    1239,
      

    ...

    "

    ...

    firstCPUIdle": 

    ...

    1239,
      

    ...

    "

    ...

    timeToFirstByte": 

    ...

    69,
      

    ...

    "

    ...

    firstPaint": 

    ...

    559,
      

    ...

    "

    ...

    estimatedInputLatency": 

    ...

    16,
      

    ...

    "

    ...

    firstContentfulPaint": 

    ...

    630,
      

    ...

    "

    ...

    score": 0.

    ...

    9947067807295903,
      

    ...

    "

    ...

    domContentLoaded": 

    ...

     

    1073
    }