Pass Custom Variables in App 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.
Here is a sample .yml file with custom variables:
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.
package com.browserstack;
import org.junit.Test;
import java.io.IOException;
import java.net.URISyntaxException;
import com.browserstack.BrowserStackSdk;
import static org.junit.Assert.assertTrue;
public class BStackLocalTest extends BrowserStackJUnitTest {
@Test
public void test() {
System.out.println(BrowserStackSdk.getCurrentPlatform());
driver.get("http://bs-local.com:45454/");
assertTrue("Local content not validated!", driver.getTitle().contains("BrowserStack Local"));
}
}
var assert = require('assert');
const {
Builder,
Capabilities
} = require("selenium-webdriver");
const {
BrowserStackSdk
} = require("browserstack-node-sdk");
var buildDriver = function() {
return new Builder().
usingServer('http://localhost:4444/wd/hub').
withCapabilities(Capabilities.chrome()).
build();
};
describe('BStack Local Testing', async function() {
this.timeout(0);
var driver;
before(function() {
driver = buildDriver();
});
it('check tunnel is working', async function() {
console.log(BrowserStackSdk.getCurrentPlatform());
await driver.get('http://bs-local.com:45454/');
let title = await driver.getTitle();
assert(title.match(/BrowserStack Local/i) != null);
});
after(async function() {
await driver.quit();
});
});
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using BrowserStack;
namespace BrowserStack {
[TestFixture]
public class BrowserStackNUnitTest {
protected RemoteWebDriver driver;
public BrowserStackNUnitTest() {}
[SetUp]
public void Init() {
Dictionary < string, object > currentPlatform = BrowserStackSdk.GetCurrentPlatform();
foreach(var (Key, Value) in currentPlatform) {
Console.WriteLine($"Key: {Key}, Value: {Value}");
}
DriverOptions capability = new OpenQA.Selenium.Chrome.ChromeOptions();
capability.BrowserVersion = "latest";
capability.AddAdditionalOption("bstack:options", capability);
driver = new RemoteWebDriver(
new Uri("http://localhost:444/wd/hub/"),
capability
);
}
[TearDown]
public void Cleanup() {
driver.Quit();
}
}
}
import json
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.options import Options as ChromeOptions
from browserstack_sdk import BrowserStackSdk
# 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
if item_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!"}}'
)
except NoSuchElementException as err:
message = "Exception: " + str(err.__class__) + str(err.msg)
driver.execute_script(
'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": '
+ json.dumps(message)
+ "}}"
)
except Exception as err:
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()
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
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!