Appium is not recognize getId() method for Android

Hi Friends,

Appium Version: 1.3.5
Android Device: Nexus 5 , API 21.

I am New to Appium, I have updated my Appium to latest version (1.3.5) and I am able to select proper date from Date Picker in Android device through recording mode. Here is the recorded script below:

(JavascriptExecutor)wd.executeScript(“mobile: scrollTo”, new HashMap<String, String>() {{ put(“element”, wd.findElement(By.xpath("//android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.DatePicker[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.NumberPicker[3]")).getId()); }});

I have copied the above script and pasted in my script which I have written in Eclipse to check whether i am able to select date from Date Picker through this script. I made some small changes in the script. Here is the modified script below:

((RemoteWebDriver) wd).executeScript(“mobile: scrollTo”, new HashMap<String, String>() {{ put(“element”, wd.findElement(By.xpath("//android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.DatePicker[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.NumberPicker[3]")).getId()); }});

Here i am facing a problem about getId() method, when I am placing my mouse cursor on getId() method it is saying: The method getId() is undefined for the type WebElement.

:sob:
OLD Version

In Appium Old version I have written a script to pick date from Date Picker

List date = wd.findElements(By.className(“android.widget.NumberPicker”));
date.get(0).sendKeys(“Feb”);
wd.navigate().back();
date.get(1).sendKeys(“17”);
wd.navigate().back();
date.get(2).sendKeys(“1995”);
wd.navigate().back();

The above script is setting the required data in date picker, but when I am click on OK button in data picker screen, current date is displaying in “Date Of Birth” field.

So please someone help me.

Hi Friends,

I still don’t know why getId() doesn’t work…

:disappointed:

Appium Log:

info: [debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:“action”,“action”:“find”,“params”:{“strategy”:“xpath”,“selector”:"//android.view.View[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.widget.Button[1]",“context”:"",“multiple”:false}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: find
info: [debug] [BOOTSTRAP] [debug] Finding //android.view.View[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.widget.Button[1] using XPATH with the contextId: multiple: false

info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:7,“value”:"Could not find an element using supplied strategy. "}

info: [debug] Waited for 59546ms so far

info: [debug] Pushing command to appium work queue: [“find”,{“strategy”:“xpath”,“selector”:"//android.view.View[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.widget.Button[1]",“context”:"",“multiple”:false}]

info: [debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:“action”,“action”:“find”,“params”:{“strategy”:“xpath”,“selector”:"//android.view.View[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.widget.Button[1]",“context”:"",“multiple”:false}}

info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: find
info: [debug] [BOOTSTRAP] [debug] Finding //android.view.View[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.widget.Button[1] using XPATH with the contextId: multiple: false

info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:7,“value”:"Could not find an element using supplied strategy. "}

info: [debug] Condition unmet after 60133ms. Timing out.
info: [debug] Responding to client with error: {“status”:7,“value”:{“message”:“An element could not be located on the page using the given search parameters.”,“origValue”:"Could not find an element using supplied strategy. "},“sessionId”:“dc896916-c514-4761-aa6c-e2a7b4bc7886”}
info: <-- POST /wd/hub/session/dc896916-c514-4761-aa6c-e2a7b4bc7886/element 500 60140.242 ms - 230

Are you able to find element by this xpath? Are you sure this element has id?

Yes i am sure that i am getting element id.

This is code which was generated while recording mode in Appium Inspector: (Appium Version :1.3.5)

(JavascriptExecutor)wd.executeScript(“mobile: scrollTo”, new HashMap() {{ put(“element”, wd.findElement(By.xpath("//android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.DatePicker[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.NumberPicker[3]")).getId()); }});

Does

