appium / java-client

Java language binding for writing Appium Tests, conforms to W3C WebDriver Protocol
Apache License 2.0
1.2k stars 755 forks source link

iOSFindBy & AndroidFindBy annotations are ignored #211

Closed justintilson closed 9 years ago

justintilson commented 9 years ago

I've been trying for a couple of days to get iOSFindBy & AndroidFindBy annotations working with page objects and the PageFactory. I've tried versions 2.2.0 and 3.0.0 of the java-client and tests for Android and iOS, all with the same result - which is the same as not including any annotations.

Here is a truncated version of one of my page objects

public class HomePhonePage extends BaseHomePage {

    public HomePhonePage(AppiumDriver driver) {
        super(driver);

        PageFactory.initElements(new AppiumFieldDecorator(driver,
                        IMPLICIT_WAIT,
                        TimeUnit.SECONDS),
                this
        );
    }

    @iOSFindBy(accessibility = "home_landing_my_favorites_count_label")
    private WebElement myFavoritesCountLabel;

    public String getMyFavoritesCountLabel() {
        return myFavoritesCountLabel.getText();
    }
}

My test code:

    @Test
    public void testHomeIsValid() {

        homePhonePage = new HomePhonePage(driver);
        globalNavPage = new GlobalNavigationPage(driver);

        homePhonePage.clickHamburgerMenu(); //FAILS HERE because element was not found
        globalNavPage.clickHome();

    }

When I debug into PageFactory.initElements, I see [unknown locator] for each property that is a WebElement. Then the PageFactory defaults to looking for each page object's property name as the id or name of the WebElement and fails to find anything.

unknown-locator2

I suspect "I'm doing it wrong" but I can't figure out where. It is behaving as if the annotations are not even there.

Simon-Kaz commented 9 years ago

Hey, could you post your capabilities as well? I've been using page factory extensively and haven't encountered this issue.

justintilson commented 9 years ago
System.out.println(driver.getCapabilities().toString());

outputs for an Android test:

Capabilities [{networkConnectionEnabled=true, desired={}, platformVersion=4.4.4, warnings={}, webStorageEnabled=false, locationContextEnabled=false, browserName=Android, takesScreenshot=true, javascriptEnabled=true, databaseEnabled=false, deviceName=192.168.56.101:5555, platform=LINUX}]

and for an iOS test:

Capabilities [{networkConnectionEnabled=false, desired={}, warnings={}, webStorageEnabled=false, locationContextEnabled=false, browserName=iOS, takesScreenshot=true, javascriptEnabled=true, databaseEnabled=false, platform=MAC}]

I also dug a bit deeper into the debugger for the particular field I've included above as it's being decorated by the PageFactory and it shows no annotations (I'm not sure if it should):

no-annotations

TikhomirovSergey commented 9 years ago

@justintilson Hi! Is it native app? Maybe hybrid. Maybe you are trying to run test with mobile browser?

TikhomirovSergey commented 9 years ago

Capabilities [{networkConnectionEnabled=true, desired={}, platformVersion=4.4.4, warnings={}, webStorageEnabled=false, locationContextEnabled=false, browserName=Android, takesScreenshot=true, javascriptEnabled=true, databaseEnabled=false, deviceName=192.168.56.101:5555, platform=LINUX}]

Are you tryng to use iOSFindBy for Android

@iOSFindBy(accessibility = "home_landing_my_favorites_count_label")

?

Please improve your page object as follows

@iOSFindBy(accessibility = "home_landing_my_favorites_count_label")
@AndroidFindBy(your locator for android)
    private WebElement myFavoritesCountLabel;

What the result?

justintilson commented 9 years ago

@TikhomirovSergey, I'm testing native apps on Android and iOS.

I have separate tests and page objects for Android and iOS and use the appropriate @FindBy in each.

justintilson commented 9 years ago

Here are the desired capabilities that are passed into the Android driver:

dcs-passed-to-android-driver

and for the iOS

dcs-passed-to-ios-driver

justintilson commented 9 years ago

And if it helps:

appium-android-settings

appium-ios-settings

TikhomirovSergey commented 9 years ago

Ok. If I clearly understand you

Appuim features allow to implement one page object for Android/iOS/browser testing. Please look at this

@iOSFindBy(locator_for_ios) //this locater is used when here is iOS native content
@AndroidFindBy(locator_for_android)//this locator is used when here 
//is Android native content
@FindBy(locator_for_HTML)//this locator is used when here mobile 
//or desktop browser or webview
 private WebElement myFavoritesCountLabel;

Ok. Is this used with Android

public class HomePhonePage extends BaseHomePage {

    public HomePhonePage(AppiumDriver driver) {
        super(driver);

        PageFactory.initElements(new AppiumFieldDecorator(driver,
                        IMPLICIT_WAIT,
                        TimeUnit.SECONDS),
                this
        );
    }

    @iOSFindBy(accessibility = "home_landing_my_favorites_count_label")
    private WebElement myFavoritesCountLabel;

    public String getMyFavoritesCountLabel() {
        return myFavoritesCountLabel.getText();
    }
}

?

Capabilities [{networkConnectionEnabled=true, desired={}, platformVersion=4.4.4, warnings={}, webStorageEnabled=false, locationContextEnabled=false, browserName=Android, takesScreenshot=true, javascriptEnabled=true, databaseEnabled=false, deviceName=192.168.56.101:5555, platform=LINUX}]

TikhomirovSergey commented 9 years ago

...I am just trying to understand your problem and usecase :)

