1
+ package samples .android ;
2
+
3
+ import io .appium .java_client .android .AndroidDriver ;
4
+ import io .appium .java_client .android .AndroidElement ;
5
+ import org .openqa .selenium .By ;
6
+ import org .openqa .selenium .WebElement ;
7
+ import org .openqa .selenium .remote .DesiredCapabilities ;
8
+ import org .openqa .selenium .support .ui .ExpectedConditions ;
9
+ import org .openqa .selenium .support .ui .WebDriverWait ;
10
+ import org .testng .Assert ;
11
+ import org .testng .annotations .AfterClass ;
12
+ import org .testng .annotations .BeforeClass ;
13
+ import org .testng .annotations .Test ;
14
+ import samples .BaseTest ;
15
+
16
+ import java .io .File ;
17
+ import java .io .IOException ;
18
+ import java .nio .file .Paths ;
19
+
20
+ public class AndroidBasicInteractionsTest extends BaseTest {
21
+ private AndroidDriver <WebElement > driver ;
22
+ private final String SEARCH_ACTIVITY = ".app.SearchInvoke" ;
23
+ private final String ALERT_DIALOG_ACTIVITY = ".app.AlertDialogSamples" ;
24
+ private final String PACKAGE = "io.appium.android.apis" ;
25
+
26
+ private static String getEnvVariable (String key , String defaultValue ) {
27
+ return System .getProperty (key ) != null ? System .getProperty (key ) : defaultValue ;
28
+ }
29
+
30
+ private static String getResourcesPath () {
31
+ return Paths .get (getEnvVariable ("user.dir" , System .getProperty ("project.basedir" )), "src" , "test" , "resources" ).toString ();
32
+ }
33
+
34
+ @ BeforeClass
35
+ public void setUp () throws IOException {
36
+ File appDir = new File (getResourcesPath (), "apps" );
37
+ File app = new File (appDir .getCanonicalPath (), "ApiDemos-debug.apk" );
38
+ DesiredCapabilities capabilities = new DesiredCapabilities ();
39
+ capabilities .setCapability ("deviceName" , "Android Emulator" );
40
+ capabilities .setCapability ("app" , app .getAbsolutePath ());
41
+ driver = new AndroidDriver <WebElement >(getServiceUrl (), capabilities );
42
+ }
43
+
44
+ @ AfterClass
45
+ public void tearDown () {
46
+ driver .quit ();
47
+ }
48
+
49
+
50
+ @ Test ()
51
+ public void testSendKeys () {
52
+ driver .startActivity (PACKAGE , SEARCH_ACTIVITY );
53
+ AndroidElement searchBoxEl = (AndroidElement ) driver .findElementById ("txt_query_prefill" );
54
+ searchBoxEl .sendKeys ("Hello world!" );
55
+ AndroidElement onSearchRequestedBtn = (AndroidElement ) driver .findElementById ("btn_start_search" );
56
+ onSearchRequestedBtn .click ();
57
+ AndroidElement searchText = (AndroidElement ) new WebDriverWait (driver , 30 )
58
+ .until (ExpectedConditions .visibilityOfElementLocated (By .id ("android:id/search_src_text" )));
59
+ String searchTextValue = searchText .getText ();
60
+ Assert .assertEquals (searchTextValue , "Hello world!" );
61
+ }
62
+
63
+ @ Test
64
+ public void testOpensAlert () {
65
+ // Open the "Alert Dialog" activity of the android app
66
+ driver .startActivity (PACKAGE , ALERT_DIALOG_ACTIVITY );
67
+
68
+ // Click button that opens a dialog
69
+ AndroidElement openDialogButton = (AndroidElement ) driver .findElementById ("io.appium.android.apis:id/two_buttons" );
70
+ openDialogButton .click ();
71
+
72
+ // Check that the dialog is there
73
+ AndroidElement alertElement = (AndroidElement ) driver .findElementById ("android:id/alertTitle" );
74
+ String alertText = alertElement .getText ();
75
+ Assert .assertEquals (alertText , "Lorem ipsum dolor sit aie consectetur adipiscing\n Plloaso mako nuto siwuf cakso dodtos anr koop." );
76
+ AndroidElement closeDialogButton = (AndroidElement ) driver .findElementById ("android:id/button1" );
77
+
78
+ // Close the dialog
79
+ closeDialogButton .click ();
80
+ }
81
+ }
0 commit comments