A step-by-step guide to help you integrate Semaphore with the BrowserStack device cloud for running all your Selenium tests on BrowserStack Automate.
Semaphore is a cloud-based automation service that lets you test and deploys code at the push of a button with hosted continuous integration and delivery. Semaphore offers a clutter-free UI and visual Workflow Builder offers effective pipeline visualizations. Semaphore is free for open-source projects. If you are new to Semaphore or CICD in general, you can read more about it here
An SCM repository on Github or Bitbucket for which you want to activate the CI pipeline and collaborator access to it.
Set up a pipeline in Semaphore
To create a pipeline in Semaphore, complete the following steps:
Go to Semaphore CI and create an organization where you plan to host the project.
Create BrowserStack credential secret for the pipeline. Secrets are used to store and retrieve sensitive data, such as API keys, which should never be committed to source control.
To add your BrowserStack account credentials to Semaphore:
Go to your Organization and click Settings.
Under Secrets, click New Secret.
Enter the BrowserStack account credentials as two environment variables, namely BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY.
Click Save Secret to save the credentials.
Sign in to your Semaphore Organization and connect your repository. You can either use a template starter workflow or customize it in the workflow builder. In your workflow editor for the project, click the Secrets tab, and verify that the BrowserStack credentials secret is selected.
You can follow the official Semaphore Guide to create a project.
Click Run the Workflow to generate a new branch set-up-semaphore along with semaphore.yml configuration file. The following code shows a sample configuration file.
version: v1.0
name: Test MyApp
agent:machine:type: e1-standard-2os_image: ubuntu2004
blocks:-name: Build and Test
task:jobs:-name: E2E Test on Browserstack
commands:- checkout
- sem-version node 12
- cache restore
- npm install
- cache store
- npm run test
secrets:-name: Browserstack Creds
After the pipeline setup is complete, commit and push your changes to the integrated SCM repository that you used to set the Semaphore pipeline.
Open the Semaphore CI dashboard, click Projects and select your project.
In order to integrate your existing test cases to BrowserStack, ensure that you have added the BrowserStack Capabilities, Credentials, and Remote URL before creating the Remote WebDriver. You can use the environment variables to pass the credentials to your test scripts.
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 os to Windows and the browser to Chrome, declares Username and Access Key from environment variables and creates a remote driver connection.
Using the default SEMAPHORE_JOB_NAME and SEMAPHORE_JOB_ID environment variables in the BrowserStack build name helps generate a unique build name for every test run. To learn more about this environment variable, check out the Semaphore Environment Variables Reference page.
username =ENV['BROWSERSTACK_USERNAME']
accessKey =ENV['BROWSERSTACK_ACCESS_KEY']
buildName =ENV['SEMAPHORE_JOB_NAME']
buildID =ENV['SEMAPHORE_JOB_ID']# to run on Chrome
options =Selenium::WebDriver::Options.chrome
capabilities ={'bstack:options'=>{"buildName"=>"semaphore-"+ buildName +"-"+ buildID,"seleniumVersion"=>"4.0.0",},}
options.add_option('bstack:options', bstack_options)
driver =Selenium::WebDriver.for(:remote,:url=>"https://"+username+":"+accessKey+"@hub.browserstack.com/wd/hub",:capabilities=> options)
my$username=$ENV{"BROWSERSTACK_USERNAME"};my$accessKey=$ENV{"BROWSERSTACK_ACCESS_KEY"};my$buildName=$ENV{"SEMAPHORE_JOB_NAME"};my$buildID=$ENV{"SEMAPHORE_JOB_ID"};my$caps={'bstack:options'=>{"buildName"=>"semaphore-"+$buildName+"-"+$buildID,"seleniumVersion"=>"4.0.0",},}my$host="$username:$accessKey\@hub-cloud.browserstack.com";my$driver= new Selenium::Remote::Driver('remote_server_addr'=>$host,'port'=>'80','extra_capabilities'=>$caps);
If you are testing websites hosted locally as part of your testing or development environment, you need to configure your Semaphore pipeline to use Local testing.
Ensure that the Internal Testing or development environment are available and accessible on the network where Semaphore is running.
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 browserstack.local capability in your test scripts.
Using Local binary, the remote browsers in the BrowserStack cloud are able to access your private or locally-hosted website through the connection established between BrowserStack and the Local binary running on your machine.
To create Semaphore pipeline that uses BrowserStack Local, complete the following steps:
Complete steps 1-6 mentioned in the [Setup a Semaphore pipeline]((#set-up-a-pipeline-in-semaphore) section.
Under the commands section, along with the commands that your tests need to run, add the following commands based on your OS.
These commands use the --daemon start and --daemon stop flags to start and stop Local binary respectively. To learn about additional flags used in running Local binary, check out the Binary parameter guide.
# For Linux-based systems, add the following commands in the given console to download the binary, run it, and stop its execution after the test has been executed.
$ wget"https://www.browserstack.com/browserstack-local/BrowserStackLocal-linux-x64.zip"
$ unzip BrowserStackLocal-linux-x64.zip
$ ./BrowserStackLocal --key $BROWSERSTACK_ACCESS_KEY --daemon start
$ <your-test-command>
$ ./BrowserStackLocal --key $BROWSERSTACK_ACCESS_KEY --daemon stop
# For macOS-based systems, add the following commands in the given console to download the binary, run it, and stop its execution after the test has been executed.
$ wget"https://www.browserstack.com/browserstack-local/BrowserStackLocal-darwin-x64.zip"
$ unzip BrowserStackLocal-darwin-x64.zip
$ ./BrowserStackLocal --key $BROWSERSTACK_ACCESS_KEY --daemon start
$ <your-test-command>
$ ./BrowserStackLocal --key $BROWSERSTACK_ACCESS_KEY --daemon stop
# For Windows-based systems, add the following commands in the given console to download the binary, run it, and stop its execution after the test has been executed.
$ wget"https://www.browserstack.com/browserstack-local/BrowserStackLocal-win32.zip"
$ powershell.exe Expand-Archive BrowserStackLocal-win32.zip
$ ./BrowserStackLocal-win32/BrowserStackLocal.exe --key %BROWSERSTACK_ACCESS_KEY% --daemon start
$ <your-test-command>
$ ./BrowserStackLocal-win32/BrowserStackLocal.exe --key %BROWSERSTACK_ACCESS_KEY% --daemon stop
Save the .semaphore.yml file, push it in your repository and run a New Build.
Note: To download another Local binary version, check out the releases and downloads section.
If you prefer to manage the Local connection through your test scripts, you can use the language bindings.
Note: Ensure to complete the steps mentioned in the set up Semaphore pipeline section to create your Semaphore pipeline.
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 browserstack.local capability to test scripts
Add the browserstack.local 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.