Skip to content

Commit

Permalink
Fixed BrowserStack execution for mobile-web (added sample test to run…
Browse files Browse the repository at this point in the history
… chrome browser test in real android device in BS)
  • Loading branch information
anandbagmar committed Nov 2, 2023
1 parent 93a254b commit 1e90fa6
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"web": {
"browserName":"chrome",
"browserstackOptions": {
"deviceName": "Samsung Galaxy S20",
"osVersion": "13.0",
"projectName": "ab",
"buildName": "fff",
"sessionName": "test",
"appiumVersion": "2.0.1",
"local": "false",
"networkLogs": "true",
"consoleLogs": "info",
"geoLocation": "IN",
"timezone": "Kolkata"
}
},
"serverConfig" : {
"server": {
"use-plugins": ["device-farm"],
"plugin": {
"device-farm": {
"platform": "web",
"skipChromeDownload": true,
"cloud": {
"cloudName": "browserstack",
"url": "http://hub-cloud.browserstack.com",
"apiUrl": "https://api-cloud.browserstack.com/app-automate/",
"devices": [
],
"web": {}
}
}
}
}
},
"appiumServerPath": "./node_modules/appium/build/lib/main.js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
RUNNER=distribute
FRAMEWORK=cucumber
RUNNER_LEVEL=methods
CAPS=./caps/googlesearch/googlesearch_browserstack_android_emulator_chrome.json
APP_NAME=Google_Chrome
APP_PACKAGE_NAME=com.chrome.conferences
APPLITOOLS_CONFIGURATION=./configs/applitools_config.json
BASE_URL_FOR_WEB=TESWIZ_BASE_URL
BROWSER=chrome
CLOUD_USERNAME=username
CLOUD_KEY=key
CLOUD_UPLOAD_APP=false
CLOUD_NAME=browserstack
CLOUD_USE_PROXY=false
CLOUD_USE_LOCAL_TESTING=false
ENVIRONMENT_CONFIG_FILE=./src/test/resources/environments.json
IS_VISUAL=true
FAIL_TEST_ON_VISUAL_DIFFERENCE=true
LOG_DIR=target
LOG_PROPERTIES_FILE=./src/test/resources/log4j.properties
PARALLEL=1
PLATFORM=android
PROXY_KEY=HTTP_PROXY
REPORT_PORTAL_FILE=src/test/resources/reportportal.properties
RP_DESCRIPTION=custom description
RUN_IN_CI=true
TARGET_ENVIRONMENT=prod
LAUNCH_NAME_SUFFIX= on 'prod' Environment
TEST_DATA_FILE=./src/test/resources/testData.json
BROWSER_CONFIG_FILE=./configs/browser_config.json
23 changes: 18 additions & 5 deletions src/main/java/com/znsio/teswiz/runner/BrowserStackSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static com.znsio.teswiz.runner.Runner.USER_NAME;
import static com.znsio.teswiz.runner.Runner.getHostName;
import static org.openqa.selenium.remote.CapabilityType.BROWSER_NAME;

