Unable to locate elements in android app for login page


#1

Hi,

I am new to Appium automation. I am stuck right now with identifying the elements of login page. The app which I am testing is available in Google Play store “SmartView Quality”. When I am using command "System.out.println("name of context is “+ driver.getContextHandles());” , then I am getting output sometime as “name of context is [NATIVE_APP]”.

But sometime the same command give output as “name of context is [NATIVE_APP, WEBVIEW_chrome]”. So I am confused whether the app is native or hybrid.

Kindly help me with finding the elements of login page. I am using Appium Desktop 1.7.0 with Java client 6.1.0. I am testing it in real device “Samsung S7 Edge”

I tried to find the elements using Appium Inspector (Screenshot attached).

I am getting following locators:

id = Text1

class = android.widget.EditText

package = at.ondot.qmapp

But still unable to locate element using these locators

Here is my code:

public class SamsungOldQual

{

@Test

public void test_qual() throws InterruptedException {

AppiumDriver driver = null;

//AndroidDriver driver =null;

DesiredCapabilities caps = new DesiredCapabilities();

caps.setCapability(“deviceName”, “Phone”);

caps.setCapability(“udid”, “31603cdcf39*”);

caps.setCapability(“appPackage”, “at.ondot.qmapp”);

caps.setCapability(“appActivity”, “at.ondot.qmapp.MainActivity”);

caps.setCapability(“noReset”, “true”);

/*caps.setCapability(MobileCapabilityType.BROWSER_NAME, “Chrome”);

caps.setCapability(“autoWebview”, “true”);

caps.setCapability(“chromedriverExecutable”, “D:\Browser Drivers\chromedriver_win32_new\chromedriver.exe”);

*/

try {

driver = new AndroidDriver(new URL(“http://0.0.0.0:4723/wd/hub”), caps);

} catch (MalformedURLException e) {

System.out.println(e.getMessage());

}

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

System.out.println("Context count is "+driver.getContextHandles().size());

System.out.println("name of context is "+ driver.getContextHandles());

/*driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.findElementById(“at.ondot.qmapp:id/Password1”).sendKeys(“aaa”);

driver.context(“WEBVIEW_chrome”);*/

WebDriverWait wait = new WebDriverWait(driver, 10);

wait.until(ExpectedConditions.visibilityOfElementLocated(By.className(“android.widget.EditText”))).sendKeys(“abcd”);

//driver.findElement(By.id(“Text1”)).sendKeys(“yusuf”);

}

}


#2

I had some problems fiding webview elements from native app. For me i managed to find by xpath, but i dont think its the best solution


#3

Thanks!! I will now try to use Xpath for finding elements from page.


#4

Hi Weyler,
I tried again with same ID and it worked in Emulator. Don’t know why the same is not working in real device.


#5

that’s odd. The emulator and device have the same android versions?


#6

Both devices are on different android version. Real device is on android version 8.0.0 and emulator is on Android version 8.1.0. The odd behavior is I am unable to see the webview in real android device, so unable to access the element. But in emulator, I am able to see the webview and can access the element with their IDs. Don’t know why this strange behavior is happening. It might be due to different android version, but not sure.


#7

Can you debug the device and check what returns for the driver.Contexts? Check on the emulator too, perhaps it have different behaviors for them.
I had the same issue on android api lvl 19, where im unable to switch to alert context, but i can find with xpath.


#8

For real android device(8.0), driver.context mostly gives output as “NATIVE_APP” only and rarely gives “WEBVIEW_chrome”.
But emulator (8.1) always gives output as “NATIVE_APP, WEBVIEW_chrome, WEBVIEW_at.ondot.qmapp”

That’s why I guess, I am unable to locate element in real android device. I don’t know why the real android device is unable to locate webview. Do you know any resolution for it?


#9

Yeah, i guess theres something different between the versions, try to build an emulator on 8.0 to check if the behavior is the same as the device. Maybe something changed after 8.0.


#10

Hi Weyler,
Could you please help me with 1 issue which I am facing while using sendKeys to a userName text field.
I am able to access the element and can perform click on that web element. But I am unable to use sendKeys and getting error message as "An unknown server-side error occurred while processing the command. Original error: unknown error: cannot focus element"
I tried below methods:
WebElement ele=driver.findElement(By.id(“userName”));
ele.click();
Thread.sleep(3000);
ele.sendKeys(“yusuf”);

I also tried driver.findElement(By.id(“userName”)).sendKeys(“yusuf”);

But none of them worked.

I am working on Emulator with android version 8.1 , with chrome version= 61, Chrome driver version 2.33 and also 2.34, Appium 1.9.0 and Java Client 6.1.0.

Could you please help me how to resolve this issue?
Thanks


#11

Hi There!
the form you used driver.findElement(By.id(“userName”)).sendKeys(“yusuf”); is correct, you don’t need to click to fill the textbox. but is really strange that you can click but not sendkeys. Can you check if there is no focus on the textbox? sometimes if the keyboard is open appium cant execute some actions.
You can use driver.HideKeyboard() to move it


#12

Hi Weyler,
I tried with driver.hideKeyboard(); But it didn’t work. My userName text field is non-focusable with or without the keyboard.
With Keyboard:

Without keyboard

I think FOCUS=FALSE might be the reason sendKeys not working. If yes then how to resolve it?

Also 1 interesting which I notice is that after clicking somewhere else in the userName field focus changes to true. See below screenshot:

Kindly suggest how to rectify it so that my sendKeys() starts working.

Thanks!


#13

can you send the xml of your screen? As I can see from there, you have a view over your edittext. Maybe its some custom control used to make the placeholder.


#14

Hi Weyler,
Yes, there was View over Edit Text. That’s why focus was false, I guess so.
When I tried to access the element by:
driver.findElement(By.id(“userName”)).sendKeys(“yusuf”);
It didn’t work.

And when I tried to point my xpath to focus field, it worked fine
driver.findElementByXPath("//*[@id=‘userName’]/input").sendKeys(“check”);

Issue is resolved now. Thanks a lot for your help!


#15

Nice! Glad to hear that!
Any issues you can DM me :wink:


#16

Sure :slight_smile:
Thanks once again!!