[英]UiAutomator — Add Widget to Home Screen
我對Google的uiautomator有相當多的經驗; 然而,在手機的主屏幕上添加小部件時,我似乎感到難過。 現在讓我們保持簡單,並假設添加小部件的屏幕是空的。 思考過程是打開應用程序抽屜>單擊窗口小部件選項卡>找到要添加的窗口小部件>長按並將窗口小部件拖動到主屏幕。 雖然小部件似乎不是“長時間可點擊”。 任何想法/建議/解決方案將不勝感激。 我實現的代碼如下。
@Override
protected void setUp() throws UiObjectNotFoundException {
getUiDevice().pressHome();
new UiObject(new UiSelector().className(TEXT_VIEW).description("Apps")).clickAndWaitForNewWindow();
new UiObject(new UiSelector().className(TEXT_VIEW).text("Widgets")).click();
UiScrollable widgets = new UiScrollable(new UiSelector().scrollable(true));
widgets.setAsHorizontalList();
widgets.flingToEnd(MAX_SWIPES);
UiObject widget = widgets.getChildByText(
new UiSelector().className(TEXT_VIEW).resourceId("com.android.launcher:id/widget_name"),
WIDGET_NAME
);
// Returns true
System.out.println("exists(): " + widget.exists());
// Returns false...
System.out.println("longClickable(): " + widget.isLongClickable());
widget.longClick();
// Also tried...
int startX = sonosWidget.getVisibleBounds().centerX();
int startY = sonosWidget.getVisibleBounds().centerY();
getUiDevice().drag(startX, startY, 0, 0, 3);
}
使用Anders的想法,我設法長按小部件並將其拖到家中的某個地方,但后來我回到小部件列表之前簡要地看到了我的配置活動:((Kolin代碼)
@Before fun setWidgetOnHome() {
mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
val screenSize = Point(mDevice.displayWidth, mDevice.displayHeight)
val screenCenter = Point(screenSize.x / 2, screenSize.y / 2)
mDevice.pressHome()
val launcherPackage = mDevice.launcherPackageName!!
mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT)
// attempt long press
mDevice.swipe(arrayOf(screenCenter, screenCenter), 150)
pauseTest(2000)
mDevice.findObject(By.text("Widgets")).click()
mDevice.waitForIdle()
val y = screenSize.y / 2
var widget = mDevice.findObject(By.text("Efficio"))
var additionalSwipe = 1
while (widget == null || additionalSwipe > 0) {
mDevice.swipe(screenCenter.x, y, screenCenter.x, 0, 150)
mDevice.waitForIdle()
if (widget == null) {
widget = mDevice.findObject(By.text("Efficio"))
} else {
additionalSwipe--
}
}
val b = widget.visibleBounds
val c = Point(b.left + 150, b.bottom + 150)
val dest = Point(c.x + 250, c.y + 250)
mDevice.swipe(arrayOf(c, c, dest), 150)
}
我相信有些事情正在發生,但又是什么? : - /就像背部被點擊一樣
uiautomator Android中的長按也可以通過以下方式執行:
public boolean drag (int startX, int startY, int endX, int endY, int steps)
執行從一個坐標到另一個坐標的滑動。 您可以通過指定步數來控制滑動的平滑度和速度。 每個步驟執行被限制為每步5毫秒,因此對於100步,滑動將花費大約0.5秒來完成。
參數:
startX ::起始坐標的X軸值
startY ::起始坐標的Y軸值
endX ::結束坐標的X軸值
endY ::結束坐標的Y軸值
Steps ::是滑動操作的步驟數。
退貨:
如果執行滑動,則為true;如果操作失敗或坐標無效,則為false。
自:
Android API等級18
所以給(startX,startY)=(endX,endY)。 這相當於長按一下。
感謝@ geob-o-matic發布的代碼。
我必須添加一些修改才能工作:
在代碼中內聯的額外解釋,您可能必須更改您的案例中的某些值,以便通過注釋。
const val TIMEOUT: Long = 5000
// WIDGET_SELECTION_AT_X: USE here true or depending
// if you have to scroll at the X or Y axis when
// navigating through widget selection screen.
const val WIDGET_SELECTION_AT_X: Boolean = true
const val WIDGET_NAME: String = "MyAppWigdetName"
@RunWith(AndroidJUnit4::class)
class WidgetAutomatorTest {
private val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
@Before
fun setWidgetOnHome() {
val screenSize = Point(mDevice.displayWidth, mDevice.displayHeight)
val screenCenter = Point(screenSize.x / 2, screenSize.y / 2)
// showWidgets: This a point on screen between the bottom icons
// and the widgets, its a point that has no objects on a Galaxy S5
// device with stock Launcher. Most probably you have to modify it in
// your device or use an empty homescreen and just long press at
// the center of it.
val showWidgets = Point(825, 1500)
mDevice.pressHome()
val launcherPackage = mDevice.launcherPackageName!!
mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), TIMEOUT)
// attempt long press
// Here you can use the screenCenter if you don't have any widget standing/living there!
// ie. mDevice.swipe(arrayOf(showWidgets, screenCenter), 150)
mDevice.swipe(arrayOf(showWidgets, showWidgets), 150)
// Navigate to the system's widget selector, localaize if needed.
mDevice.findObject(By.text("Widgets")).click()
var diment = screenSize.y / 2
if (WIDGET_SELECTION_AT_X) {
diment = screenSize.x / 2
}
var widget = findMyWidget(WIDGET_NAME)
while (widget == null) {
if (WIDGET_SELECTION_AT_X) {
// Swipe left to right
mDevice.swipe(diment, screenCenter.y, 0, screenCenter.y, 150)
} else {
// Swipe top to bottom
mDevice.swipe(screenCenter.x, diment, screenCenter.x, 0, 150)
}
widget = findMyWidget(WIDGET_NAME)
}
// Throw the selected widget on screen
val b = widget.visibleBounds
val c = Point(b.left + 150, b.bottom + 150)
val dest = Point(c.x + 250, c.y + 250)
mDevice.swipe(arrayOf(c, c, dest), 150)
}
private fun findMyWidget(withName: String): UiObject2? {
return mDevice.findObject(By.text(withName))
}
@Test
fun addWidget() {
// Press the button on the Widget Configuration Activity
val okButton = mDevice.findObject(UiSelector()
.text("Add widget")
.className("android.widget.Button"))
okButton.click()
// Find the just added widget
val widget = mDevice.findObject(By.descContains(WIDGET_NAME))
// Click outside the widget in order to added in the screen
mDevice.click(widget.visibleBounds.left - 150, widget.visibleBounds.top - 150)
}
}
注意。 提供的示例在Galaxy S5,Android 6.0.1,1080x1920中完美運行。
你可以使用dragTo(相同的UI對象,步驟)
*步驟應> 100,以確保點擊足夠長
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.