appium / java-client

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

is @AndroidFindBy not working? #92

Closed liqing380 closed 10 years ago

liqing380 commented 10 years ago

Env: java-client:1.6.1 Appium:v1.2.0

1:Create class PageObjects

public class PageObjects {
    protected AppiumDriver driver;
    protected CmSelenium2 se2;
    protected CmReport cm;

    public PageObjects(AppiumDriver driver, CmSelenium2 se2) {
        this.se2 = se2;
        this.cm = se2.cm;
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }
    public PageObjects() {

    }
    public void initElements(AppiumDriver driver ) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }
}

2:Create class FolderPage:

import com.mailtech.selenium2.CmSelenium2;
import com.mailtech.selenium2.pageobjects.PageObjects;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import  io.appium.java_client.pagefactory.AndroidFindBy;

public class FolderPage extends PageObjects {
    @AndroidFindBy(id="cn.mailtech.app:id/compose")
    public WebElement toMAIl;

    public FolderPage(WebDriver driver, CmSelenium2 se2) {
        super(driver,se2);
    }
}

3. my test code:

FolderPage folderPage=new FolderPage(driver,se2);
            folderPage.toMAIl.click();

error

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 231 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'jli', ip: '192.168.200.85', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_39'
Session ID: a69b18bd-9190-4de1-b5fe-09b0efa2800d
Driver info: io.appium.java_client.AppiumDriver
Capabilities [{platform=LINUX, javascriptEnabled=true, browserName=, appActivity=cn.mailtech.app.activity.SplashMailtech, desired={newCommandTimeout=100, platformVersion=4.4, deviceName=Android Emulator, platformName=Android, browserName=, appActivity=cn.mailtech.app.activity.SplashMailtech, appPackage=cn.mailtech.app}, appPackage=cn.mailtech.app, newCommandTimeout=100, platformVersion=4.4, databaseEnabled=false, deviceName=Android Emulator, platformName=Android, webStorageEnabled=false, warnings={}, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:95)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByName(RemoteWebDriver.java:425)
    at org.openqa.selenium.By$ByName.findElement(By.java:299)
    at org.openqa.selenium.support.ByIdOrName.findElement(ByIdOrName.java:49)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344)
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:59)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:34)
    at sun.proxy.$Proxy9.click(Unknown Source)
    at com.mailtech.selenium2.example.appium.cmappTest.Cal.CalTest(Cal.java:115)
    at com.mailtech.selenium2.example.appium.cmappTest.Cal.testCal(Cal.java:131)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Process finished with exit code -1

My question: why is @AndroidFindBy not working?

when i use @FindBy ,it is ok!

@FindBy(id="cn.mailtech.app:id/compose")
    public WebElement toMAIl;
TikhomirovSergey commented 10 years ago

https://github.com/appium/java-client/issues/73

TikhomirovSergey commented 10 years ago

Please read this: https://github.com/appium/java-client/pull/68 https://groups.google.com/forum/#!topic/appium-discuss/dXD2bUYW6ag

TikhomirovSergey commented 10 years ago
PageFactory.initElements(driver, this); 

This invocation uses DefaultFieldDecorator (https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/support/pagefactory/DefaultFieldDecorator.java). It doesn't decorate fields marked by @AndroidFindBy or @iOSFindBy.

Please try this:

PageFactory.initElements(new AppiumFieldDecorator(driver), 
pageObject //an instance of PageObject.class
);
Anand093 commented 6 years ago

Getting Null Pointer Exception on line (String company = companyName.getText();) even though i am using pagefactory Problem with when i use (MobileElement companyName).

plz provide some solution, its blocker for me.

public LoginPage(AndroidDriver driver) { this.driver = driver; PageFactory.initElements(new AppiumFieldDecorator(driver, 20,TimeUnit.SECONDS), LoginPage.class); }

@AndroidFindBy(id ="com.octanner.android.performance:id/identityEditText")
 MobileElement companyName;

@AndroidFindBy(id ="com.octanner.android.performance:id/next_page_button")
@CacheLookup
MobileElement nextButton;

public void companyLogin(TestDataReader dataReader){
    try{
        companyName.sendKeys("xyz");

    }catch(Exception e){
        e.printStackTrace();
    }
}
vikramvi commented 6 years ago

@Anand093 can you open new issue instead of commenting on closed one ? This way your issue will get noticed and tracked properly

