Skip to content

Commit 63a8b6f

Browse files
Merge pull request aws-samples#6 from hunguyenn/master
Fixed inconsistent spacing and incompatiblity with < 4.3 devices
2 parents 83521a5 + 4cb0699 commit 63a8b6f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+410
-269
lines changed

src/test/java/Pages/AlertPage.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
* Copyright 2014-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
@@ -17,9 +17,7 @@
1717

1818
import io.appium.java_client.AppiumDriver;
1919
import io.appium.java_client.MobileElement;
20-
import io.appium.java_client.android.AndroidElement;
2120
import io.appium.java_client.pagefactory.AndroidFindBy;
22-
import org.openqa.selenium.Alert;
2321

2422
/**
2523
* A page for alerts
@@ -28,13 +26,13 @@ public class AlertPage extends BasePage {
2826
/**
2927
* The alert button
3028
*/
31-
@AndroidFindBy(id = "notifications_alert_button")
29+
@AndroidFindBy(name = "ALERT")
3230
private MobileElement alertButton;
3331

3432
/**
3533
* The toast button
3634
*/
37-
@AndroidFindBy(id = "notifications_toast_button")
35+
@AndroidFindBy(name = "TOAST")
3836
private MobileElement toastButton;
3937

4038
public AlertPage(AppiumDriver driver) {
@@ -45,30 +43,30 @@ public AlertPage(AppiumDriver driver) {
4543
* Toast isn't directly supported by Appium. The recommended way is to
4644
* take a screen shot and use a OCR program to read the toast content
4745
*/
48-
public void clickToastButton(){
46+
public void clickToastButton() {
4947
toastButton.click();
5048
}
5149

5250
/**
5351
* clicks the alert button
5452
*/
55-
public void clickAlertsButton(){
53+
public void clickAlertsButton() {
5654
alertButton.click();
5755
}
5856

5957
/**
6058
*
6159
* @return the message within the alert
6260
*/
63-
public String getAlertText(){
61+
public String getAlertText() {
6462
MobileElement alertMessage = (MobileElement) driver.findElementsByClassName("android.widget.TextView").get(1);
6563
return alertMessage.getText();
6664
}
6765

6866
/**
6967
* accepts the alert
7068
*/
71-
public void acceptAlertMessage(){
69+
public void acceptAlertMessage() {
7270
MobileElement alertOkButton = (MobileElement) driver.findElementsByClassName("android.widget.Button").get(0);
7371
alertOkButton.click();
7472
}

src/test/java/Pages/BasePage.java

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
* Copyright 2014-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import io.appium.java_client.AppiumDriver;
2020
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
21+
import org.openqa.selenium.WebElement;
2122
import org.openqa.selenium.support.PageFactory;
2223

2324
import java.util.concurrent.TimeUnit;
@@ -26,6 +27,8 @@
2627
* A base for all the pages within the suite
2728
*/
2829
public abstract class BasePage {
30+
private static final int KEYBOARD_ANIMATION_DELAY = 1000;
31+
private static final int XML_REFRESH_DELAY = 1000;
2932

3033
/**
3134
* The driver
@@ -47,4 +50,35 @@ protected BasePage(AppiumDriver driver){
4750
this.driver = driver;
4851
PageFactory.initElements(new AppiumFieldDecorator(driver, 5, TimeUnit.SECONDS), this);
4952
}
53+
54+
/**
55+
* Tries three times to send text to element properly.
56+
*
57+
* Note: This method was needed because Appium sometimes sends text to textboxes incorrectly.
58+
*
59+
* @param input String to be sent
60+
* @param element WebElement to receive text, cannot be a secure text field.
61+
* @param appendNewLine true to append a new line character to incoming string when sending to element, else false
62+
*
63+
* @return true if keys were successfully sent, otherwise false.
64+
*/
65+
protected boolean sendKeysToElement(String input, WebElement element, boolean appendNewLine) throws InterruptedException {
66+
final int MAX_ATTEMPTS = 3;
67+
int attempts = 0;
68+
69+
do {
70+
element.clear();
71+
Thread.sleep(KEYBOARD_ANIMATION_DELAY);
72+
73+
if (appendNewLine) {
74+
element.sendKeys(input + "\n");
75+
} else {
76+
element.sendKeys(input);
77+
}
78+
79+
Thread.sleep(XML_REFRESH_DELAY);
80+
} while (!element.getText().contains(input) && ++attempts < MAX_ATTEMPTS);
81+
82+
return element.getText().contains(input);
83+
}
5084
}

src/test/java/Pages/CrashPage.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
* Copyright 2014-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
1818
import io.appium.java_client.AppiumDriver;
1919
import io.appium.java_client.pagefactory.AndroidFindBy;
2020
import org.openqa.selenium.WebElement;
21-
import org.testng.TestException;
2221

2322
/**
2423
* A page representing a crash
@@ -28,13 +27,13 @@ public class CrashPage extends BasePage {
2827
/**
2928
* The crash button
3029
*/
31-
@AndroidFindBy(id = "crash_button")
30+
@AndroidFindBy(name = "Crash Button")
3231
private WebElement crashButton;
3332

3433
/**
3534
* The crash message
3635
*/
37-
@AndroidFindBy(id = "bug_fragment_message")
36+
@AndroidFindBy(name = "Unfortunately, ReferenceApp has stopped.")
3837
private WebElement crashMessage;
3938

4039
public CrashPage(AppiumDriver driver) {

src/test/java/Pages/FixturesPage.java

+31-30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
* Copyright 2014-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
@@ -17,35 +17,13 @@
1717

1818
import io.appium.java_client.AppiumDriver;
1919
import io.appium.java_client.MobileElement;
20-
import io.appium.java_client.pagefactory.AndroidFindBy;
20+
21+
import java.util.List;
2122

2223
/**
2324
* A page representing the fixtures
2425
*/
2526
public class FixturesPage extends BasePage {
26-
/**
27-
* wifi value
28-
*/
29-
@AndroidFindBy(id = "wifi")
30-
private MobileElement wifi;
31-
32-
/**
33-
* bluetooth value
34-
*/
35-
@AndroidFindBy(id = "bluetooth")
36-
private MobileElement bluetooth;
37-
38-
/**
39-
* gps value
40-
*/
41-
@AndroidFindBy(id = "gps")
42-
private MobileElement gps;
43-
44-
/**
45-
* nfc value
46-
*/
47-
@AndroidFindBy(id = "nfc")
48-
private MobileElement nfc;
4927

5028
public FixturesPage(AppiumDriver driver) {
5129
super(driver);
@@ -56,30 +34,53 @@ public FixturesPage(AppiumDriver driver) {
5634
* @return wifi status
5735
*/
5836
public String getWifi() {
59-
return wifi.getText();
37+
return getStatus("Wifi:");
6038
}
6139

6240
/**
6341
*
6442
* @return bluetooth status
6543
*/
6644
public String getBluetooth() {
67-
return bluetooth.getText();
45+
return getStatus("Bluetooth:");
6846
}
6947

7048
/**
7149
*
72-
* @return gps statsu
50+
* @return gps status
7351
*/
7452
public String getGps() {
75-
return gps.getText();
53+
return getStatus("GPS:");
7654
}
7755

7856
/**
7957
*
8058
* @return nfc status
8159
*/
8260
public String getNfc() {
83-
return nfc.getText();
61+
return getStatus("NFC:");
62+
}
63+
64+
/**
65+
* Helper function to retrieve the status of inputted radio name.
66+
*
67+
* @param radioName should be followed by a colon, for example: "NFC:"
68+
* @return status of radio signal as a String
69+
*/
70+
private String getStatus(String radioName) {
71+
driver.scrollTo(radioName);
72+
List textViews = driver.findElementsByClassName("android.widget.TextView");
73+
74+
int idx;
75+
76+
// Retrieve index of desired radio text view
77+
for (idx = textViews.size() - 1; idx >= 0; idx--) {
78+
if (((MobileElement) textViews.get(idx)).getText().equals(radioName)) {
79+
break;
80+
}
81+
}
82+
83+
// Desired radio status comes immediately after its name. For example: NFC: true
84+
return ((MobileElement) textViews.get(idx + 1)).getText();
8485
}
8586
}

src/test/java/Pages/HomePage.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
* Copyright 2014-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
@@ -27,13 +27,13 @@ public class HomePage extends BasePage{
2727
/**
2828
* The headline of the homepage
2929
*/
30-
@AndroidFindBy(id = "homepage_headline")
30+
@AndroidFindBy(name = "Homepage Headline")
3131
private WebElement headline;
3232

3333
/**
3434
* the subheader of the homepage
3535
*/
36-
@AndroidFindBy(id = "homepage_subheadline")
36+
@AndroidFindBy(id = "Homepage Subheader")
3737
private WebElement subheader;
3838

3939
public HomePage(AppiumDriver driver) {

src/test/java/Pages/Inputs/CheckBoxPage.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
* Copyright 2014-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
@@ -27,13 +27,13 @@ public class CheckBoxPage extends BasePage {
2727
/**
2828
* The checkbox control
2929
*/
30-
@AndroidFindBy(id = "input_checkbox")
30+
@AndroidFindBy(name = "Checkbox Control")
3131
private MobileElement checkBox;
3232

3333
/**
3434
* The display for the status
3535
*/
36-
@AndroidFindBy(id = "input_checkbox_status")
36+
@AndroidFindBy(name = "Checkbox Display")
3737
private MobileElement display;
3838

3939

src/test/java/Pages/Inputs/EditTextPage.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
* Copyright 2014-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
@@ -27,7 +27,7 @@ public class EditTextPage extends BasePage {
2727
/**
2828
* The text input
2929
*/
30-
@AndroidFindBy(id = "input_edit_text")
30+
@AndroidFindBy(name = "Text Input Control")
3131
private MobileElement textBox;
3232

3333
public EditTextPage(AppiumDriver driver) {
@@ -38,14 +38,11 @@ public EditTextPage(AppiumDriver driver) {
3838
* Input text
3939
*
4040
* @param text to input
41+
*
42+
* @return true, if text properly entered, else false.
4143
*/
42-
public void enterText(String text){
43-
textBox.sendKeys(text);
44-
try {
45-
Thread.sleep(1000);
46-
} catch (InterruptedException e) {
47-
e.printStackTrace();
48-
}
44+
public boolean enterText(String text) throws InterruptedException {
45+
return sendKeysToElement(text, textBox, false);
4946
}
5047

5148
/**

src/test/java/Pages/Inputs/GesturesPage.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
* Copyright 2014-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@
2020
import io.appium.java_client.MobileElement;
2121
import io.appium.java_client.SwipeElementDirection;
2222
import io.appium.java_client.TouchAction;
23-
import io.appium.java_client.android.AndroidElement;
2423
import io.appium.java_client.pagefactory.AndroidFindBy;
2524

2625
/**
@@ -30,13 +29,13 @@ public class GesturesPage extends BasePage {
3029
/**
3130
* A page that performs specific gestures
3231
*/
33-
@AndroidFindBy(id = "input_gesture_action_pad")
32+
@AndroidFindBy(name = "Gesture Action Pad")
3433
private MobileElement gestureBox;
3534

3635
/**
3736
* The displays which lists the performed gestures
3837
*/
39-
@AndroidFindBy(id = "input_gesture_content")
38+
@AndroidFindBy(name = "Gestures Display")
4039
private MobileElement display;
4140

4241

@@ -47,7 +46,7 @@ public GesturesPage(AppiumDriver driver) {
4746
/**
4847
* Performs a single press
4948
*/
50-
public void singlePress(){
49+
public void singlePress() {
5150
gestureBox.click();
5251
}
5352

@@ -63,7 +62,9 @@ public void longPress() {
6362
* performs a fling gesture
6463
*/
6564
public void flingGesture() {
66-
gestureBox.swipe(SwipeElementDirection.UP, 10, 10, 100);
65+
try {
66+
gestureBox.swipe(SwipeElementDirection.UP, 10, 10, 100);
67+
} catch (Exception e) {} // Catch "The swipe did not complete successfully" error
6768
}
6869

6970
/**

0 commit comments

Comments
 (0)