Skip to content

Commit

Permalink
feat: Add mobile endpoint to retrieve UIAutomator page source dump (#628
Browse files Browse the repository at this point in the history
)
  • Loading branch information
pr4bh4sh authored Oct 26, 2020
1 parent 5b1e9ff commit 52f9405
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
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

0 comments on commit 52f9405

Please sign in to comment.