How do i select value from dropdown of a hybrid app using appium?

馋奶兔 提交于 2020-03-03 12:17:28

问题


I am relatively new to appium, and is actively involved in a poc. The requirement is to select a value from the drop down, however i am able to click the spinner element but anything below that is not getting recognized my uiautomator. as a result i am not able to select any values from the spinner element. I am attaching the code block of the script and also the element tree snapshot.

 //trying to click the dropdown list
		 try{
			 WebElement parentElement1 = driver.findElement(By.id("retProdOp0"));
			 WebElement childElement1 = parentElement1.findElement(By
					.xpath("//android.view.View[@index='1']"));
			childElement1.click();
			driver.label("dropdown list 2nd element clicked");
		 }catch(Exception e){
			driver.label("Failed to click dropdown list on prodexchg screen");
			System.out.println(e.getMessage());
		 }
		 

Snapshot1

Snapshot2

I want to select the values from the drop down of the spinner in Snapshot2. However i am not able to locate them in uiautomator. Looking for some help. Thanks in advance.


回答1:


You are using incorrect XPath locator to select item in dropdown.

On your screenshot of uiautomator it is clear that before you click dropdown, items in it are not yet in DOM. So search in parent is not correct, as it does not contain elements at that moment.

  1. Use appium-desktop instead of UIAutomator for elements inspection, it works better
  2. Click dropdown to expand it
  3. To click one of items do it like:

List<WebElement> items = driver.findElements(By.xpath("//android.view.View")); items.get(0).click();

or

driver.findElement(By.xpath("(//android.view.View)[1])




回答2:


Tried appium-desktop, but facing the same issues. Appium-desktop screenshot 1 Appium-desktop screenshot 2

Please refer to the second screenshot. Thanks




回答3:


The below solution will help you. Let me know if it doesn't.

  1. From your object properties it seems that you are in the Native context right now. So before selecting the drop down item please change to Web Context.

driver.Context = "WebContext"; Or

driver.Context = "CHROMIUM";

The name of your webcontext can be different 2. Now select the element just as you would do in web using selenium webdriver

Select dropdown = new Select(driver.findElement(By.id("mySelect"))); dropdown.selectByVisibleText("Text");

  1. Now you can switch back to native context of your hybrid application if you want.

Note: To find the properties of webcontext you can fetch the source code or go to the web URL in your browser.




回答4:


If I understand correctly you have an application with a WebView showing a drop-down in HTML/JS.

I loaded https://www.w3schools.com/howto/howto_js_dropdown.asp as an example and generated a test using CulebraTester clicking on the button and then on "Link 3" item.

The generated script is

#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Copyright (C) 2013-2018  Diego Torres Milano
Created on 2018-04-11 by CulebraTester 
                      __    __    __    __
                     /  \  /  \  /  \  /  \ 
____________________/  __\/  __\/  __\/  __\_____________________________
___________________/  /__/  /__/  /__/  /________________________________
                   | / \   / \   / \   / \   \___
                   |/   \_/   \_/   \_/   \    o \ 
                                           \_____/--<
@author: Diego Torres Milano
@author: Jennifer E. Swofford (ascii art snake)
'''


import re
import sys
import os


import unittest
try:
    sys.path.insert(0, os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
except:
    pass

import pkg_resources
pkg_resources.require('androidviewclient>=12.4.0')
from com.dtmilano.android.viewclient import ViewClient, CulebraTestCase
from com.dtmilano.android.uiautomator.uiautomatorhelper import UiAutomatorHelper, UiScrollable, UiObject, UiObject2

TAG = 'CULEBRA'


class CulebraTests(CulebraTestCase):

    @classmethod
    def setUpClass(cls):
        cls.kwargs1 = {'ignoreversioncheck': False, 'verbose': False, 'ignoresecuredevice': False}
        cls.kwargs2 = {'forceviewserveruse': False, 'useuiautomatorhelper': True, 'ignoreuiautomatorkilled': True, 'autodump': False, 'startviewserver': True, 'compresseddump': True}
        cls.options = {'start-activity': None, 'concertina': False, 'device-art': None, 'use-jar': False, 'multi-device': False, 'unit-test-class': True, 'save-screenshot': None, 'use-dictionary': False, 'glare': False, 'dictionary-keys-from': 'id', 'scale': 1, 'find-views-with-content-description': True, 'window': -1, 'orientation-locked': None, 'save-view-screenshots': None, 'find-views-by-id': True, 'log-actions': False, 'use-regexps': False, 'null-back-end': False, 'auto-regexps': None, 'do-not-verify-screen-dump': True, 'verbose-comments': False, 'gui': False, 'find-views-with-text': True, 'prepend-to-sys-path': False, 'install-apk': None, 'drop-shadow': False, 'output': None, 'unit-test-method': None, 'interactive': False}
        cls.sleep = 5

    def setUp(self):
        super(CulebraTests, self).setUp()

    def tearDown(self):
        super(CulebraTests, self).tearDown()

    def preconditions(self):
        if not super(CulebraTests, self).preconditions():
            return False
        return True

    def testSomething(self):
        if not self.preconditions():
            self.fail('Preconditions failed')

        _s = CulebraTests.sleep
        _v = CulebraTests.verbose

        UiScrollable(self.vc.uiAutomatorHelper, uiSelector='clazz@android.webkit.WebView,index@0,parentIndex@0,package@com.android.chrome').getChildByText(uiSelector='text@Click Me', text="Click Me", allowScrollSearch=True).click()
        UiScrollable(self.vc.uiAutomatorHelper, uiSelector='clazz@android.webkit.WebView,index@0,parentIndex@0,package@com.android.chrome').getChildByText(uiSelector='text@Link 3', text="Link 3", allowScrollSearch=True).click()


if __name__ == '__main__':
    CulebraTests.main()

which seems to work correctly.

You can give it a try if you can't find other solution using appium.



来源:https://stackoverflow.com/questions/49758547/how-do-i-select-value-from-dropdown-of-a-hybrid-app-using-appium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!