Skip to content

Commit 8d451f8

Browse files
authored
Add element tests (#11)
* Implement RadioButton test * Cover CheckBox methods with test * Reused Attributes enum * Cover ComboBox methods and Element's findChildElement methods with tests * Cover Link methods and ElementFactory's findChildElement method with tests, refactoring * Add WebCheckbox and WebRadioButton tests * Refactored CheckBox and RadioButton elements * Replaced usage of isChecked() with super.isChecked() inside the toggle() method * Refactored tests and add AndroidActivity handling * Add waiting for isKeyboardShown result to WebTextBoxTest * Increased wait for keyboard shown timeout and reordered steps in textbox test
1 parent d8e76f4 commit 8d451f8

31 files changed

+572
-178
lines changed

src/main/java/aquality/appium/mobile/elements/Attributes.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package aquality.appium.mobile.elements;
22

33
public enum Attributes {
4+
CHECKED("checked"),
45
CLASS("class"),
56
DISABLED("disabled"),
67
HREF("href"),

src/main/java/aquality/appium/mobile/elements/CheckBox.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* The class describing the checkbox
99
*/
10-
public class CheckBox extends Element implements ICheckBox {
10+
public class CheckBox extends CheckableElement implements ICheckBox {
1111

1212
protected CheckBox(final By locator, final String name, final ElementState state) {
1313
super(locator, name, state);
@@ -29,12 +29,13 @@ public void uncheck() {
2929

3030
@Override
3131
public boolean isChecked() {
32-
return getState();
32+
logElementAction("loc.checkbox.get.state");
33+
return super.isChecked();
3334
}
3435

3536
@Override
3637
public void toggle() {
37-
setState(!isChecked());
38+
setState(!super.isChecked());
3839
}
3940

4041
/**
@@ -44,13 +45,8 @@ public void toggle() {
4445
*/
4546
private void setState(boolean state) {
4647
logElementAction("loc.setting.value", state);
47-
if (state != getState()) {
48+
if (state != super.isChecked()) {
4849
click();
4950
}
5051
}
51-
52-
private boolean getState() {
53-
logElementAction("loc.checkbox.get.state");
54-
return getElement().isSelected();
55-
}
5652
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package aquality.appium.mobile.elements;
2+
3+
import aquality.selenium.core.elements.ElementState;
4+
import org.openqa.selenium.By;
5+
6+
public abstract class CheckableElement extends Element {
7+
/**
8+
* The main constructor
9+
*
10+
* @param loc By Locator
11+
* @param nameOf Output in logs
12+
* @param stateOf desired ElementState
13+
*/
14+
protected CheckableElement(By loc, String nameOf, ElementState stateOf) {
15+
super(loc, nameOf, stateOf);
16+
}
17+
18+
public boolean isChecked() {
19+
return doWithRetry(() -> {
20+
String checked = getElement().getAttribute(Attributes.CHECKED.toString());
21+
if (checked == null || checked.equals("")) {
22+
return getElement().isSelected();
23+
}
24+
return checked.equals("true");
25+
});
26+
}
27+
}

src/main/java/aquality/appium/mobile/elements/Link.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ protected String getElementType() {
1919

2020
@Override
2121
public String getHref() {
22-
return getAttribute(Attributes.HREF.toString());
22+
return getAttribute(Attributes.HREF);
2323
}
2424
}

src/main/java/aquality/appium/mobile/elements/RadioButton.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Class describing the Radiobutton
99
*/
10-
public class RadioButton extends Element implements IRadioButton {
10+
public class RadioButton extends CheckableElement implements IRadioButton {
1111

1212
protected RadioButton(final By locator, final String name, final ElementState state) {
1313
super(locator, name, state);
@@ -16,9 +16,4 @@ protected RadioButton(final By locator, final String name, final ElementState st
1616
protected String getElementType() {
1717
return getLocalizationManager().getLocalizedMessage("loc.radio");
1818
}
19-
20-
@Override
21-
public boolean isChecked() {
22-
return doWithRetry(() -> getElement().isSelected());
23-
}
2419
}

src/main/java/aquality/appium/mobile/elements/TextBox.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void clearAndTypeSecret(final String value) {
5252

5353
@Override
5454
public String getValue() {
55-
return getAttribute(Attributes.VALUE.toString());
55+
return getAttribute(Attributes.VALUE);
5656
}
5757

5858
@Override

src/main/java/aquality/appium/mobile/elements/interfaces/IElement.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package aquality.appium.mobile.elements.interfaces;
22

3+
import aquality.appium.mobile.elements.Attributes;
34
import aquality.appium.mobile.elements.ElementType;
45
import aquality.selenium.core.elements.ElementState;
56
import io.appium.java_client.MobileElement;
@@ -51,7 +52,7 @@ default <T extends IElement> T findChildElement(By childLoc, ElementType element
5152
* @return found child element
5253
*/
5354
default <T extends IElement> T findChildElement(By childLoc, ElementType elementType) {
54-
return findChildElement(childLoc, null, elementType, ElementState.DISPLAYED);
55+
return findChildElement(childLoc, elementType, ElementState.DISPLAYED);
5556
}
5657

5758
/**
@@ -88,4 +89,14 @@ default MobileElement getElement() {
8889
*/
8990
@Override
9091
MobileElement getElement(Duration timeout);
92+
93+
/**
94+
* Gets attribute value of the element.
95+
*
96+
* @param attribute Attribute
97+
* @return Attribute value
98+
*/
99+
default String getAttribute(Attributes attribute) {
100+
return getAttribute(attribute.toString());
101+
}
91102
}

src/main/java/aquality/appium/mobile/elements/interfaces/IElementFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ default <T extends IElement> T findChildElement(IElement parentElement, By child
256256
* @return Child element.
257257
*/
258258
default <T extends IElement> T findChildElement(IElement parentElement, By childLoc, ElementType type) {
259-
return findChildElement(parentElement, childLoc, null, type, ElementState.EXISTS_IN_ANY_STATE);
259+
return findChildElement(parentElement, childLoc, type, ElementState.EXISTS_IN_ANY_STATE);
260260
}
261261

262262
/**

src/main/java/aquality/appium/mobile/screens/AndroidScreen.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import aquality.appium.mobile.application.AqualityServices;
44
import aquality.appium.mobile.application.PlatformName;
5+
import io.appium.java_client.android.Activity;
56
import io.appium.java_client.android.AndroidDriver;
67
import io.appium.java_client.android.AndroidElement;
78
import org.openqa.selenium.By;
@@ -22,4 +23,12 @@ protected AndroidDriver<AndroidElement> getDriver(){
2223
ensureApplicationPlatformCorrect(PlatformName.ANDROID);
2324
return (AndroidDriver<AndroidElement>) AqualityServices.getApplication().getDriver();
2425
}
26+
27+
protected void startActivity(Activity activity) {
28+
AqualityServices.getLogger().info(
29+
String.format("Starting the '%s' activity of the android app at package '%s'",
30+
activity.getAppActivity(),
31+
activity.getAppPackage()));
32+
getDriver().startActivity(activity);
33+
}
2534
}

src/main/resources/localization/be.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"loc.platform.name.wrong": "Платформа '%s' не падтрымліваецца.",
1616
"loc.application.ready": "Праграма на платформе '%1$s' гатовая...",
1717
"loc.application.implicit.timeout": "Задаем таймаўт implicit(няяўнага) чакання: '%1$s' сек.",
18+
"loc.application.android.activity.start": "Стартуем '%1$s' актыўнасць Android праграмы ў пакеце '%2$s'",
1819
"loc.button": "Кнопка",
1920
"loc.checkbox": "Чэкбокс",
2021
"loc.checkbox.get.state": "Атрымліваем стан",

0 commit comments

Comments
 (0)