mirror of
				https://github.com/ARM-software/workload-automation.git
				synced 2025-10-30 22:54:18 +00:00 
			
		
		
		
	Add more convenience methods to BaseUiAutomation.
These additions make it easier to get UiObjects and perform UiDevice based gestures when writing UiAutomation workloads
This commit is contained in:
		| @@ -20,6 +20,7 @@ import java.io.File; | |||||||
| import java.io.BufferedReader; | import java.io.BufferedReader; | ||||||
| import java.io.InputStreamReader; | import java.io.InputStreamReader; | ||||||
| import java.util.concurrent.TimeoutException; | import java.util.concurrent.TimeoutException; | ||||||
|  | import java.util.concurrent.TimeUnit; | ||||||
|  |  | ||||||
| import android.app.Activity; | import android.app.Activity; | ||||||
| import android.os.Bundle; | import android.os.Bundle; | ||||||
| @@ -33,6 +34,7 @@ import com.android.uiautomator.testrunner.UiAutomatorTestCase; | |||||||
|  |  | ||||||
| public class BaseUiAutomation extends UiAutomatorTestCase { | public class BaseUiAutomation extends UiAutomatorTestCase { | ||||||
|  |  | ||||||
|  |     public long waitTimeout = TimeUnit.SECONDS.toMillis(4); | ||||||
|  |  | ||||||
|     public void sleep(int second) { |     public void sleep(int second) { | ||||||
|         super.sleep(second * 1000); |         super.sleep(second * 1000); | ||||||
| @@ -40,7 +42,7 @@ public class BaseUiAutomation extends UiAutomatorTestCase { | |||||||
|  |  | ||||||
|     public boolean takeScreenshot(String name) { |     public boolean takeScreenshot(String name) { | ||||||
|         Bundle params = getParams(); |         Bundle params = getParams(); | ||||||
| 	String png_dir = params.getString("workdir"); |         String png_dir = params.getString("workdir"); | ||||||
|  |  | ||||||
|         try { |         try { | ||||||
|             return getUiDevice().takeScreenshot(new File(png_dir, name + ".png")); |             return getUiDevice().takeScreenshot(new File(png_dir, name + ".png")); | ||||||
| @@ -109,5 +111,94 @@ public class BaseUiAutomation extends UiAutomatorTestCase { | |||||||
|             throw new TimeoutException("Timed out waiting for Logcat text \"%s\"".format(searchText)); |             throw new TimeoutException("Timed out waiting for Logcat text \"%s\"".format(searchText)); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } |  | ||||||
|  |  | ||||||
|  |     public UiObject getUiObjectByResourceId(String resourceId, String className) throws Exception { | ||||||
|  |         UiObject object = new UiObject(new UiSelector().resourceId(resourceId) | ||||||
|  |                                                        .className(className)); | ||||||
|  |         if (!object.waitForExists(waitTimeout)) { | ||||||
|  |            throw new UiObjectNotFoundException(String.format("Could not find \"%s\" \"%s\"", | ||||||
|  |                                                               resourceId, className)); | ||||||
|  |         }; | ||||||
|  |         return object; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public UiObject getUiObjectByDescription(String description, String className) throws Exception { | ||||||
|  |         UiObject object = new UiObject(new UiSelector().descriptionContains(description) | ||||||
|  |                                                        .className(className)); | ||||||
|  |         if (!object.waitForExists(waitTimeout)) { | ||||||
|  |             throw new UiObjectNotFoundException(String.format("Could not find \"%s\" \"%s\"", | ||||||
|  |                                                               description, className)); | ||||||
|  |         }; | ||||||
|  |         return object; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public UiObject getUiObjectByText(String text, String className) throws Exception { | ||||||
|  |         UiObject object = new UiObject(new UiSelector().textContains(text) | ||||||
|  |                                                       .className(className)); | ||||||
|  |         if (!object.waitForExists(waitTimeout)) { | ||||||
|  |             throw new UiObjectNotFoundException(String.format("Could not find \"%s\" \"%s\"", | ||||||
|  |                                                               text, className)); | ||||||
|  |         }; | ||||||
|  |         return object; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public int getDisplayHeight () { | ||||||
|  |         return getUiDevice().getInstance().getDisplayHeight(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public int getDisplayWidth () { | ||||||
|  |         return getUiDevice().getInstance().getDisplayWidth(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public int getDisplayCentreWidth () { | ||||||
|  |         return getDisplayWidth() / 2; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public int getDisplayCentreHeight () { | ||||||
|  |         return getDisplayHeight() / 2; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public void tapDisplayCentre () { | ||||||
|  |         tapDisplay(getDisplayCentreWidth(),  getDisplayCentreHeight()); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public void tapDisplay (int x, int y) { | ||||||
|  |         getUiDevice().getInstance().click(x, y); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public void uiDeviceSwipeUp (int steps) { | ||||||
|  |         getUiDevice().getInstance().swipe( | ||||||
|  |             getDisplayCentreWidth(), | ||||||
|  |             (getDisplayCentreHeight() / 2), | ||||||
|  |             getDisplayCentreWidth(), | ||||||
|  |             (getDisplayCentreHeight() + (getDisplayCentreHeight() / 2)), | ||||||
|  |             steps); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public void uiDeviceSwipeDown (int steps) { | ||||||
|  |         getUiDevice().getInstance().swipe( | ||||||
|  |             getDisplayCentreWidth(), | ||||||
|  |             (getDisplayCentreHeight() + (getDisplayCentreHeight() / 2)), | ||||||
|  |             getDisplayCentreWidth(), | ||||||
|  |             (getDisplayCentreHeight() / 2), | ||||||
|  |             steps); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public void uiDeviceSwipeLeft (int steps) { | ||||||
|  |         getUiDevice().getInstance().swipe( | ||||||
|  |             (getDisplayCentreWidth() + (getDisplayCentreWidth() / 2)), | ||||||
|  |             getDisplayCentreHeight(), | ||||||
|  |             (getDisplayCentreWidth() / 2), | ||||||
|  |             getDisplayCentreHeight(), | ||||||
|  |             steps); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public void uiDeviceSwipeRight (int steps) { | ||||||
|  |         getUiDevice().getInstance().swipe( | ||||||
|  |             (getDisplayCentreWidth() / 2), | ||||||
|  |             getDisplayCentreHeight(), | ||||||
|  |             (getDisplayCentreWidth() + (getDisplayCentreWidth() / 2)), | ||||||
|  |             getDisplayCentreHeight(), | ||||||
|  |             steps); | ||||||
|  |     } | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user