justintilson commented 9 years ago

@TikhomirovSergey - I understand that I can use a single page object to test both platforms but I've found in this particular instance it didn't work out well because the implementations are somewhat different and evolving at different rates.

Here is the Android page object (same as iOS in this instance but in a different package):

public class HomePhonePage extends BaseHomePage {

    public HomePhonePage(AppiumDriver driver) {
        super(driver);

        PageFactory.initElements(new AppiumFieldDecorator(driver,
                        IMPLICIT_WAIT,
                        TimeUnit.SECONDS),
                this
        );
    }

    @AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"com.wholefoods.wholefoodsmarket:id/tvFavOrangeBubble\")")
    private MobileElement myFavoritesCountLabel;

    public String getMyFavoritesCountLabel() {
        return myFavoritesCountLabel.getText();
    }
}
TikhomirovSergey commented 9 years ago

ok. Now it is clear. Is your app hybrid? Appium-specific annotations were designed for native contents. What does return .getContext() method before your test is failed?

justintilson commented 9 years ago

System.out.println(driver.getContext());

prints: NATIVE_APP

TikhomirovSergey commented 9 years ago

Ok. Is it possible that your project is still depending on java_client 2.2.0 or lower? Please look at your dependencies hierarchy.

justintilson commented 9 years ago

I thought that might have had something to do with it so I upgraded yesterday. Here is my build.gradle file:

dependencies {
    testCompile 'org.testng:testng:6.8.21'
    testCompile 'org.seleniumhq.selenium:selenium-java:2.46.0'
    testCompile 'io.appium:java-client:3.0.0'
    testCompile 'com.google.code.gson:gson:2.3.1'
    testCompile 'org.json:json:20140107'
    testCompile 'org.apache.httpcomponents:httpclient:4.3.6'
    testCompile 'net.sf.opencsv:opencsv:2.3'
}

and the gradle cache:

gradle-cache

TikhomirovSergey commented 9 years ago

androidcaps

And when you are running

System.out.println(driver.getCapabilities().toString());

it is printing

Capabilities [{networkConnectionEnabled=true, desired={}, platformVersion=4.4.4, warnings={}, webStorageEnabled=false, locationContextEnabled=false, browserName=Android, takesScreenshot=true, javascriptEnabled=true, databaseEnabled=false, deviceName=192.168.56.101:5555, platform=LINUX}]

Am I right? It interesting where is platformName = "Android".

TikhomirovSergey commented 9 years ago

Ok. I have two things to say. The first it is my fault that I forgot to improve this: https://github.com/appium/java-client/blob/master/src/main/java/io/appium/java_client/pagefactory/AppiumElementLocator.java#L74 It is outdated thing from times when there weren't AndroidDriver/IOSFriver (there was only AppiumDriver). I can change it and propese PR soon.

The second. If I am right then there is the strange difference between given and printed capabilities. The absence of "platformName" parameter causes your problem.

justintilson commented 9 years ago

I changed the 'platformName' desired capability to 'platform' with no difference and then tried both platform and platformName with the same result.

Here is the DesiredCapabilities instance that is passed to the AndroidDriver's constructor:

dcs-passed-to-android-driver3

and this is how they read after the driver has been instantiated:

dcs-read-from-driver-after-instantiation

I'm not sure where LINUX is coming from but it's not something I seem to be able to control.

justintilson commented 9 years ago

I tried switching to node server (instead of Appium.app) and got the same results. It's still trying to use the property name of the page object to find elements instead of using the values provided in the annotations.

TikhomirovSergey commented 9 years ago

@justintilson The PR is proposed and now it is waiting for the merging.

justintilson commented 9 years ago

@TikhomirovSergey - thanks for the update and your speedy fix. How long is typical for a merge? Our tiny team of two is somewhat blocked by this issue.

TikhomirovSergey commented 9 years ago

It is depending on @Jonahss

Jonahss commented 9 years ago

@justintilson merged! Are you ok compiling on your own from this repo? Or would you prefer we do a full release to the Maven Central Repository?

justintilson commented 9 years ago

@Jonahss - our current gradle build script pulls from the Maven repo. I could probably tweak it to download the source and compile it if you weren't planning to do a release for a while. If it's relatively effortless to do a release, that would accelerate things on my side.

Jonahss commented 9 years ago

@justintilson Yeah I can do a release :) It's relatively effortless compared to gardening in my backyard. But is an incredible amount of effort compared to using npm :P

I'll start running the test suite now.

Jonahss commented 9 years ago

@justintilson Ok, released version 3.1.0

justintilson commented 9 years ago

It works! I tested on both Android and iOS. @TikhomirovSergey, @Jonahss Thanks so much for moving this issue through so quickly!

Jonahss commented 9 years ago

Yay!

On Mon, Jul 6, 2015 at 3:57 PM, Justin Tilson notifications@github.com wrote:

It works! I tested on both Android and iOS. @TikhomirovSergey https://github.com/TikhomirovSergey, @Jonahss https://github.com/Jonahss Thanks so much for moving this issue through so quickly!

— Reply to this email directly or view it on GitHub https://github.com/appium/java-client/issues/211#issuecomment-119022425.

TikhomirovSergey commented 9 years ago

I'm happy :)

umer-ali-khan commented 8 years ago

one question. Do I need to change the context of the driver before I try to locate webview elements (hybrid app - like FB login) by using Page Objects/Factory as mentioned above?

FindBy(locator_for_HTML)//this locator is used when here mobile //or desktop browser or webview private WebElement myFavoritesCountLabel;