Getting Started With Jenkins and CrossBrowserTesting
For this document, we provide example tests located in our Jenkins plugin Github Repository.
Jenkins is a continuous integration tool that lets you automate your development process quickly, safely, and at scale. In this guide we will use Jenkins along with our Jenkins Plugin for testing using the Selenium Webdriver and the Python programming language.
Setting Up Jenkins
- Ensure Java is installed on your machine. If you do not have Java installed, we recommend following the documentation: https://www.java.com/en/download/help/download_options.xml
- We will be using the Web Application Archive (WAR) file version of Jenkins. This version of Jenkins can be installed on any OS or platform that supports Java.
- After downloading the WAR file, run the following command from the terminal/command prompt:
java -jar jenkins.war
- Navigate to http://localhost:8080 and finish set up by waiting for the unlocking Jenkins and choosing a customization option.
Congratulations! You have successfully setup Jenkins. Now you are ready to install our Jenkins plugin.
Installing Our Jenkins Plugin
via the interface
- From the Manage Jenkins page, click Manage Plugins.
- Click the Available tab.
- Find the CrossBrowserTesting.com Plugin and select the checkbox.
- then click either Install without restart or Download now and install after restart.
by hand(not recommended)
- Download CrossBrowserTesting.hpi.
- Save the downloaded .hpi file into your `$JENKINS_HOME/plugins` directory.
- Restart Jenkins.
For more detailed information about our Jenkins Plugin, please visit https://help.crossbrowsertesting.com/integrations/tutorials/installing-jenkins/
Running A Test
- Start a new project by clicking New Item -> Freestyle Project and giving your project a name.
- From the Build Environment tab, select Crossbrowstesting.com
- Since this example demonstrates a Selenium test, Add a Selenium Test should be selected and configured.
- Select a browser and click Enable Test Results Pages
- Add CBT Credentials
- Select a browser and click Enable Test Results Pages
- From the Build tab, select Execute shell (Mac) or Execute Windows batch command (Windows) from the Add build step drop down and add the following command:
python /PATH_TO_FILE/jenkins_python.py
- In a directory of your choice, create file jenkins_python.py with the following content:
import unittest from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import requests
import os class SeleniumCBT(unittest.TestCase): def setUp(self): self.username = os.environ['CBT_USERNAME'] self.authkey = os.environ['CBT_APIKEY'] self.api_session = requests.Session() self.api_session.auth = (self.username,self.authkey) self.test_result = None caps = {} caps['name'] = os.environ['CBT_BUILD_NAME'] caps['build'] = os.environ['CBT_BUILD_NUMBER'] caps['browser_api_name'] = os.environ['CBT_BROWSER'] caps['os_api_name'] = os.environ['CBT_OPERATING_SYSTEM'] caps['screen_resolution'] = os.environ['CBT_RESOLUTION'] caps['record_video'] = 'true' try: self.driver = webdriver.Remote( desired_capabilities=caps, command_executor="http://%s:%s@hub.crossbrowsertesting.com:80/wd/hub"%(self.username, self.authkey)) except Exception as e: raise e def test_CBT(self): try: self.driver.implicitly_wait(10) self.driver.get('https://google.com') self.assertEqual(self.driver.title, 'Google') self.test_result = 'pass' except AssertionError as e: # log the error message, and set the score self.api_session.put('https://crossbrowsertesting.com/api/v3/selenium/' + self.driver.session_id + '/snapshots/' + snapshot_hash, data={'description':"AssertionError: " + str(e)}) self.test_result = 'fail' raise self.driver.quit() # Here we make the api call to set the test's score # Pass if it passes, fail if an assertion fails, unset if the test didn't finish if self.test_result is not None: self.api_session.put('https://crossbrowsertesting.com/api/v3/selenium/' + self.driver.session_id, data={'action':'set_score', 'score':self.test_result}) if __name__ == '__main__': unittest.main() - Save your configurations and run your test by selecting Build Now. Results can be viewed by clicking the build number.
Using the Pipeline
- Start a new project by clicking New Item -> Pipeline and giving your project a name.
- From the Pipeline tab, add the following pipeline script:
pipeline{ agent any stages{ stage('build'){ steps{ cbt(credentialsId: 'YOUR_CREDENTIALS_ID', useLocalTunnel: true, tunnelName: "tunnel", localTunnelPath: "", useTestResults: true) { cbtSeleniumTest(browser: 'FF64', operatingSystem: 'Mac10.14', resolution: '1366x768') { sh 'python /PATH_TO_FILE/jenkins_python.py' } } } } }
Your credentials ID can be found in the ID section on the Credentials page by selecting Credentials from the Jenkins dropdown menu.
- Save your configuration and run your test by selecting Build Now.
Using a Local Connection
If you would like to test behind your firewall or access non-public sites, you can use our local connection tool directly through our Jenkins plugin. Simply check Use Local Tunnel checkbox. (The CBT NodeJS Tunnel must be installed globally.)
Conclusions
By following the steps outlined in this guide, you are now able to seamlessly integrate Jenkins and CrossBrowserTesting. If you have any questions or concerns, please feel free to reach out to our support team.