From 4186cbb333cc91698592338fdd89a4d6f1bdb97c Mon Sep 17 00:00:00 2001 From: Princeton Baretto <43331485+princebaretto99@users.noreply.github.com> Date: Mon, 24 Jan 2022 20:46:56 +0530 Subject: [PATCH] Webdriver Framework Integration (#5) Co-authored-by: princebaretto99 Co-authored-by: s7arlord --- .gitignore | 9 ++ Jenkinsfile | 43 ++++--- README.md | 59 +--------- resources/conf/caps/bstack-config.yaml | 57 ++++++++++ resources/conf/caps/bstack-local-config.yaml | 57 ++++++++++ resources/conf/caps/test_caps.json | 112 ------------------- resources/conf/runners/Library.py | 59 +++------- resources/conf/runners/keywords.robot | 57 ++-------- 8 files changed, 180 insertions(+), 273 deletions(-) create mode 100644 resources/conf/caps/bstack-config.yaml create mode 100644 resources/conf/caps/bstack-local-config.yaml delete mode 100644 resources/conf/caps/test_caps.json diff --git a/.gitignore b/.gitignore index 04a41a2..95eb04a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,10 @@ **.pyc + +pabot_results +webdriverFrameworkCorePython +*.png + +log.html +local.log +output.xml +report.html diff --git a/Jenkinsfile b/Jenkinsfile index 4970a74..fbf3706 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -22,7 +22,10 @@ bstack-local''', stage('Pull from Github') { dir('test') { - git branch: 'develop', changelog: false, poll: false, url: 'https://github.com/browserstack/browserstack-examples-robot.git' + git branch: 'review', changelog: false, poll: false, url: 'https://github.com/browserstack/browserstack-examples-robot.git' + } + dir('pipPackage') { + git branch: 'develop_webdriver-framework_python', changelog: false, poll: false, url: 'https://github.com/browserstack/webdriver-framework.git' } } @@ -43,31 +46,46 @@ bstack-local''', stage('Install Dependencies'){ sh ''' - pip3 install robotframework --user - pip3 install robotframework-seleniumlibrary --user - pip3 install browserstack --user + pip3 install robotframework + pip3 install robotframework-seleniumlibrary + pip3 install browserstack pip3 install webdriver_manager - pip3 install robotframework-pabot --user - - + pip3 install robotframework-pabot ''' } stage('Run Test(s)') { browserstack(credentialsId: "${params.BROWSERSTACK_USERNAME}") { + + sh ''' + cd pipPackage/selenium + + pip3 install wheel + pip3 install . + ''' + if ( "${params.TEST_TYPE}".contains('parallel') ) { sh ''' cd test - - pabot --testlevelsplit --variable testType:bstack-single . + export CONFIG_FILE_PATH=`pwd`/'resources/conf/caps/bstack-config.yaml' + python3 -m robot --variable testType:bstack-single . + + //pabot --testlevelsplit --verbose --variable testType:bstack-single --suite product . + + ''' + else if ( "${params.TEST_TYPE}".contains('local') ) { + sh ''' + cd test + export CONFIG_FILE_PATH=`pwd`/'resources/conf/caps/bstack-local-config.yaml' + python3 -m robot --variable testType:bstack-single . ''' - - } else{ sh ''' cd test - robot --variable testType:${TEST_TYPE} . + export CONFIG_FILE_PATH=`pwd`/'resources/conf/caps/bstack-config.yaml' + python3 -m robot --variable testType:bstack-single . + ''' } @@ -83,4 +101,3 @@ bstack-local''', } } } - diff --git a/README.md b/README.md index 2747982..a09a136 100644 --- a/README.md +++ b/README.md @@ -226,7 +226,7 @@ Note: ### Run a specific test on BrowserStack -In this section, we will run a single test on Chrome browser on Browserstack. To change test capabilities for this configuration, please refer to the `single` object in `test_caps.json` file. +In this section, we will run a single test on Chrome browser on Browserstack. To change test capabilities for this configuration, please refer to the `bstack-config.json` file. - How to run the test? @@ -250,7 +250,7 @@ In this section, we will run a single test on Chrome browser on Browserstack. To ### Run the entire test suite in parallel on a single BrowserStack browser -In this section, we will run the tests in parallel on a single browser on Browserstack. Refer to `single` object in `test_caps.json` file to change test capabilities for this configuration. +In this section, we will run the tests in parallel on a single browser on Browserstack. Refer to `bstack-config.json` file to change test capabilities for this configuration. - How to run the test? @@ -261,7 +261,6 @@ In this section, we will run the tests in parallel on a single browser on Browse ``` - - Output This run profile executes the entire test suite in parallel on a single BrowserStack browser. Please refer to your [BrowserStack dashboard](https://automate.browserstack.com/) for test results. @@ -269,59 +268,11 @@ In this section, we will run the tests in parallel on a single browser on Browse - Note: By default, this execution would run maximum 5 test threads in parallel on BrowserStack. Refer to the section ["Configuring the maximum parallel test threads for this repository"](#Configuring-the-maximum-parallel-test-threads-for-this-repository) for updating the parallel thread count based on your requirements. -### [Web application hosted on internal environment] Running your tests on BrowserStack using BrowserStackLocal - -#### Prerequisites - -- Clone the [BrowserStack demo application](https://github.com/browserstack/browserstack-demo-app) repository. - ```sh - git clone https://github.com/browserstack/browserstack-demo-app - ``` -- Please follow the README.md on the BrowserStack demo application repository to install and start the dev server on localhost. -- In this section, we will run a single test case to test the BrowserStack Demo app hosted on your local machine i.e. localhost. Refer to the `single_local` object in `caps.json` file to change test capabilities for this configuration. -- Note: You may need to provide additional BrowserStackLocal arguments to successfully connect your localhost environment with BrowserStack infrastructure. (e.g if you are behind firewalls, proxy or VPN). -- Further details for successfully creating a BrowserStackLocal connection can be found here: - - - [Local Testing with Automate](https://www.browserstack.com/local-testing/automate) - - [BrowserStackLocal Python GitHub](https://github.com/browserstack/browserstack-local-python) - - -### [Web application hosted on internal environment] Run a specific test on BrowserStack using BrowserStackLocal - -- How to run the test? - - To run a specific test scenario, use the following command with the additional test-name argument: - - ```sh - robot --variable testType:bstack-local --test "" . - - ``` - - where, the argument 'test-name' can be any scenario name configured in this repository. - - E.g. "Check Login with locked_user", "E2E Test", "Apply 'Lowest to Highest' Order By Filter" or any of the other test scenario names, as outlined in [About the tests in this repository](#About-the-tests-in-this-repository) section. - - -- Output - - This run profile executes a single test on an internally hosted web application on a single browser on BrowserStack. Please refer to your BrowserStack dashboard(https://automate.browserstack.com/) for test results. - - -### [Web application hosted on internal environment] Run the entire test suite in parallel on a single BrowserStack browser using BrowserStackLocal +### Run a tests on BrowserStack which need Local Environment access -In this section, we will run the test cases to test the internally hosted website in parallel on a single browser on Browserstack. Refer to the `single_local` object in `test_caps.json` file to change test capabilities for this configuration. - -- How to run the test? - - To run the entire test suite in parallel on a single BrowserStack browser using BrowserStackLocal, use the following command: - ```sh - pabot --testlevelsplit --variable testType:bstack-local --suite . - ``` - - -- Output + The only change you need to do here, is to add the respective options in the `bstack-config.json` file. + The webdriver framework handles the starting and termination of BrowserStack Local Instance. - This run profile executes the entire test suite on an internally hosted web application on a single browser on BrowserStack. Please refer to your [BrowserStack dashboard](https://automate.browserstack.com/) for test results. ## Generating Allure Reports diff --git a/resources/conf/caps/bstack-config.yaml b/resources/conf/caps/bstack-config.yaml new file mode 100644 index 0000000..475c3bb --- /dev/null +++ b/resources/conf/caps/bstack-config.yaml @@ -0,0 +1,57 @@ +testEndpoint: https://bstackdemo.com + +namedTestUrls: + url_one: https://www.google.com + url_two: https://www.yahoo.com + +driverType: cloudDriver + +onPremDriver: + platforms: + - name: + browser: Chrome + driverPath: /PATH/TO/CHROME/DRIVER + - name: + browser: Firefox + driverPath: /PATH/TO/FIREFOX/DRIVER + +cloudDriver: + hubUrl: https://hub-cloud.browserstack.com/wd/hub + user: BROWSERSTACK_USERNAME + accessKey: BROWSERSTACK_ACCESSKEY + localTunnel: + enabled: false + local_options: + forcelocal: true + force: true + verbose: true + common_capabilities: + project: BrowserStack Demo Repository + buildPrefix: browserstack-examples-python-robot + capabilities: + browserstack.debug: true + browserstack.networkLogs: true + browserstack.console: debug + platforms: + - name: Win10_Chrome_Latest-1 + os: Windows + os_version: '10' + browser: Chrome + browser_version: latest-1 + capabilities: + browserstack.selenium_version: 3.141.59 + - name: OSX_BigSur_Chrome_Latest + os: OS X + os_version: Big Sur + browser: Chrome + browser_version: latest + capabilities: + browserstack.selenium_version: 3.141.59 + - name: Win10_IE11 + os: Windows + os_version: '10' + browser: IE + browser_version: '11.0' + capabilities: + browserstack.ie.arch: x32 + browserstack.selenium_version: 3.141.59 \ No newline at end of file diff --git a/resources/conf/caps/bstack-local-config.yaml b/resources/conf/caps/bstack-local-config.yaml new file mode 100644 index 0000000..47c45b9 --- /dev/null +++ b/resources/conf/caps/bstack-local-config.yaml @@ -0,0 +1,57 @@ +testEndpoint: https://bstackdemo.com + +namedTestUrls: + url_one: https://www.google.com + url_two: https://www.yahoo.com + +driverType: cloudDriver + +onPremDriver: + platforms: + - name: + browser: Chrome + driverPath: /PATH/TO/CHROME/DRIVER + - name: + browser: Firefox + driverPath: /PATH/TO/FIREFOX/DRIVER + +cloudDriver: + hubUrl: https://hub-cloud.browserstack.com/wd/hub + user: BROWSERSTACK_USERNAME + accessKey: BROWSERSTACK_ACCESSKEY + localTunnel: + enabled: true + local_options: + forcelocal: true + force: true + verbose: true + common_capabilities: + project: BrowserStack Demo Repository + buildPrefix: browserstack-examples-python-robot + capabilities: + browserstack.debug: true + browserstack.networkLogs: true + browserstack.console: debug + platforms: + - name: Win10_Chrome_Latest-1 + os: Windows + os_version: '10' + browser: Chrome + browser_version: latest-1 + capabilities: + browserstack.selenium_version: 3.141.59 + - name: OSX_BigSur_Chrome_Latest + os: OS X + os_version: Big Sur + browser: Chrome + browser_version: latest + capabilities: + browserstack.selenium_version: 3.141.59 + - name: Win10_IE11 + os: Windows + os_version: '10' + browser: IE + browser_version: '11.0' + capabilities: + browserstack.ie.arch: x32 + browserstack.selenium_version: 3.141.59 \ No newline at end of file diff --git a/resources/conf/caps/test_caps.json b/resources/conf/caps/test_caps.json deleted file mode 100644 index 73171b5..0000000 --- a/resources/conf/caps/test_caps.json +++ /dev/null @@ -1,112 +0,0 @@ -{ - "user": "BROWSERSTACK_USERNAME", - "key": "BROWSERSTACK_ACCESS_KEY", - "application_endpoint": "https://bstackdemo.com", - "tests": { - "parallel": { - "common_caps": { - "project": "browserstack-examples-robot", - "build": "browserstack-examples-robot", - "name": "Parallel Test", - "browserstack.debug": "true", - "browserstack.networkLogs": "true", - "browserstack.console": "verbose" - }, - "env_caps": [ - { - "os": "Windows", - "os_version": "10", - "browser": "Internet Explorer", - "browser_version": "11.0" - }, - { - "os": "Windows", - "os_version": "10", - "browser": "Chrome", - "browser_version": "latest-1" - }, - { - "os": "OS X", - "os_version": "Big Sur", - "browser": "Chrome", - "browser_version": "latest" - }, - { - "os": "OS X", - "os_version": "Catalina", - "browser": "Safari", - "browser_version": "13.0" - }, - { - "os": "Android", - "os_version": "11.0", - "device": "Samsung Galaxy S20", - "realMobile": true - }, - { - "os": "iPhone", - "os_version": "13.0", - "device": "iPhone XS", - "realMobile": true - } - ] - }, - "single": { - "common_caps": { - "project": "browserstack-examples-robot", - "build": "browserstack-examples-robot", - "name": "Single Test", - "chromeOptions": { - "w3c" : "false" - }, - "browserstack.maskCommands": "setValues" - }, - "env_caps": [ - { - "os": "Windows", - "os_version": "10", - "browser": "Chrome" - } - ] - }, - "local": { - "application_env": "dev", - "application_endpoint": "http://localhost:3000", - "common_caps": { - "project": "browserstack-examples-robot", - "build": "browserstack-examples-robot", - "name": "Local Test", - "browserstack.debug": "true", - "browserstack.networkLogs": "true", - "browserstack.console": "verbose", - "browserstack.local": "true", - "browserstack.maskCommands": "setValues" - }, - "env_caps": [ - { - "os": "Windows", - "os_version": "10", - "browser": "Chrome" - } - ] - }, - "mobile": { - "common_caps": { - "project": "browserstack-examples-robot", - "build": "browserstack-examples-robot", - "name": "Mobile Parallel Test" - }, - "env_caps": [ - { - "device": "Google Pixel", - "realMobile": true - }, - { - "device": "iPhone 11", - "os_version": "13", - "realMobile": true - } - ] - } - } - } \ No newline at end of file diff --git a/resources/conf/runners/Library.py b/resources/conf/runners/Library.py index 3ecc880..15e4647 100644 --- a/resources/conf/runners/Library.py +++ b/resources/conf/runners/Library.py @@ -1,4 +1,3 @@ -from browserstack.local import Local import json import csv import os @@ -7,11 +6,13 @@ from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.keys import Keys -import urllib3 +import urllib3 +from webdriverFrameworkCorePython.src.main.webdriver.core import WebDriverFactory + urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) -bstack_local = 0 se2lib = BuiltIn().get_library_instance('SeleniumLibrary') +factory = WebDriverFactory.WebDriverFactoryClass.getInstance() user_data = '../../data/user.csv' product_data = '../../data/product.csv' @@ -24,54 +25,22 @@ def combine_dict(dict1, dict2): return dict_1 +def get_endpoint(): + url_to_test = factory.getTestEndpoint() + return url_to_test -def get_caps_single(caps_path, username, access_key): - - with open(caps_path) as f: - data = json.load(f) - - single_caps_common = data['tests']['single']['common_caps'] - single_caps_env = data['tests']['single']['env_caps'][0] - - single_caps_common.update(single_caps_env) - single_caps_common['browserstack.user'] = username - single_caps_common['browserstack.key'] = access_key - - return single_caps_common - - -def get_caps_local(caps_path, username, access_key): - - with open(caps_path) as f: - data = json.load(f) - - caps_common = data['tests']['local']['common_caps'] - caps_env = data['tests']['local']['env_caps'][0] - - caps_common.update(caps_env) - caps_common['browserstack.user'] = username - caps_common['browserstack.key'] = access_key - - return caps_common - - -def start_local(key): - - global bstack_local - bstack_local = Local() - - bstack_local_args = {"key": key} - - bstack_local.start(**bstack_local_args) +def get_caps_single(): - return bstack_local + allPlatforms = factory.getPlatforms() + single_caps = factory.getPlatformCaps(allPlatforms[0], "Robot Test", None) + return single_caps -def stop_local(): +def get_test_endpoint(): - global bstack_local + testendpoint = factory.getTestEndpoint() - bstack_local.stop() + return testendpoint def get_row_item_from_file(filepath, key): diff --git a/resources/conf/runners/keywords.robot b/resources/conf/runners/keywords.robot index 83ca004..0afc860 100644 --- a/resources/conf/runners/keywords.robot +++ b/resources/conf/runners/keywords.robot @@ -1,34 +1,21 @@ *** Settings *** Library Library.py -*** Variables *** -${caps_path} ${CURDIR}/../../../resources/conf/caps/test_caps.json - *** Keywords *** Start Test [Arguments] ${testType} - Run Keyword If '${testType}'=='bstack-single' Start single Test ELSE IF '${testType}'=='bstack-local' Start local Test ELSE IF '${testType}'=='docker' Start docker Test ELSE Start onprem Test + Run Keyword If '${testType}'=='bstack-single' Start single Test ELSE IF '${testType}'=='docker' Start docker Test ELSE Start onprem Test Start single Test ${remote_url}= Set Variable https://hub-cloud.browserstack.com/wd/hub - ${json2}= Get file ${caps_path} - ${json_object2}= Evaluate json.loads('''${json2}''') json - - - ${application_endpoint}= Set Variable ${json_object2['application_endpoint']} - - ${username_status} Run Keyword And Return Status Get Environment Variable BROWSERSTACK_USERNAME - ${acess_key_status} Run Keyword And Return Status Get Environment Variable BROWSERSTACK_ACCESS_KEY - - ${user}= Set Variable If """${username_status}""" == 'True' %{BROWSERSTACK_USERNAME} ${json_object2['user']} - ${access_key}= Set Variable If """${acess_key_status}""" == 'True' %{BROWSERSTACK_ACCESS_KEY} ${json_object2['key']} + ${application_endpoint}= get_test_endpoint - ${caps}= get_caps_single ${caps_path} ${user} ${access_key} + ${caps}= get_caps_single Open Browser ${application_endpoint} remote_url=${remote_url} desired_capabilities=${caps} @@ -36,38 +23,16 @@ Start local Test ${remote_url}= Set Variable https://hub-cloud.browserstack.com/wd/hub - ${json2}= Get file ${caps_path} - ${json_object2}= Evaluate json.loads('''${json2}''') json - - - ${application_endpoint}= Set Variable ${json_object2['tests']['local']['application_endpoint']} - - ${username_status} Run Keyword And Return Status Get Environment Variable BROWSERSTACK_USERNAME - ${acess_key_status} Run Keyword And Return Status Get Environment Variable BROWSERSTACK_ACCESS_KEY - - ${user}= Set Variable If """${username_status}""" == 'True' %{BROWSERSTACK_USERNAME} ${json_object2['user']} - ${access_key}= Set Variable If """${acess_key_status}""" == 'True' %{BROWSERSTACK_ACCESS_KEY} ${json_object2['key']} + ${application_endpoint}= get_test_endpoint - - ${caps}= get_caps_local ${caps_path} ${user} ${access_key} - - - ${local_instance}= start_local ${access_key} + ${caps}= get_caps_single Open Browser ${application_endpoint} remote_url=${remote_url} desired_capabilities=${caps} - -Stop Local Test - stop_local - - Start onprem Test - ${json2}= Get file ${caps_path} - ${json_object2}= Evaluate json.loads('''${json2}''') json - - ${application_endpoint}= Set Variable ${json_object2['application_endpoint']} + ${application_endpoint}= get_test_endpoint Open Browser ${application_endpoint} chrome @@ -75,11 +40,7 @@ Start onprem Test Start docker Test - ${json2}= Get file ${caps_path} - ${json_object2}= Evaluate json.loads('''${json2}''') json - - - ${application_endpoint}= Set Variable ${json_object2['application_endpoint']} + ${application_endpoint}= get_test_endpoint ${docker_remote_url}= http://localhost:4444/wd/hub @@ -89,9 +50,7 @@ Start docker Test Stop Test [Arguments] ${testType} - # Run Keyword If '${testType}'=='bstack-single' Mark Test Status ${TEST_STATUS} ELSE IF '${testType}'=='bstack-local' Mark Test Status ${TEST_STATUS} Only Run On Browserstack Mark Test Status ${TEST_STATUS} - Run Keyword If '${testType}'=='bstack-local' stop_local Close Browser Get Password From CSV @@ -115,7 +74,7 @@ Rename Session Only Run On Browserstack [Arguments] ${keyword} ${param} - Run Keyword If '${testType}'=='bstack-single' ${keyword} ${param} ELSE IF '${testType}'=='bstack-local' ${keyword} ${param} + Run Keyword If '${testType}'=='bstack-single' ${keyword} ${param} Get Product Prices From CSV ${prices}= get_product_prices