CrossBrowserTesting.com

A Design, Development, Testing Blog

  • Product Overview
  • Pricing
  • Back Home
  • Free Trial
  • Design
  • Development
  • Manual Testing
  • Test Automation
  • Visual Testing

Getting Started With CrossBrowserTesting’s API

August 15, 2019 By Andrew Elick

APIs are a powerful way to connect to a company’s products and services to use their existing infrastructure seamlessly.  Here at CrossBrowserTesting we are no exception to this statement

What is an API?

The term API is an acronym, and it stands for “Application Programming Interface.”

APIs make your life as a developer much easier by allowing you to connect to specific resources and access relevant information.  By creating a simple endpoint to talk to, APIs take away the hassle of handling and transforming the data that you are dealing with.

To connect with an API you simply call a http request to the correct URL that is needed. You may request data or send it the endpoint, either way it is a very clean and simple way to communicate back and forth.

With CrossBrowserTesting’s API you can connect to a variety of different services to conduct your testing without the need of our web-app. Here is a list of our main services that are supported through our API:

  1. Live Tests
  2. Automation Tests
  3. Screenshots
  4. Record & Replay

You can view all of our support services at our API website here

Where do I start?

Getting started using our API is quite easy! I will show you how I would start a Screenshot test using CrossBrowserTesting’s API! In this example I will be writing our API calls in Python 3.7 using the Requests library. Here is a guide to installing your Python environment if needed: Python Installation Guide

To install the Requests library, open up a terminal and run this command:

pip install requests

We will need to get our login credentials so that we can launch our tests from our account. You can find these by scrolling to the bottom of this page

You will see that we have passed our login information directly into the URL that we are talking to. This is called basic authentication; this allows us to securely communicate with CrossBrowserTesting’s infrastructure.

Setting up the test

Before we begin structuring our API request, we will need the proper URL endpoint so that we can talk to CrossBrowserTesting’s service. For screenshots this will be:

crossbrowsertesting.com/api/v3/screenshots

After that we will begin writing out our API request! We will need to get a list of device and browser combinations that we would like to use for our tests. The way I prefer to get these is by going to this screenshots page and clicking the TRY button. Do not worry this may take a minute to load in all of the results.

Once we have all of the results, I have picked out the api_name parameter for the device and the browser combination for a couple different devices. Here’s some listed below:

Device/Browser Combo Device Browser
Pixel 2 with Chrome 74 Pixel2-And90 MblChrome74
iPhone XR sim with Safari 12 iPhoneXRSim MblSafari12.0
Mac OSX 10.14 with Firefox 67 Mac10.14 FF67
Windows 10 with Chrome 75 Win10 Chrome75x64

Let’s build the test

Now that we have all the required components to start our API screenshot test, let’s write the test!

First, we need to import all the packages that we will be using in our test:

import json
import time
from requests import post, get

The json package is used for parsing our data that we get back from CrossBrowserTesting.  The time package is used for setting delays used in our test.  And finally, the requests package is used to send the request to CrossBrowserTesting.

Next, we will add all of the parameters that we will need to let CrossBrowserTesting who we are and what we are trying to do:

#Your CrossBrowserTesting credentials
user = “YOUR_EMAIL”
authkey = “YOUR_AUTHKEY”

#Screenshot parameters
test_url = "https://google.com/"
devices_list = "Pixel2-And90|MblChrome74, iPhoneXRSim|MblSafari12.0, Mac10.14|FF67, Win10|Chrome75X64"

We have added our CrossBrowserTesting credentials that will be used to access our account. We have also added our devices that we want to test as well as the URL that we want to check.

Now let’s send the request with all of our parameters to CrossBrowserTesting to begin the test:

#Send the request to CrossBrowserTesting to start the test
request = post("https://"+user+":"+authkey+"@crossbrowsertesting.com/api/v3/screenshots/?browsers="+devices_list+"&url="+test_url)
results = request.json()

This takes all of our information that we set earlier and put into one URL string. This data is then sent to CrossBrowserTesting and handled to begin the test!

Our next part we will create a loop to check if the test is finished every 10 seconds:

This may appear a bit lengthy and complicated, but I promise you it is actually quite simple!  First, we set some variables to keep the state of our test and get our current test ID to check it.

