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: Add proper info to the /status API response #1022

Merged
merged 3 commits into from
Jul 15, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,22 @@

package io.appium.espressoserver.lib.handlers

import androidx.test.platform.app.InstrumentationRegistry
import io.appium.espressoserver.lib.helpers.getEspressoServerVersion
import io.appium.espressoserver.lib.model.AppiumParams
import io.appium.espressoserver.lib.model.BuildInfo

class Status : RequestHandler<AppiumParams, Void?>, NoSessionCommandHandler {

override fun handleInternal(params: AppiumParams): Void? {
return null
class Status : RequestHandler<AppiumParams, io.appium.espressoserver.lib.model.Status>, NoSessionCommandHandler {

override fun handleInternal(params: AppiumParams): io.appium.espressoserver.lib.model.Status {
return io.appium.espressoserver.lib.model.Status(
ready = true,
message = "The server is ready to accept new connections",
build = BuildInfo(
version = getEspressoServerVersion(),
packageName = InstrumentationRegistry.getInstrumentation().targetContext.packageName
)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.appium.espressoserver.lib.helpers

// This value is updated automatically by the NPM versioning script
// It should be in sync with the NPM module version from package.json
private const val VERSION = "3.1.0"

fun getEspressoServerVersion() = VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.google.gson.JsonParseException
import fi.iki.elonen.NanoHTTPD.Method
import io.appium.espressoserver.lib.handlers.*
import io.appium.espressoserver.lib.handlers.PointerEventHandler.TouchType.*
import io.appium.espressoserver.lib.handlers.Status
import io.appium.espressoserver.lib.handlers.TouchAction
import io.appium.espressoserver.lib.handlers.exceptions.*
import io.appium.espressoserver.lib.helpers.AndroidLogger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@

package io.appium.espressoserver.lib.model

import com.google.gson.annotations.Expose
import io.appium.espressoserver.lib.handlers.exceptions.AppiumException


const val SESSION_ID_PARAM_NAME = "sessionId"
const val ELEMENT_ID_PARAM_NAME = "elementId"

open class AppiumParams {
var uriParams:MutableMap<String, String>? = null
@Expose(serialize = false, deserialize = true)
private var uriParams: MutableMap<String, String>? = null

val sessionId: String?
get() = getUriParameterValue(SESSION_ID_PARAM_NAME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@

package io.appium.espressoserver.lib.model

data class Location(var x: Int? = null, var y: Int? = null) : AppiumParams()
data class Location(
val x: Int,
val y: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ package io.appium.espressoserver.lib.model

import android.graphics.Rect

class Rect(
var x: Int? = null,
var y: Int? = null,
var width: Int? = null,
var height: Int? = null
) : AppiumParams() {
data class Rect(
var x: Int,
var y: Int,
var width: Int,
var height: Int
) {
companion object {
fun fromBounds(bounds: Rect): io.appium.espressoserver.lib.model.Rect =
io.appium.espressoserver.lib.model.Rect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ package io.appium.espressoserver.lib.model
data class Size(
val width: Int,
val height: Int
) : AppiumParams()
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.espressoserver.lib.model

data class Status(
val ready: Boolean,
val message: String,
val build: BuildInfo
)

data class BuildInfo(
val version: String,
val packageName: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ data class WindowRect(
val height: Int,
val x: Int,
val y: Int
) : AppiumParams()
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ package io.appium.espressoserver.lib.model
data class WindowSize(
val width: Int,
val height: Int
) : AppiumParams()
)
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@
"lint:server": "cd espresso-server && ./gradlew lint || cd ..",
"lint:fix": "npm run lint -- --fix",
"prepare": "npm run rebuild",
"sync-version": "node ./scripts/sync-version.js --package-version=${npm_package_version}",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, i didn't know of the npm_package_version

"test": "npm run test:node",
"test:node": "mocha --exit --timeout 1m \"./test/unit/**/*-specs.js\"",
"test:server": "cd espresso-server && ./gradlew test --stacktrace || cd ..",
"version": "npm run sync-version && npm run build:server",
"e2e-test": "mocha --exit --timeout 5m \"./test/functional/**/*-specs.js\""
},
"peerDependencies": {
Expand Down Expand Up @@ -130,6 +132,7 @@
"rimraf": "^5.0.0",
"semantic-release": "^24.0.0",
"sinon": "^17.0.0",
"semver": "^7.3.7",
"ts-node": "^10.9.1",
"typescript": "^5.4.2",
"webdriverio": "^8.0.2",
Expand Down
47 changes: 47 additions & 0 deletions scripts/sync-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const path = require('node:path');
const { logger, fs } = require('appium/support');
const semver = require('semver');

const LOG = logger.getLogger('VersionSync');

const ROOT_DIR = path.resolve(__dirname, '..');
const ESPRESSO_SERVER_ROOT = path.join(ROOT_DIR, 'espresso-server');
const VERSION_FILE = path.join(
ESPRESSO_SERVER_ROOT, 'library', 'src', 'main', 'java',
'io', 'appium', 'espressoserver',
'lib', 'helpers', 'Version.kt'
);
const VERSION_PATTERN = /VERSION\s*=\s*"([0-9.]+)"/;

function parseArgValue (argName) {
const argNamePattern = new RegExp(`^--${argName}\\b`);
for (let i = 1; i < process.argv.length; ++i) {
const arg = process.argv[i];
if (argNamePattern.test(arg)) {
return arg.includes('=') ? arg.split('=')[1] : process.argv[i + 1];
}
}
return null;
}

async function syncModuleVersion () {
const origContent = await fs.readFile(VERSION_FILE, 'utf8');
const espressoVersionMatch = VERSION_PATTERN.exec(origContent);
if (!espressoVersionMatch) {
throw new Error(`Could not parse Espresso module version from '${VERSION_FILE}'`);
}
const packageVersion = parseArgValue('package-version');
if (!packageVersion) {
throw new Error('No package version argument (use `--package-version=xxx`)');
}
if (!semver.valid(packageVersion)) {
throw new Error(
`Invalid version specified '${packageVersion}'. Version should be in the form '1.2.3'`
);
}
const updatedContent = origContent.replace(espressoVersionMatch[1], packageVersion);
await fs.writeFile(VERSION_FILE, updatedContent, 'utf8');
LOG.info(`Synchronized module version '${packageVersion}' to '${VERSION_FILE}'`);
}

(async () => await syncModuleVersion())();
Loading