2017-02-20 38 views
0

У меня возникла проблема с чтением списка контактов моего устройства Android с помощью appium. Моя цель - прочитать все контакты в Android-устройстве, прокручивая до конца страницы и выйти после достижения конца последнего контакта.Чтение контактов в моем устройстве Android с помощью appium путем прокрутки до конца списка всех контактов

Я использовал следующий фрагмент кода.

package sampleinfo; 

import java.util.ArrayList; 
import java.util.List; 

import org.openqa.selenium.Dimension; 
import io.appium.java_client.MobileElement; 

public class Readingcontacts extends sampleappiumlaunch{ 

    List<String>previous=new ArrayList<String>(); 
    List<String>current=new ArrayList<String>(); 

     public void scrolltillEnd(){ 

      System.out.println("Intiated method to read contacts of current window"); 
       readContacts(); 

       while(!current.equals(previous)){ 

        System.out.println("Intiated method to Scroll..."); 
        verticalScroll(); 

        System.out.println("adding current window contacts to previous window list"); 
        previous.addAll(current); 

        System.out.println("@@@@@@Previous List is of elements [email protected]@@@"+previous); 

        System.out.println("Intiated method to read contacts of current window"); 
        readContacts(); 

       } 

       System.out.println("****End of while loop..reached end of page..***"); 
       String finalcontact = current.get(current.size() - 1); 
       System.out.println("Final contact name is"+finalcontact); 

     } 

     public void readContacts(){ 
      String layout = "android.widget.ListView"; 
      MobileElement Rootelframe = (MobileElement) appiumdriver.findElementByAndroidUIAutomator(
        "new UiSelector().className(\"" + layout + "\").enabled(true).instance(0)"); 

      List<MobileElement>viewgroup=Rootelframe.findElementsByClassName("android.view.ViewGroup"); 
      for (int i=0;i<viewgroup.size();i++) { 

        try{ 
         System.out.println("####..currently in view group index of...####"+i); 
         String str=viewgroup.get(i).findElementByClassName("android.widget.TextView").getText().toString(); 
         System.out.println("@@@@@...Current List element [email protected]@@@"+str); 
         current.add(i, str); 
         } 

       catch(Exception e){ 
        System.out.println("currently Text view element is not visiable in view group index of"+i); 
       } 

      } 
      System.out.println("*****current List of elements were.....******"+current); 


     } 



     public void verticalScroll() {   
      Dimension size=appiumdriver.manage().window().getSize(); 

     int y_start=(int)(size.height*0.70); 
     int y_end=(int)(size.height*0.30); 
     int x=size.width/2; 

     if(!current.equals(previous)){ 
     appiumdriver.swipe(x,y_start,x,y_end,5000); 
     } 
     else{ 
     System.out.println("Reached end of page"); 
     } 
    } 
} 
+0

Я могу прокручивать до конца страницы, но не могу выйти из цикла while даже после достижения конца страницы. – ADITHYA

ответ

0

Условие, указанное вами, недействительно. Переменные текущие и предшествующие никогда не будут совпадать. Вы можете попробовать под кодом, установив флаг в другой части метода verticalScroll() и проверив тот же флаг в цикле while.

Объявить класс переменной boolean, называемый isPageEndReached = false. И установите значение true, если оно достигнет конца страницы. И проверьте значение в цикле, если его «истина» прерывает цикл.

 Смежные вопросы

  • Нет связанных вопросов^_^