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 mobile endpoint to retrieve UIAutomator page source dump #628

Merged
merged 5 commits into from
Oct 26, 2020
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
@@ -0,0 +1,35 @@
/*
* 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.handlers

import io.appium.espressoserver.lib.handlers.exceptions.AppiumException
import io.appium.espressoserver.lib.helpers.InteractionHelper
import io.appium.espressoserver.lib.model.AppiumParams
import java.io.ByteArrayOutputStream
import java.io.IOException

class UiautomatorPageSource : RequestHandler<AppiumParams, String> {

@Throws(AppiumException::class)
override fun handleInternal(params: AppiumParams): String = try {
val stream = ByteArrayOutputStream()
InteractionHelper.getUiDevice().dumpWindowHierarchy(stream)
String(stream.toByteArray())
} catch (e: IOException) {
throw AppiumException("Could not get page source with UiAutomator", e)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ internal class Router {
routeMap.addRoute(RouteDefinition(Method.POST, "/session/:sessionId/appium/execute_mobile/backdoor", MobileBackdoor(), MobileBackdoorParams::class.java))
routeMap.addRoute(RouteDefinition(Method.POST, "/session/:sessionId/appium/execute_mobile/:elementId/flash", MobileViewFlash(), ViewFlashParams::class.java))
routeMap.addRoute(RouteDefinition(Method.POST, "/session/:sessionId/appium/execute_mobile/uiautomator", Uiautomator(), UiautomatorParams::class.java))
routeMap.addRoute(RouteDefinition(Method.GET, "/session/:sessionId/appium/execute_mobile/uiautomator_page_source", UiautomatorPageSource(), AppiumParams::class.java))
routeMap.addRoute(RouteDefinition(Method.POST, "/session/:sessionId/appium/execute_mobile/:elementId/click_action", MobileClickAction(), MobileClickActionParams::class.java))
routeMap.addRoute(RouteDefinition(Method.POST, "/session/:sessionId/appium/execute_mobile/web_atoms", WebAtoms(), WebAtomsParams::class.java))
routeMap.addRoute(RouteDefinition(Method.POST, "/session/:sessionId/appium/execute_mobile/:elementId/dismiss_autofill", PerformAutofillDismissal(), AppiumParams::class.java))
Expand Down
2 changes: 2 additions & 0 deletions lib/commands/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ extensions.executeMobile = async function executeMobile (mobileCommand, opts = {

uiautomator: 'mobileUiautomator',

uiautomatorPageSource: 'mobileUiautomatorPageSource',

clickAction: 'mobileClickAction',

webAtoms: 'mobileWebAtoms',
Expand Down
9 changes: 9 additions & 0 deletions lib/commands/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,15 @@ commands.mobileUiautomator = async function mobileUiautomator (opts = {}) {
return await this.espresso.jwproxy.command(`/appium/execute_mobile/uiautomator`, 'POST', {strategy, locator, index, action});
};

/**
* Execute UiAutomator2 command to return the UI dump when AUT is in background.
* @throws {Error} if uiautomator view dump is unsuccessful
* @returns {string} uiautomator DOM xml as string
*/
commands.mobileUiautomatorPageSource = async function mobileUiautomatorPageSource () {
return await this.espresso.jwproxy.command(`/appium/execute_mobile/uiautomator_page_source`, 'GET');
};

/**
* Flash the element with given id.
* durationMillis and repeatCount are optional
Expand Down