Integrate BrowserStack Automate with Google Cloud Build
A step-by-step guide to help you integrate Google Cloud Build with the BrowserStack device cloud for running all your Selenium tests on BrowserStack Automate.
Google Cloud Build is a service that executes your builds on the Google Cloud Platform (GCP). Cloud Build can import source code from Google Cloud Storage, Cloud Source Repositories,
GitHub, or Bitbucket, to continuously build, test, and deploy projects. It helps catch failures ahead of the production stage and mitigate them as they occur.
To create a pipeline and perform execution in Google Cloud Build, complete the following steps:
Log in to your Google Cloud Build account.
On the dashboard, click the project drop-down and then click New Project.
In your project, click the navigation menu, and select Cloud Build.
On the Cloud Build dashboard, click the Triggers tab.
Click Connect Repository to link your GitHub repository with build and select the source code repository.
Click Create A Trigger to connect the repository to build and add the trigger name, region, and description.
Under the Event section, select the type of trigger.
Under the Source section, select your repository and branch.
Under the Configuration section, select the relevant configuration method ( .yaml or .json) that you want to use.
The following sample YAML file includes the arguments and environment details required to trigger a test case.
Under the Advanced Configuration area, add your BrowserStack Username, BROWSERSTACK_USERNAME, and Access key, BROWSERSTACK_ACCESS_KEY, as variables.
On the Cloud Build dashboard page, click Run to run your test for the repository. Based on the selected trigger method, a new build will be triggered when that action occurs.
Integrate existing test cases
With existing test cases, integrating BrowserStack involves editing your test cases to add BrowserStack capabilities, credentials, and, remote URL.
Though your existing test scripts include capabilities, BrowserStack also provides specific capabilities that help determine how tests are run. The following example code snippet sets the build name, declares Access Key and Username as environment variables, and creates a remote driver connection.
Appending the default BUILD_ID environment variable to the BrowserStack build name helps generate a unique build name for every test run. To learn more about this environment variable, check out the Google Cloud Build pipeline labeling guide.
Set capabilities using the following code snippet:
my$username=$ENV{"BROWSERSTACK_USERNAME"};my$accessKey=$ENV{"BROWSERSTACK_ACCESS_KEY"};my$buildId=$ENV{"BUILD_ID"};my$builName="googlecloudbuild-"+$buildId;my$caps={"build"=>"BStack Build Number:".$buildName,"name"=>"BStack-[Perl] Sample Test",};my$host="$username:$accessKey\@hub-cloud.browserstack.com";my$driver= new Selenium::Remote::Driver('remote_server_addr'=>$host,'port'=>'80','extra_capabilities'=>$caps);
Integrate test cases for privately hosted websites
If you are testing websites hosted locally as part of your testing or development environment, you need to configure your Google Cloud Build pipeline to use the Local testing.
Using the Local testing feature of BrowserStack, remote browsers at the BrowserStack cloud can access websites hosted on your private or internal networks. To learn more about how Local testing works, check out the Local testing guide.
Apart from setting up a Local connection, you must also add the local or browserstack.local capability in your test scripts.
Apart from these configurations, you can set other Local options, such as, testing behind a proxy, folder testing, or using multiple local instances. Check out Introduction to Local Testing for more information.
Add local capability to test scripts
Add the local (W3C protocol) or browserstack.local (JSON Wire protocol) capability to test scripts using the following code snippets. When you set this capability to true, BrowserStack resolves the request via the Local agent running in your network.
After configuring Local, run your test cases from the Google Cloud Build console by following the steps mentioned in the Set up a Google Cloud pipeline section.