Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
String uiAutomator() default "";
String accessibility() default "";
String id() default "";
String name() default "";
@Deprecated
/**
* By.name selector is not supported by Appium server node since 1.5.x.
* So this option is going to be removed further. Be careful.
*/String name() default "";
String className() default "";
String tagName() default "";
String xpath() default "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package io.appium.java_client.pagefactory;

import static io.appium.java_client.remote.MobilePlatform.*;
import static io.appium.java_client.remote.AutomationName.*;

import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.HashMap;
Expand Down Expand Up @@ -117,8 +114,7 @@ protected By buildDefaultBy() {
@Override
protected By buildMobileNativeBy() {
AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated();
if (ANDROID.toUpperCase().equals(platform)
&& SELENDROID.toUpperCase().equals(automation)) {
if (isSelendroidAutomation()) {
SelendroidFindBy selendroidFindBy = annotatedElement.getAnnotation(SelendroidFindBy.class);
SelendroidFindBys selendroidFindBys = annotatedElement.getAnnotation(SelendroidFindBys.class);
SelendroidFindAll selendroidFindByAll = annotatedElement.getAnnotation(SelendroidFindAll.class);
Expand All @@ -136,7 +132,7 @@ protected By buildMobileNativeBy() {
}
}

if (ANDROID.toUpperCase().equals(platform)) {
if (isAndroid()) {
AndroidFindBy androidFindBy = annotatedElement.getAnnotation(AndroidFindBy.class);
AndroidFindBys androidFindBys= annotatedElement.getAnnotation(AndroidFindBys.class);
AndroidFindAll androidFindAll = annotatedElement.getAnnotation(AndroidFindAll.class);
Expand All @@ -154,7 +150,7 @@ protected By buildMobileNativeBy() {
}
}

if (IOS.toUpperCase().equals(platform)) {
if (isIOS()) {
iOSFindBy iOSFindBy = annotatedElement.getAnnotation(iOSFindBy.class);
iOSFindBys iOSFindBys= annotatedElement.getAnnotation(iOSFindBys.class);
iOSFindAll iOSFindAll = annotatedElement.getAnnotation(iOSFindAll.class);
Expand All @@ -181,25 +177,38 @@ public boolean isLookupCached() {
return (annotatedElement.getAnnotation(CacheLookup.class) != null);
}

private By returnMappedBy(By byDefault, By nativeAppBy) {
Map<ContentType, By> contentMap = new HashMap<>();
contentMap.put(ContentType.HTML_OR_DEFAULT, byDefault);
contentMap.put(ContentType.NATIVE_MOBILE_SPECIFIC, nativeAppBy);
return new ContentMappedBy(contentMap);
}

@Override
public By buildBy() {
assertValidAnnotations();

By defaultBy = buildDefaultBy();
By mobileNativeBy = buildMobileNativeBy();

if (defaultBy == null) {
String idOrName = ((Field) annotatedElementContainer.getAnnotated()).getName();

if (defaultBy == null && mobileNativeBy == null) {
defaultBy = new ByIdOrName(((Field) annotatedElementContainer.getAnnotated()).getName());
mobileNativeBy = new By.ById(idOrName);
return returnMappedBy(defaultBy, mobileNativeBy);
}

if (defaultBy == null) {
defaultBy = new ByIdOrName(((Field) annotatedElementContainer.getAnnotated()).getName());
return returnMappedBy(defaultBy, mobileNativeBy);
}

if (mobileNativeBy == null) {
mobileNativeBy = defaultBy;
return returnMappedBy(defaultBy, mobileNativeBy);
}

Map<ContentType, By> contentMap = new HashMap<>();
contentMap.put(ContentType.HTML_OR_DEFAULT, defaultBy);
contentMap.put(ContentType.NATIVE_MOBILE_SPECIFIC, mobileNativeBy);
return new ContentMappedBy(contentMap);
return returnMappedBy(defaultBy, mobileNativeBy);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.appium.java_client.pagefactory;

import org.openqa.selenium.InvalidSelectorException;
import org.openqa.selenium.StaleElementReferenceException;

import java.lang.reflect.InvocationTargetException;
Expand All @@ -28,7 +29,11 @@ static boolean isInvalidSelectorRootCause(Throwable e) {
return false;
}

if (String.valueOf(e.getMessage()).contains(INVALID_SELECTOR_PATTERN)) {
if (InvalidSelectorException.class.isAssignableFrom(e.getClass())) {
return true;
}

if (String.valueOf(e.getMessage()).contains(INVALID_SELECTOR_PATTERN) || String.valueOf(e.getMessage()).contains("Locator Strategy \\w+ is not supported")) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
import java.util.ArrayList;
import java.util.List;

import static io.appium.java_client.remote.AutomationName.SELENDROID;
import static io.appium.java_client.remote.MobilePlatform.ANDROID;
import static io.appium.java_client.remote.MobilePlatform.IOS;

/**
* It is the basic handler of Appium-specific page object annotations
* About the Page Object design pattern please read these documents:
Expand Down Expand Up @@ -163,6 +167,18 @@ public void setAnnotated(AnnotatedElement annotated) {
this.annotatedElementContainer.setAnnotated(annotated);
}

protected boolean isAndroid() {
return ANDROID.toUpperCase().equals(platform);
}

protected boolean isSelendroidAutomation() {
return isAndroid() && SELENDROID.toUpperCase().equals(automation);
}

protected boolean isIOS() {
return IOS.toUpperCase().equals(platform);
}

/**
* Defines how to transform given object (field, class, etc)
* into {@link org.openqa.selenium.By} class used by webdriver to locate elements.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
String uiAutomator() default "";
String accessibility() default "";
String id() default "";
String name() default "";
@Deprecated
/**
* By.name selector is not supported by Appium server node since 1.5.x.
* So this option is going to be removed further. Be careful.
*/String name() default "";
String className() default "";
String tagName() default "";
String xpath() default "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@
public class MobileBrowserCompatibilityTest {

private WebDriver driver;

@FindBy(name = "q")
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/someId\")")
private WebElement searchTextField;

private AppiumDriverLocalService service;

@AndroidFindBys({
@AndroidFindBy(className = "someClass"),
@AndroidFindBy(xpath = "//someTag")})
@FindBy(name="btnG")
private RemoteWebElement searchButton;

private RemoteWebElement btnG; //this element should be found by id = 'btnG' or name = 'btnG'

@FindBy(name = "q")
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/someId\")")
private WebElement searchTextField;

@AndroidFindBy(className = "someClass")
@FindBys({@FindBy(className = "r"), @FindBy(tagName = "a")})
private List<WebElement> foundLinks;
Expand Down Expand Up @@ -84,7 +84,7 @@ public void test() {
driver.get("https://www.google.com");

searchTextField.sendKeys("Hello");
searchButton.click();
btnG.click();
Assert.assertNotEquals(0, foundLinks.size());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.appium.java_client.pagefactory_tests;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.pagefactory.*;
import io.appium.java_client.remote.AutomationName;
import io.appium.java_client.remote.MobileCapabilityType;
Expand All @@ -37,7 +38,7 @@
import static org.junit.Assert.*;

public class SelendroidModeTest {
private static int SELENDROID_PORT = 9999;
private static int SELENDROID_PORT = 9999;

private static WebDriver driver;
private static AppiumDriverLocalService service;
Expand All @@ -63,30 +64,30 @@ public class SelendroidModeTest {
private WebElement textXpath;

@SelendroidFindBys({
@SelendroidFindBy(id = "text1")})
@SelendroidFindBy(id = "text1")})
private WebElement textIds;

@SelendroidFindAll({
@SelendroidFindBy(id = "text1")})
@SelendroidFindBy(id = "text1")})
private WebElement textAll;

@SelendroidFindAll({
@SelendroidFindBy(id = "text1")})
@SelendroidFindBy(id = "text1")})
private List<WebElement> textsAll;

@SelendroidFindBy(className = "android.widget.TextView")
private WebElement textClass;

@SelendroidFindBy(tagName = "TextView")
private WebElement textTag;

@SelendroidFindBy(linkText = "Accessibility")
private WebElement textLink;

@SelendroidFindBy(partialLinkText = "ccessibilit")
private WebElement textPartialLink;

@BeforeClass
@BeforeClass
public static void beforeClass() throws Exception {
AppiumServiceBuilder builder = new AppiumServiceBuilder().withArgument(GeneralServerFlag.AUTOMATION_NAME, AutomationName.SELENDROID);
service = builder.build();
Expand Down Expand Up @@ -123,8 +124,8 @@ public static void afterClass() throws Exception {
public void findByIdElementTest() {
assertNotEquals(null, textId.getAttribute("text"));
}
@Test

@Test
public void findBySelendroidSelectorTest() {
assertNotEquals(null, textSelendroidId.getAttribute("text"));
}
Expand Down Expand Up @@ -173,15 +174,15 @@ public void findByElementByCalssTest() {
public void findByElementByTagTest() {
assertNotEquals(null, textTag.getAttribute("text"));
}

@Test
public void findBySelendroidAnnotationOnlyTest() {
assertNotEquals(null, textSelendroidId.getAttribute("text"));
}

@Test
public void findBySelendroidLinkTextTest() {
assertEquals("Accessibility", textLink.getText());

}
}
}
Loading