#active test
active_test = True

#Used to store finished test results
finished_test_results = None

#Test ID
test_id = results['screenshot_test_id']

#Check if test is complete, if not try again in 10s
while active_test:
    print ("Checking if test is still active")

    #Send HTTP request to CrossBrowserTesting to see if test is still running
    request = get("https://"+user+":"+authkey+"@crossbrowsertesting.com/api/v3/screenshots/"+str(test_id))
    results = request.json()

    #Status of the test
    test_status = results['versions'][0]['active']

    #Check active parameter if is false, if so stop the loop
    if test_status is False:
        active_test = False

        #Put test results into finished test results
        finished_test_results = results['versions'][0]

        print("Test is complete!")

    else:
        print ("Test is still running, checking again in 10 seconds")

        #Wait 10 seconds and retry loop
        time.sleep(10)

Inside of our while loop you will see that we send a request to CrossBrowserTesting every time to see if the test is still running. If it is, we wait 10 seconds and check again.

When the test is completed, we will then get the pass/fail results and the URL of our test:

#Test results URL
test_url = finished_test_results['show_results_web_url']

#Get pass/fail from test results
test_results = finished_test_results['result_count']
pass_results = str(test_results['successful'])
fail_results = str(test_results['failed'])

#Show test results
print ("Pass: "+pass_results+" Failed: "+fail_results)

print("To view these results visit: "+test_url)

Once the test is finished this will show us if it passed or failed, as well as letting us check the status in CrossBrowserTesting’s web application.

Finally, here is our complete test that will run everything we went through

import json
import time
from requests import post, get

#Your CrossBrowserTesting credentials
user = “YOUR_EMAIL”
authkey = “YOUR_AUTHKEY”

#Screenshot parameters
devices_list = "Pixel2-And90|MblChrome74, iPhoneXRSim|MblSafari12.0, Mac10.14|FF67, Win10|Chrome75X64"
test_url = "https://google.com/"

#Send the request to CrossBrowserTesting to start the test
request = post("https://"+user+":"+authkey+"@crossbrowsertesting.com/api/v3/screenshots/?browsers="+devices_list+"&url="+test_url)
results = request.json()

#Wait 15 seconds for test to run before checking
print ("Waiting 15 seconds for test to begin processing...")
time.sleep(15)

#active test
active_test = True

#Used to store finished test results
finished_test_results = None

#Test ID
test_id = results['screenshot_test_id']

#Check if test is complete, if not try again in 10s
while active_test:
    print ("Checking if test is still active")

    #Send HTTP request to CBT to see if test is still running
    request = get("https://"+user+":"+authkey+"@crossbrowsertesting.com/api/v3/screenshots/"+str(test_id))
    results = request.json()

    #Status of the test

Running the test:

Now that we have everything set up it is time to actually run our test! With python this is super simple, and will let us quickly get this thing going.

Open up a terminal on your device:

Next we need to go to where ever we saved our test file at. For mine I saved it in my OneDrive folder on my desktop.

Time to run the test:

Running the test is as simple as typing python and then the filename.

`python test.py`

If you are running this on OSX you may need to use python3

After doing so you will start to see the test run and see it complete!

Filed Under: Test Automation

Try CrossBrowserTesting

Everything you need for testing on the web. Browsers & Devices included.


  • Grid In The Cloud
  • Simple CI Integrations
  • Native Debugging Tools
  • Real iOS and Android
  • 2050+ Browser/OS Combinations
  • Intuitive REST API

Start Testing Today

Want Testing Tips?

Want the latest tips & strategies from industry experts right to your inbox? Sign up below.
 

Join Over 600,000 Testers & Developers And Start Testing Today

Learn more Free Trial

Features

  • Live Testing
  • Selenium Automation
  • Automated Screenshots
  • Screenshot Comparison
  • Local Testing
  • Real Devices

Solutions

  • Automated Testing
  • Visual Testing
  • Manual Testing
  • Enterprise
  • Internet Explorer

Resources

  • Browsers & Devices
  • Blog
  • Webinars
  • Integrations
  • ROI Calculator

Company

  • About Us
  • Careers
  • Plans
  • Terms of use
  • Security

Support

  • Help Center
  • API Docs
  • Schedule A Demo
  • Contact Us
  • Write For Us