在自动测试中滚动布局

时间:2014-01-31 20:30:49

标签: android python selenium appium

我使用python在Selenium appium中开发自动测试。 我需要滚动条目列表,但我不知道,怎么做。 我试试:

scrollLayout = android.driver.find_elements_by_class_name("android.widget.RelativeLayout")
params = {"element": scrollLayout[0].id, "text": SEARCH_STRING}
self.android.driver.execute_script("mobile: scrollTo", params)

但是,它不起作用。 我该怎么做?

2 个答案:

答案 0 :(得分:4)

WebElement list = driver.findElement(By.id("id of your list"));

     HashMap<String, String> scrollObject = new HashMap<String, String>();
     scrollObject.put("text", "name to search");
     scrollObject.put("element",( (RemoteWebElement) list).getId());
     driver.executeScript("mobile: scrollTo", scrollObject);
     if(scrollObject.containsValue("string to search"))
     {
        System.out.println("Found");
        List<WebElement> list_user = list.findElements(By.id("id of the text view"));
        for (WebElement component : list_user) {
           System.out.println(component.getText());
           if (component.getText().contains("string to search")) {
                component.click();
                break;
            } else {
                System.out.println("Not equal");
            }
     }
     }
     else
     {
         System.out.println("Not Found");
     }

答案 1 :(得分:1)

使用以下代码即可。这是java代码相应地改为python

            WebElement element = driver.findElement(By.id("android:id/select_dialog_listview"));
            HashMap<String, String> scrollObject = new HashMap<String, String>();
            scrollObject.put("text", "The text name you need to scroll to");
            scrollObject.put("element",( (RemoteWebElement) element).getId());
            driver.executeScript("mobile: scrollTo", scrollObject);