Skip to main content
Experience faster, smarter testing with BrowserStack AI Agents. See what your workflow’s been missing. Explore now!
No Result Found
Get your setup working faster. Join our Discord for optimisation tips from elite testers. Join our DiscordJoin our Discord

Disable CORS restriction

Learn how to disable CORS restriction in the Safari browser for running tests in BrowserStack Automate

In this guide, you will learn:

What is CORS?

Cross-Origin Resource Sharing (CORS) is a mechanism that enables web pages to access resources running on a restricted or different domain. BrowserStack provides the disableCorsRestrictions capability to disable CORS restrictions on the Safari browser.

Using the disableCorsRestrictions capability in your test script enables your publicly available or locally-hosted website to access resources from another domain, server, or APIs.

Disabling CORS restriction may cause security issues in your website under test. Use this capability only after careful consideration.

Sample screenshots with and without CORS restriction

The following sample screenshots show the content of https://polar-inlet-22085.herokuapp.com with and without CORS restriction disabled:

Due to CORS restriction, https://polar-inlet-22085.herokuapp.com fails to fetch content from another domain on its page.

With CORS restriction

Supported versions

The following table provides a list of supported browsers and OS platforms where you can use the disableCorsRestrictions capability.

Browser/OS Supported versions
Safari browser v12 and above
macOS platform Golden Gate, Tahoe, Sequoia, Sonoma, Ventura, Monterey, Big Sur, Catalina, and Mojave

Set the capability

If you are using BrowserStack SDK, you can set the following capabilities in the browserstack.yml file:

Capability Description Expected values
disableCorsRestrictions Disable CORS restrictions in the Safari browser, which is enabled by default. true: Disables CORS restriction
false(default): Enables CORS restriction
Copy icon Copy

Example test script

In the following example test script, we run a sample test that disables CORS restriction, opens https://polar-inlet-22085.herokuapp.com on Safari browser, and prints screenshot of the web page in the Text Logs section of the Automate session dashboard:

import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.OutputType;
import java.util.concurrent.TimeUnit;
import java.net.URL;
import java.time.Duration;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class JavaSample {
    WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
    try {
      driver.get("https://polar-inlet-22085.herokuapp.com/");
      new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.titleIs("CORS Tester"));
      // wait for 5 seconds for the web page to load completely
      TimeUnit.SECONDS.sleep(5);
      // print screenshot of the web page in the 'Text Logs' section of your Automate session dashboard
      driver = (RemoteWebDriver) new Augmenter().augment(driver);
	    ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    }
    catch (Exception e){
      // print any exception in the IDE's console
      System.out.println(e);
    }
  driver.quit();
  }
}

After you run the test script, a screenshot of the web page is displayed in the Text Logs section of your Automate session dashboard, as shown in the following image:

CORS session view

Need some help?

If you need any help, contact our Support team.

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback!

Talk to an Expert
Download Copy Check Circle