wd.findElement(By.xpath("//android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.DatePicker[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.NumberPicker[3]")

return element?

The above code is generated when we are scrolling the date picker to select a year.

The code above is very unstable. In fact, using generated code and xPath like this one above is bad practice.

Kirill,
As I am new to appium, so I am recording the test and using the generated code from Appium Inspector and trying to run in eclipse by connecting the real device. At that moment getId() is returning error “The method getId() is undefined for the type WebElement”.

That’s strange. getId() should return id of WebElement object, right? I will try to set up Appium with Java environments and test this function. It should for any WebElement object as long as it is not null (in your case it is not, otherwise you’d get NullPointerException).

Hi Bhaskar, I am also new to Appium, Is there a way to record our test cases aswell? Can you please show some pointer on it?
Is it is same for both iOS and Andriod?

WebElement does not have getId() method. RemoteWebElement does. Try to cast your element to RemoteWebElement.

(JavascriptExecutor)wd.executeScript("mobile: scrollTo", new HashMap() {{ put("element", (RemoteWebElement) wd.findElement(By.xpath("//android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.DatePicker[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.NumberPicker[3]")).getId()); }});

Hi Kirill,

Thanks for the support, I have tried the code which you have given to me. But I am getting an error at run time.

((RemoteWebDriver) wd).executeScript(“mobile: scrollTo”, new HashMap() {{ put(“element”,((RemoteWebElement) wd.findElement(By.xpath("//android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.DatePicker[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.NumberPicker[3]"))).getId()); }});

Appium Error:

info: [debug] Responding to client with success: {“status”:0,“value”:{“ELEMENT”:“3”},“sessionId”:“0bb10fe2-b796-42d1-bc70-2f579ef82d17”}

info: <-- POST /wd/hub/session/0bb10fe2-b796-42d1-bc70-2f579ef82d17/element 200 780.320 ms - 87 {“status”:0,“value”:{“ELEMENT”:“3”},“sessionId”:“0bb10fe2-b796-42d1-bc70-2f579ef82d17”}

info: --> POST /wd/hub/session/0bb10fe2-b796-42d1-bc70-2f579ef82d17/execute {“script”:“mobile: scrollTo”,“args”:[{“element”:“3”}]}

warn: scrollTo will be removed in a future version of appium

info: [debug] Pushing command to appium work queue: [“element:scrollTo”,{“text”:null,“direction”:“vertical”,“elementId”:“3”}]

info: [debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:“action”,“action”:“element:scrollTo”,“params”:{“text”:null,“direction”:“vertical”,“elementId”:“3”}}

info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION

info: [debug] [BOOTSTRAP] [debug] Got command action: scrollTo

info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:13,“value”:“Could not scroll element into view: null”}

info: [debug] Responding to client with error: {“status”:13,“value”:{“message”:“An unknown server-side error occurred while processing the command.”,“origValue”:“Could not scroll element into view: null”},“sessionId”:“0bb10fe2-b796-42d1-bc70-2f579ef82d17”}

info: <-- POST /wd/hub/session/0bb10fe2-b796-42d1-bc70-2f579ef82d17/execute 500 4816.007 ms - 209

info: --> GET /wd/hub/status {}

info: [debug] Responding to client with success: {“status”:0,“value”:{“build”:{“version”:“1.3.5”,“revision”:“a124a15677e26b33db16e81c4b3b34d9c6b8cac9”}},“sessionId”:“0bb10fe2-b796-42d1-bc70-2f579ef82d17”}

info: <-- GET /wd/hub/status 200 2.601 ms - 155 {“status”:0,“value”:{“build”:{“version”:“1.3.5”,“revision”:“a124a15677e26b33db16e81c4b3b34d9c6b8cac9”}},“sessionId”:“0bb10fe2-b796-42d1-bc70-2f579ef82d17”}

And in eclipse I am getting this warning as

Multiple markers at this line

  • Type safety: The method put(Object, Object) belongs to the raw type HashMap. References to generic type HashMap<K,V> should be

parameterized

  • HashMap is a raw type. References to generic type HashMap<K,V> should be parameterized.

Android Screens:


iOS screens:

I am unable to set date for iOS device also, Please help me…

Here is the screen shot of the Date of Birth field.

Since HashMap is generic collection, you have to provide type parameters, like that:

new HashMap<String, RemoteWebElement>() {{ put("element",((RemoteWebElement) wd.findElement(By.xpath("//android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.DatePicker[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.NumberPicker[3]"))).getId()); }};

I can’t help much without apk at least. I am trying DatePicker from API 19, but yours is different. Seems like it is custom view. Can’t really tell exactly…

Hi kirill, This is error i am getting while using the code which you have give to me. I am able to see that the year is scrolling but immediately it is showing an error message.

Code:
((AndroidDriver) wd).executeScript(“mobile: scrollTo”, new HashMap<String, String>() {{ put(“element”,((RemoteWebElement) wd.findElement(By.xpath("//android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.DatePicker[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.NumberPicker[3]"))).getId()); }});

Appium Log:

info: --> POST /wd/hub/session/b838e563-7ead-42de-9bfe-8e40ede6bf08/execute {“script”:“mobile: scrollTo”,“args”:[{“element”:“3”}]}

warn: scrollTo will be removed in a future version of appium
info: [debug] Pushing command to appium work queue: [“element:scrollTo”,{“text”:null,“direction”:“vertical”,“elementId”:“3”}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:“action”,“action”:“element:scrollTo”,“params”:{“text”:null,“direction”:“vertical”,“elementId”:“3”}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: scrollTo

info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:13,“value”:“Could not scroll element into view: null”}

info: [debug] Responding to client with error: {“status”:13,“value”:{“message”:“An unknown server-side error occurred while processing the command.”,“origValue”:“Could not scroll element into view: null”},“sessionId”:“b838e563-7ead-42de-9bfe-8e40ede6bf08”}
info: <-- POST /wd/hub/session/b838e563-7ead-42de-9bfe-8e40ede6bf08/execute 500 4363.167 ms - 209

WebElement interface does not have ID field also , getter method for it.
You can do casting MobileElement element = (MobileElement) driver.findElementBy(By.something)
MobileElement has ID field and getter method for it.