From 1e90fa665a36a314083f9274925c31b8bf8c0834 Mon Sep 17 00:00:00 2001 From: Anand Bagmar Date: Thu, 2 Nov 2023 13:55:09 +0530 Subject: [PATCH] Fixed BrowserStack execution for mobile-web (added sample test to run chrome browser test in real android device in BS) --- ..._browserstack_android_emulator_chrome.json | 38 +++++++++++++++++++ ...wserstack_android_chrome_config.properties | 30 +++++++++++++++ .../teswiz/runner/BrowserStackSetup.java | 23 ++++++++--- .../GoogleSearchLandingScreen.java | 3 ++ .../GoogleSearchResultsScreen.java | 3 ++ .../GoogleSearchLandingScreenWeb.java | 38 +++++++++++++++++++ .../GoogleSearchResultsScreenWeb.java | 35 +++++++++++++++++ .../teswiz/features/googlesearch.feature | 7 +++- src/test/resources/environments.json | 4 +- 9 files changed, 174 insertions(+), 7 deletions(-) create mode 100644 caps/googlesearch/googlesearch_browserstack_android_emulator_chrome.json create mode 100644 configs/googlesearch/googlesearch_browserstack_android_chrome_config.properties create mode 100644 src/test/java/com/znsio/teswiz/screen/web/googlesearch/GoogleSearchLandingScreenWeb.java create mode 100644 src/test/java/com/znsio/teswiz/screen/web/googlesearch/GoogleSearchResultsScreenWeb.java diff --git a/caps/googlesearch/googlesearch_browserstack_android_emulator_chrome.json b/caps/googlesearch/googlesearch_browserstack_android_emulator_chrome.json new file mode 100644 index 000000000..f223ddd03 --- /dev/null +++ b/caps/googlesearch/googlesearch_browserstack_android_emulator_chrome.json @@ -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" +} \ No newline at end of file diff --git a/configs/googlesearch/googlesearch_browserstack_android_chrome_config.properties b/configs/googlesearch/googlesearch_browserstack_android_chrome_config.properties new file mode 100644 index 000000000..c0e499718 --- /dev/null +++ b/configs/googlesearch/googlesearch_browserstack_android_chrome_config.properties @@ -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 \ No newline at end of file diff --git a/src/main/java/com/znsio/teswiz/runner/BrowserStackSetup.java b/src/main/java/com/znsio/teswiz/runner/BrowserStackSetup.java index d442f3cd1..58bec5312 100644 --- a/src/main/java/com/znsio/teswiz/runner/BrowserStackSetup.java +++ b/src/main/java/com/znsio/teswiz/runner/BrowserStackSetup.java @@ -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()); @@ -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 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); @@ -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); @@ -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'", diff --git a/src/test/java/com/znsio/teswiz/screen/googlesearch/GoogleSearchLandingScreen.java b/src/test/java/com/znsio/teswiz/screen/googlesearch/GoogleSearchLandingScreen.java index 1ae1b976d..5ca70f820 100644 --- a/src/test/java/com/znsio/teswiz/screen/googlesearch/GoogleSearchLandingScreen.java +++ b/src/test/java/com/znsio/teswiz/screen/googlesearch/GoogleSearchLandingScreen.java @@ -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; @@ -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()); } diff --git a/src/test/java/com/znsio/teswiz/screen/googlesearch/GoogleSearchResultsScreen.java b/src/test/java/com/znsio/teswiz/screen/googlesearch/GoogleSearchResultsScreen.java index 85bb30b41..70623257d 100644 --- a/src/test/java/com/znsio/teswiz/screen/googlesearch/GoogleSearchResultsScreen.java +++ b/src/test/java/com/znsio/teswiz/screen/googlesearch/GoogleSearchResultsScreen.java @@ -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; @@ -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()); diff --git a/src/test/java/com/znsio/teswiz/screen/web/googlesearch/GoogleSearchLandingScreenWeb.java b/src/test/java/com/znsio/teswiz/screen/web/googlesearch/GoogleSearchLandingScreenWeb.java new file mode 100644 index 000000000..19c090e9d --- /dev/null +++ b/src/test/java/com/znsio/teswiz/screen/web/googlesearch/GoogleSearchLandingScreenWeb.java @@ -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(); + } +} diff --git a/src/test/java/com/znsio/teswiz/screen/web/googlesearch/GoogleSearchResultsScreenWeb.java b/src/test/java/com/znsio/teswiz/screen/web/googlesearch/GoogleSearchResultsScreenWeb.java new file mode 100644 index 000000000..c637112c2 --- /dev/null +++ b/src/test/java/com/znsio/teswiz/screen/web/googlesearch/GoogleSearchResultsScreenWeb.java @@ -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 getSearchResults() { + visually.checkWindow(SCREEN_NAME, "india - Google Search"); + return driver.findElements(searchResultsHeadings).stream().map(webElement -> webElement.getText()).collect(Collectors.toList()); + } +} diff --git a/src/test/resources/com/znsio/teswiz/features/googlesearch.feature b/src/test/resources/com/znsio/teswiz/features/googlesearch.feature index 18e521a79..45fad63d2 100644 --- a/src/test/resources/com/znsio/teswiz/features/googlesearch.feature +++ b/src/test/resources/com/znsio/teswiz/features/googlesearch.feature @@ -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" diff --git a/src/test/resources/environments.json b/src/test/resources/environments.json index bfc2aaa5d..5337fa24a 100644 --- a/src/test/resources/environments.json +++ b/src/test/resources/environments.json @@ -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/"