Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(java): 🔊 added creation of all logs supported by appium #232

Merged
merged 5 commits into from
Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/test-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ jobs:
name: target
path: ${{ github.workspace }}/core-java

- name: Install Appium
run: npm install -g appium
- name: Install Appium 2.0
run: |
npm install -g appium@next
appium driver install uiautomator2

- name: Start Selenium Grid
run: java -jar core-java/libs/selenium-server-4.4.0.jar standalone > core-java/selenium-grid.log &
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
<a href="https://mvnrepository.com/artifact/com.github.wasiqb.boyka/boyka-framework">
<img src="https://img.shields.io/maven-central/v/com.github.wasiqb.boyka/boyka-framework.svg?style=for-the-badge" alt="Maven Central" />
</a>
<a href="https://github.com/WasiqBhamla/boyka-framework/releases/tag/v0.8.1">
<img src="https://img.shields.io/github/downloads/WasiqBhamla/boyka-framework/v0.8.1/total?color=brightgreen&label=Downloads%20for%20v0.8.1&logo=GitHub&style=for-the-badge" alt="GitHub releases" />
<a href="https://github.com/WasiqBhamla/boyka-framework/releases/tag/v0.9.0">
<img src="https://img.shields.io/github/downloads/WasiqBhamla/boyka-framework/v0.9.0/total?color=brightgreen&label=Downloads%20for%20v0.9.0&logo=GitHub&style=for-the-badge" alt="GitHub releases" />
</a>
<a href="https://github.com/WasiqBhamla/boyka-framework/blob/master/LICENSE">
<img src="https://img.shields.io/github/license/WasiqBhamla/boyka-framework.svg?style=for-the-badge" alt="license" />
Expand Down
5 changes: 3 additions & 2 deletions core-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
~ copies or substantial portions of the Software.
-->

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.wasiqb.boyka</groupId>
<artifactId>boyka-framework</artifactId>
Expand All @@ -40,7 +41,7 @@
<log4j-core.version>2.19.0</log4j-core.version>
<log4j-api.version>2.19.0</log4j-api.version>
<jackson-dataformat-yaml.version>2.13.4</jackson-dataformat-yaml.version>
<jackson-databind.version>2.13.4</jackson-databind.version>
<jackson-databind.version>2.13.4.2</jackson-databind.version>
<ok2curl.version>0.8.0</ok2curl.version>
<everit-json-schema.version>1.5.1</everit-json-schema.version>
<maven-clean-plugin.version>3.2.0</maven-clean-plugin.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@

import static com.github.wasiqb.boyka.actions.CommonActions.getDriverAttribute;
import static com.github.wasiqb.boyka.actions.CommonActions.performDriverAction;
import static com.github.wasiqb.boyka.enums.Message.ERROR_CREATING_LOGS;
import static com.github.wasiqb.boyka.enums.Message.ERROR_SAVING_SCREENSHOT;
import static com.github.wasiqb.boyka.enums.Message.ERROR_WRITING_LOGS;
import static com.github.wasiqb.boyka.sessions.ParallelSession.getSession;
import static com.github.wasiqb.boyka.utils.ErrorHandler.handleAndThrow;
import static java.lang.String.format;
import static java.lang.System.getProperty;
import static java.lang.Thread.currentThread;
import static java.text.MessageFormat.format;
import static org.apache.commons.io.FileUtils.copyFile;
import static org.apache.logging.log4j.LogManager.getLogger;
import static org.openqa.selenium.OutputType.FILE;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand All @@ -40,6 +46,7 @@
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WindowType;

/**
Expand Down Expand Up @@ -265,6 +272,29 @@ public static void refresh () {
LOGGER.traceExit ();
}

/**
* Save all the available logs to files in `logs` folder.
*/
public static void saveLogs () {
performDriverAction (d -> {
final var logSetting = getSession ().getMobileSetting ()
.getServer ()
.getLogs ();
if (!logSetting.isEnable ()) {
return;
}
try {
final var logTypes = d.manage ()
.logs ()
.getAvailableLogTypes ();
logTypes.forEach (logType -> saveLogType (d, logType, logSetting.getPath ()));
} catch (final WebDriverException e) {
LOGGER.catching (e);
LOGGER.warn ("Error while saving different logs: {}", e.getMessage ());
}
});
}

