Get your setup working faster. Join our Discord for optimisation tips from elite testers.Join our Discord
Pass Custom Variables in Automate
How do I pass custom variables specific to different browsers in the yml file?
You can pass custom variables specific to different browsers within the browserstack.yml file. At runtime you can identify these current browser instances and the corresponding custom variables that you declared. The getCurrentPlatform function returns the browser data, and you may retrieve the custom variables related to that browser and manipulate them as needed.
This can be useful when testing login functionality and passing different credentials to 3 different browsers.
The following code snippets show how to get the current platform details using the getCurrentPlatform method. This method also returns an object for the customVariables object.
packagecom.browserstack;importorg.junit.Test;importjava.io.IOException;importjava.net.URISyntaxException;importcom.browserstack.BrowserStackSdk;importstaticorg.junit.Assert.assertTrue;publicclassBStackLocalTestextendsBrowserStackJUnitTest{@Testpublicvoidtest(){System.out.println(BrowserStackSdk.getCurrentPlatform());driver.get("http://bs-local.com:45454/");assertTrue("Local content not validated!",driver.getTitle().contains("BrowserStack Local"));}}
varassert=require('assert');const{Builder,Capabilities}=require("selenium-webdriver");const{BrowserStackSdk}=require("browserstack-node-sdk");varbuildDriver=function(){returnnewBuilder().usingServer('http://localhost:4444/wd/hub').withCapabilities(Capabilities.chrome()).build();};describe('BStack Local Testing',asyncfunction(){this.timeout(0);vardriver;before(function(){driver=buildDriver();});it('check tunnel is working',asyncfunction(){console.log(BrowserStackSdk.getCurrentPlatform());awaitdriver.get('http://bs-local.com:45454/');lettitle=awaitdriver.getTitle();assert(title.match(/BrowserStack Local/i)!=null);});after(asyncfunction(){awaitdriver.quit();});});
importjsonfromseleniumimportwebdriverfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasECfromselenium.webdriver.common.byimportByfromselenium.common.exceptionsimportNoSuchElementExceptionfromselenium.webdriver.chrome.optionsimportOptionsasChromeOptionsfrombrowserstack_sdkimportBrowserStackSdk# The webdriver management will be handled by the browserstack-sdk
# so this will be overridden and tests will run browserstack -
# without any changes to the test files!
options=ChromeOptions()options.set_capability("sessionName","BStack Sample Test")driver=webdriver.Chrome(options=options)BrowserStackSdk.get_current_platform()try:driver.get("https://bstackdemo.com/")WebDriverWait(driver,10).until(EC.title_contains("StackDemo"))# Get text of an product - iPhone 12
item_on_page=(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="1"]/p'))).text)# Click the 'Add to cart' button if it is visible
WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="1"]/div[4]'))).click()# Check if the Cart pane is visible
WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.CLASS_NAME,"float-cart__content")))# Get text of product in cart
item_in_cart=(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="__next"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]',))).text)# Verify whether the product (iPhone 12) is added to cart
ifitem_on_page==item_in_cart:# Set the status of test as 'passed' if item is added to cart
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "iPhone 12 has been successfully added to the cart!"}}')else:# Set the status of test as 'failed' if item is not added to cart
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "iPhone 12 not added to the cart!"}}')exceptNoSuchElementExceptionaserr:message="Exception: "+str(err.__class__)+str(err.msg)driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": '+json.dumps(message)+"}}")exceptExceptionaserr:message="Exception: "+str(err.__class__)+str(err.msg)driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": '+json.dumps(message)+"}}")finally:# Stop the driver
driver.quit()
Did this page help you?
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