class BrowserStackSetup {
private static final Logger LOGGER = Logger.getLogger(BrowserStackSetup.class.getName());
Expand All @@ -35,14 +36,11 @@ static void updateBrowserStackCapabilities(String deviceLabURL) {
String authenticationKey = Setup.getFromConfigs(Setup.CLOUD_KEY);
String platformName = Setup.getPlatform().name();
String capabilityFile = Setup.getFromConfigs(Setup.CAPS);
String appPath = new File(Setup.getFromConfigs(Setup.APP_PATH)).getAbsolutePath();

Map<String, Map> loadedCapabilityFile = JsonFile.loadJsonFile(capabilityFile);
Map loadedPlatformCapability = loadedCapabilityFile.get(platformName);
String appIdFromBrowserStack = getAppIdFromBrowserStack(authenticationUser,
authenticationKey, appPath, deviceLabURL);
LOGGER.info(String.format("app Id retreived from browser stack is: %s", appIdFromBrowserStack));
loadedPlatformCapability.put("app", appIdFromBrowserStack);

addAppOrBrowserNameToBrowserStackCapablities(deviceLabURL, loadedPlatformCapability, authenticationUser, authenticationKey);
loadedPlatformCapability.put("browserstack.user", authenticationUser);
loadedPlatformCapability.put("browserstack.key", authenticationKey);
loadedPlatformCapability.put("browserstack.LoggedInUser", USER_NAME);
Expand All @@ -57,6 +55,20 @@ static void updateBrowserStackCapabilities(String deviceLabURL) {
loadedCapabilityFile);
}

private static void addAppOrBrowserNameToBrowserStackCapablities(String deviceLabURL, Map loadedPlatformCapability, String authenticationUser, String authenticationKey) {
Object browserName = loadedPlatformCapability.get(BROWSER_NAME);
if (null != browserName) {
LOGGER.info(String.format("app Id retreived from browser stack is: %s", browserName));
loadedPlatformCapability.put("browserstack.browserName", browserName);
} else {
String appPath = new File(Setup.getFromConfigs(Setup.APP_PATH)).getAbsolutePath();
String appIdFromBrowserStack = getAppIdFromBrowserStack(authenticationUser,
authenticationKey, appPath, deviceLabURL);
LOGGER.info(String.format("app Id retreived from browser stack is: %s", appIdFromBrowserStack));
loadedPlatformCapability.put("app", appIdFromBrowserStack);
}
}

private static void setupLocalTesting(String authenticationKey, Map loadedPlatformCapability) {
if(Setup.getBooleanValueFromConfigs(Setup.CLOUD_USE_LOCAL_TESTING)) {
String browserStackLocalIdentifier = Randomizer.randomize(10);
Expand Down Expand Up @@ -92,6 +104,7 @@ static MutableCapabilities updateBrowserStackCapabilities(MutableCapabilities ca
browserstackOptions.put("buildName",
Setup.getFromConfigs(Setup.LAUNCH_NAME) + "-" + subsetOfLogDir);

browserstackOptions.put("sessionName", Runner.getTestExecutionContext(Thread.currentThread().getId()).getTestName());
if(Setup.getBooleanValueFromConfigs(Setup.CLOUD_USE_LOCAL_TESTING)) {
LOGGER.info(String.format(
"CLOUD_USE_LOCAL_TESTING=true. Setting up BrowserStackLocal testing using " + "identified: '%s'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.znsio.teswiz.runner.Runner;
import com.znsio.teswiz.runner.Visual;
import com.znsio.teswiz.screen.android.googlesearch.GoogleSearchLandingScreenAndroid;
import com.znsio.teswiz.screen.web.googlesearch.GoogleSearchLandingScreenWeb;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.log4j.Logger;

Expand All @@ -22,6 +23,8 @@ public static GoogleSearchLandingScreen get() {
switch (platform) {
case android:
return new GoogleSearchLandingScreenAndroid(driver, visually);
case web:
return new GoogleSearchLandingScreenWeb(driver, visually);
default:
throw new NotImplementedException(SCREEN_NAME + " is not implemented in " + Runner.getPlatform());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.znsio.teswiz.runner.Runner;
import com.znsio.teswiz.runner.Visual;
import com.znsio.teswiz.screen.android.googlesearch.GoogleSearchResultsScreenAndroid;
import com.znsio.teswiz.screen.web.googlesearch.GoogleSearchResultsScreenWeb;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.log4j.Logger;

Expand All @@ -24,6 +25,8 @@ public static GoogleSearchResultsScreen get() {
switch (platform) {
case android:
return new GoogleSearchResultsScreenAndroid(driver, visually);
case web:
return new GoogleSearchResultsScreenWeb(driver, visually);
default:
throw new NotImplementedException(
SCREEN_NAME + " is not implemented in " + Runner.getPlatform());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.znsio.teswiz.screen.web.googlesearch;

import com.context.TestExecutionContext;
import com.znsio.teswiz.runner.Driver;
import com.znsio.teswiz.runner.Runner;
import com.znsio.teswiz.runner.Visual;
import com.znsio.teswiz.screen.googlesearch.GoogleSearchLandingScreen;
import com.znsio.teswiz.screen.googlesearch.GoogleSearchResultsScreen;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;

public class GoogleSearchLandingScreenWeb extends GoogleSearchLandingScreen {
private static final String URL = "https://google.com";
private static final String SCREEN_NAME = GoogleSearchLandingScreenWeb.class.getSimpleName();
private static final Logger LOGGER = Logger.getLogger(SCREEN_NAME);
private static final By SEARCH_INPUT = By.name("q");

private final Driver driver;
private final Visual visually;
private final TestExecutionContext context;

public GoogleSearchLandingScreenWeb(Driver driver, Visual visually) {
this.driver = driver;
this.visually = visually;
long threadId = Thread.currentThread().getId();
context = Runner.getTestExecutionContext(threadId);
driver.getInnerDriver().get(URL);
driver.waitTillElementIsPresent(SEARCH_INPUT);
}

@Override
public GoogleSearchResultsScreen searchFor(String searchText) {
visually.checkWindow(SCREEN_NAME, "Google");
driver.findElement(SEARCH_INPUT).sendKeys(searchText, Keys.ENTER);
return GoogleSearchResultsScreen.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.znsio.teswiz.screen.web.googlesearch;

import com.context.TestExecutionContext;
import com.znsio.teswiz.runner.Driver;
import com.znsio.teswiz.runner.Runner;
import com.znsio.teswiz.runner.Visual;
import com.znsio.teswiz.screen.googlesearch.GoogleSearchResultsScreen;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;

import java.util.List;
import java.util.stream.Collectors;

public class GoogleSearchResultsScreenWeb extends GoogleSearchResultsScreen {
private static final String SCREEN_NAME = GoogleSearchResultsScreenWeb.class.getSimpleName();
private static final Logger LOGGER = Logger.getLogger(SCREEN_NAME);
private final Driver driver;
private final Visual visually;
private final TestExecutionContext context;

private final By searchResultsHeadings = By.cssSelector("a div[role='heading']");

public GoogleSearchResultsScreenWeb(Driver driver, Visual visually) {
this.driver = driver;
this.visually = visually;
long threadId = Thread.currentThread().getId();
context = Runner.getTestExecutionContext(threadId);
}

@Override
public List<String> getSearchResults() {
visually.checkWindow(SCREEN_NAME, "india - Google Search");
return driver.findElements(searchResultsHeadings).stream().map(webElement -> webElement.getText()).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@ Feature: Scenarios for "google-search"
# CONFIG=./configs/googlesearch/googlesearch_android_chrome_config.properties PLATFORM=android TAG="@google-search and @android-chrome" ./gradlew run
@android-chrome @android
Scenario: Google search results
Scenario: Google search results in local emulator using appium
Given I search for "india" in "chrome-android"

# CLOUD_USERNAME=$BROWSERSTACK_CLOUD_USERNAME CLOUD_KEY=$BROWSERSTACK_CLOUD_KEY CONFIG=./configs/googlesearch/googlesearch_browserstack_android_chrome_config.properties PLATFORM=web TAG="@google-search and @android-chrome" ./gradlew run
@browserstack @android-chrome @web
Scenario: Google search results in android-browserstack using Selenium
Given I search for "india" in "chrome-web"
4 changes: 3 additions & 1 deletion src/test/resources/environments.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"BING_BASE_URL": "http://bing.com/images",
"JIOMEET_BASE_URL": "https://jiomeetpro.jio.com/home",
"INDIGO_BASE_URL": "https://www.goindigo.in/",
"DINEOUT_BASE_URL": "https://www.dineout.co.in/"
"DINEOUT_BASE_URL": "https://www.dineout.co.in/",
"TESWIZ_BASE_URL": "https://github.com/znsio/teswiz",
"CHROME-WEB_BASE_URL": "https://github.com/znsio/teswiz"
},
"dev": {
"CONFENGINE_BASE_URL": "https://www.dev.confengine.com/"
Expand Down

0 comments on commit 1e90fa6

Please sign in to comment.