/**
* Switch to an iFrame.
*
Expand Down Expand Up @@ -340,7 +370,7 @@ public static void takeScreenshot () {
final var timeStamp = date.format (Calendar.getInstance ()
.getTime ());
final var fileName = "%s/%s-%s.%s";
takeScreenshot (format (fileName, path, prefix, timeStamp, extension));
takeScreenshot (String.format (fileName, path, prefix, timeStamp, extension));
}

/**
Expand Down Expand Up @@ -412,6 +442,26 @@ public static List<String> windowHandles () {
return new ArrayList<> (handles);
}

private static <D extends WebDriver> void saveLogType (final D driver, final String logType, final String logPath) {
final var logEntries = driver.manage ()
.logs ()
.get (logType);
final var fileName = format ("{0}/{1}/{2}-{3}.log", getProperty ("user.dir"), logPath, logType,
currentThread ().getId ());
try (final var writer = new BufferedWriter (new FileWriter (fileName))) {
logEntries.forEach (logEntry -> {
try {
writer.write (logEntry.getMessage ());
writer.write (getProperty ("line.separator"));
} catch (final IOException e) {
handleAndThrow (ERROR_WRITING_LOGS, e);
}
});
} catch (final IOException e) {
handleAndThrow (ERROR_CREATING_LOGS, e);
}
}

private DriverActions () {
// Utility class.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public enum Message {
* Empty browser is not allowed.
*/
EMPTY_BROWSER_NOT_ALLOWED ("Browser type cannot be empty in the config..."),
/**
* Error while creating log file for a log type.
*/
ERROR_CREATING_LOGS ("Error occurred while creating log file for log type: {}..."),
/**
* Error deleting log files.
*/
Expand Down Expand Up @@ -105,6 +109,10 @@ public enum Message {
* Error writing file
*/
ERROR_WRITING_FILE ("Error occurred writing file [{0}]..."),
/**
* Error while writing logs to file.
*/
ERROR_WRITING_LOGS ("Error occurred while writing logs..."),
/**
* Host name is required for Remote execution
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import java.nio.file.Path;
import java.util.HashMap;

import com.github.wasiqb.boyka.config.FrameworkSetting;
import com.github.wasiqb.boyka.config.ui.TimeoutSetting;
import com.github.wasiqb.boyka.config.ui.mobile.device.ApplicationSetting;
import com.github.wasiqb.boyka.config.ui.mobile.device.DeviceSetting;
Expand Down Expand Up @@ -128,15 +127,12 @@ public static DriverManager createDriver (final PlatformType platformType, final
return LOGGER.traceExit (instance);
}

private final String driverKey;
private final PlatformType platformType;
private final FrameworkSetting setting;
private final PlatformType platformType;

private DriverManager (final PlatformType platformType, final String driverKey) {
LOGGER.traceEntry ();
this.platformType = platformType;
this.driverKey = driverKey;
this.setting = getSession ().getSetting ();
getSession ().setConfigKey (driverKey);
LOGGER.traceExit ();
}

Expand Down Expand Up @@ -263,8 +259,7 @@ private void setLocalUiAutomatorOptions (final UiAutomator2Options options, fina
}

private void setupAndroidDriver () {
final var androidSetting = this.setting.getUi ()
.getMobileSetting (this.driverKey);
final var androidSetting = getSession ().getMobileSetting ();
final var serverSetting = androidSetting.getServer ();
final var deviceSetting = androidSetting.getDevice ();
final var automation = deviceSetting.getAutomation ();
Expand All @@ -280,8 +275,7 @@ private void setupAndroidSettings (final DeviceSetting setting) {
}

private void setupAppiumServer () {
final var serviceManager = new ServiceManager (this.setting.getUi ()
.getMobileSetting (this.driverKey)
final var serviceManager = new ServiceManager (getSession ().getMobileSetting ()
.getServer ());
getSession ().setServiceManager (serviceManager);
getSession ().getServiceManager ()
Expand Down Expand Up @@ -391,8 +385,7 @@ private void setupUiAutomatorDriver (final ServerSetting serverSetting, final De

private void setupWebDriver () {
LOGGER.traceEntry ();
final var webSetting = this.setting.getUi ()
.getWebSetting (this.driverKey);
final var webSetting = getSession ().getWebSetting ();
switch (requireNonNull (webSetting.getBrowser (), EMPTY_BROWSER_NOT_ALLOWED)) {
case CHROME:
setDriver (this.platformType, setupChromeDriver (webSetting));
Expand All @@ -415,7 +408,8 @@ private void setupWebDriver () {
break;
}
setDriverSize (webSetting);
setDriverWaits (this.setting.getUi ()
setDriverWaits (getSession ().getSetting ()
.getUi ()
.getTimeout ());
LOGGER.traceExit ();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URL;

import com.github.wasiqb.boyka.config.ui.mobile.server.AndroidSetting;
import com.github.wasiqb.boyka.config.ui.mobile.server.LogSetting;
import com.github.wasiqb.boyka.config.ui.mobile.server.ServerSetting;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServerHasNotBeenStartedLocallyException;
Expand Down Expand Up @@ -77,6 +74,11 @@ public class ServiceManager {
}
}

/**
* Determines if the server is running.
*
* @return true, if server is running, else false.
*/
public boolean isRunning () {
if (isCloud ()) {
return true;
Expand All @@ -85,7 +87,7 @@ public boolean isRunning () {
LOG.trace ("Checking if Appium Service is running...");
return this.service.isRunning ();
}
final SocketAddress address = new InetSocketAddress (this.setting.getHost (), this.setting.getPort ());
final var address = new InetSocketAddress (this.setting.getHost (), this.setting.getPort ());
try (final Socket socket = new Socket ()) {
socket.connect (address, 2000);
} catch (final IOException e) {
Expand Down Expand Up @@ -151,7 +153,7 @@ private void buildService () {
}

private String getUrl () {
final StringBuilder sb = new StringBuilder (this.setting.getProtocol ()
final var sb = new StringBuilder (this.setting.getProtocol ()
.name ()).append ("://");
if (isCloud ()) {
sb.append (this.setting.getUserName ())
Expand All @@ -176,7 +178,7 @@ private boolean isCloud () {
}

private void setAndroidArguments () {
final AndroidSetting android = this.setting.getAndroid ();
final var android = this.setting.getAndroid ();
if (android != null) {
setArgument (BOOTSTRAP_PORT_NUMBER, android.getBootstrapPort ());
setArgument (REBOOT, android.isReboot ());
Expand All @@ -186,7 +188,7 @@ private void setAndroidArguments () {

private void setAppiumJS () {
if (this.setting.getAppiumPath () != null) {
final File appJs = new File (this.setting.getAppiumPath ());
final var appJs = new File (this.setting.getAppiumPath ());
this.builder.withAppiumJS (appJs);
}
}
Expand Down Expand Up @@ -225,7 +227,7 @@ private void setCommonArguments () {
}

private void setLogArguments () {
final LogSetting logs = this.setting.getLogs ();
final var logs = this.setting.getLogs ();
if (!logs.isEnable ()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import static org.apache.logging.log4j.LogManager.getLogger;

import com.github.wasiqb.boyka.config.FrameworkSetting;
import com.github.wasiqb.boyka.config.ui.mobile.MobileSetting;
import com.github.wasiqb.boyka.config.ui.web.WebSetting;
import com.github.wasiqb.boyka.enums.PlatformType;
import com.github.wasiqb.boyka.manager.ServiceManager;
import lombok.Data;
Expand All @@ -39,6 +41,7 @@
public class DriverSession<D extends WebDriver> {
private static final Logger LOGGER = getLogger ();

private String configKey;
private D driver;
private PlatformType platformType;
private ServiceManager serviceManager;
Expand All @@ -52,4 +55,24 @@ public class DriverSession<D extends WebDriver> {
this.setting = loadSetting ();
LOGGER.traceExit ();
}

/**
* Gets Current Mobile settings
*
* @return Mobile setting
*/
public MobileSetting getMobileSetting () {
return this.setting.getUi ()
.getMobileSetting (this.configKey);
}

/**
* Gets current Web settings
*
* @return Web Setting
*/
public WebSetting getWebSetting () {
return this.setting.getUi ()
.getWebSetting (this.configKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.github.wasiqb.boyka.testng.mobile;

import static com.github.wasiqb.boyka.actions.DriverActions.saveLogs;
import static com.github.wasiqb.boyka.manager.DriverManager.closeDriver;
import static com.github.wasiqb.boyka.manager.DriverManager.createDriver;
import static com.github.wasiqb.boyka.sessions.ParallelSession.getSession;
Expand Down Expand Up @@ -51,6 +52,7 @@ public void setupTestClass (final PlatformType appType, final String driverKey)
*/
@AfterClass (description = "Tear down test class")
public void tearDownTestClass () {
saveLogs ();
closeDriver ();
}

Expand Down
7 changes: 5 additions & 2 deletions core-java/src/test/resources/boyka-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@
"host": "127.0.0.1",
"port": 4723,
"base_path": "/wd/hub",
"session_override": true
"session_override": true,
"allow_insecure": [
"get_server_logs"
]
},
"device": {
"os": "ANDROID",
Expand Down Expand Up @@ -189,7 +192,7 @@
"projectName": "BrowserStack Android Project",
"buildName": "Test BrowserStack Build",
"sessionName": "Test BrowserStack Session",
"appiumVersion": "1.22.0",
"appiumVersion": "2.0.0",
"deviceLogs": true,
"networkLogs": true,
"debug": true,
Expand Down
23 changes: 23 additions & 0 deletions core-java/test-suites/testng-mobile-pr.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ MIT License
~
~ Copyright (c) 2022 Wasiq Bhamla
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"), to deal
~ in the Software without restriction, including without limitation the rights
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
~ copies of the Software, and to permit persons to whom the Software is
~ furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in all
~ copies or substantial portions of the Software.
-->

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Boyka PR Mobile Suite" verbose="2">
<suite-files>
<suite-file path="testng-mobile-local.xml"/>
</suite-files>
</suite>