narendra-chandratre commented 6 years ago

@Anand093 Did you open or file any new issue in same regards? I am also facing NullPointer while accessing pageFactory elements. Pls share if you got any solution Thanks in advance!

Anand093 commented 6 years ago

@narendra-chandratre I figured it out

Before using @AndroidFindBy in page object class write below lines. Hope it will solve your problem, plz let me know if it solves ur issue

public PageObjectClassName(AndroidDriver driver) { this.driver = driver; PageFactory.initElements(new AppiumFieldDecorator(driver, 20,TimeUnit.SECONDS), this); }

Note-> I am using appium 1.6.4

narendra-chandratre commented 6 years ago

Still failing :( I am using Appium version 1.8.1, Java-Client version 6.1.0 On which versions its working for you? Can you share pls ?

Anand093 commented 6 years ago

@narendra-chandratre this is the pom dependencies & i am using appium 1.6.4

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-android-driver</artifactId>
        <version>2.39.0</version>
    </dependency>

    <dependency>
        <groupId>io.appium</groupId>
        <artifactId>java-client</artifactId>
        <version>5.0.4</version>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.6.0</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>3.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-remote-driver</artifactId>
        <version>3.6.0</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.14.3</version>
        <scope>test</scope>
    </dependency>
narendra-chandratre commented 6 years ago

Appium 1.6.4 and all above dependancies are you able to run Page Objects and Page Factory comfortably for Android & iOS?

Anand093 commented 6 years ago

@narendra-chandratre

Android, its working perfectly, ios i dint try

narendra-chandratre commented 6 years ago

Actually I am still trying to make it work but last hope is to downgrade the appium version :( Thanks a lot @Anand093 for your quick help.

Anand093 commented 6 years ago

@narendra-chandratre

Check with the 1.6.4 and mine pom dependencies, i think it should work, even i struggled with this alot, but finally it worked for me.

Below are few helps, if u can get anything out of it.

import statements

import io.appium.java_client.MobileElement; import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.pagefactory.AndroidFindBy; import io.appium.java_client.pagefactory.AppiumFieldDecorator;

Capablities static AndroidDriver androidDriver; public Capablities(AndroidDriver androidDriver){ Capablities.androidDriver = androidDriver; }

public static AndroidDriver getAppCapablities(String device, String ip){ try{ capabilities = new DesiredCapabilities(); capabilities.setCapability(DEVICE_NAME, device); capabilities.setCapability(CapabilityType.BROWSER_NAME, ANDROID); capabilities.setCapability(PLATFORM_NAME, ANDROID); capabilities.setCapability(CapabilityType.VERSION, VERSION_7_1_1); capabilities.setCapability(APP_PACKAGE, YOUR APP PKG); capabilities.setCapability(CLEAR_SYSTEM_FILES, true); capabilities.setCapability(APP_ACTIVITY, YOUR ACTIVITY);
capabilities.setCapability(CapabilityType.TAKES_SCREENSHOT, "true"); capabilities.setCapability(AUTO_ACCEPT_ALERT, true); capabilities.setCapability(AUTO_DISMISS_ALERT, true); capabilities.setCapability(ANDROID_EXPORTED, true); capabilities.setCapability(NO_RESET, "true"); capabilities.setCapability(FULL_RESET, "false"); capabilities.setCapability(NATIVE_WEB_TAP, true); // to hide android keyboard capabilities.setCapability(UNICODE_KEYBOARD, true); capabilities.setCapability(RESET_KEYBOARD, true); capabilities.setCapability(NEW_COMMAND_TIMEOUT, 120); capabilities.setCapability(SESSION_OVERRIDE, true); capabilities.setCapability(AVD, device);
androidDriver = new AndroidDriver(new URL(ip), capabilities); androidDriver.manage().timeouts().implicitlyWait(MAX_TIMEOUT, TimeUnit.SECONDS);

PageObject Class

public YourPageObjectClass(AndroidDriver driver) { this.driver = driver; PageFactory.initElements(new AppiumFieldDecorator(driver, 20,TimeUnit.SECONDS), this); util = new ActionUtil(driver); }

@AndroidFindBy(id = "")
@CacheLookup
MobileElement somButton;

Below is the test class from where you are trying to use page object class components.

driver = Capablities.getAppCapablities(device_name, IP2);
YourPageObjectClass= new YourPageObjectClass(driver);