Search code examples
javaselenium-webdriverappiumappium-android

How To Fetch And Store List Data In Appium And How To Click On Particular Searched Record


enter image description here
I am trying to fetch and store the list of Year data in list variable but unable to store all data.

And at the same time I also want to search for a particular year and if the year will not find then I want to scroll and search again on the list.

I am also attaching the screenshot of my elements in application.

This is my code for store and search list of record in the list variable

public void fetchVehicleListAndClickOnYear(String year) {

        java.util.List<MobileElement> vehicleList =driver.findElementsById(packageName+":id/title"); 

        for (int i=0;i<= vehicleList.size();i++) {
            String actuallist = vehicleList.get(i).getText();
            System.out.println("Print vehicle year list " +actuallist);
            if(actuallist.equals(year)) {
                driver.findElementByName(year).click();
            } else {
                Utils.scrollDown(driver);
            }
        }
    }

    public void fillVehicleInfoOnSignup(String Vehicle_Year) {

        vehicleYearTextBox.click();
        Utils.PauseTestExecution(2);
        fetchVehicleListAndClickOnYear(Vehicle_Year);

    }



For scrolling code 

public static void scrollDown(AndroidDriver<MobileElement> driver) {
            //if pressX was zero it didn't work for me
            int pressX = driver.manage().window().getSize().width / 2;
            // 4/5 of the screen as the bottom finger-press point
            int bottomY = driver.manage().window().getSize().height * 4/5;
            // just non zero point, as it didn't scroll to zero normally
            int topY = driver.manage().window().getSize().height / 8;
            //scroll with TouchAction by itself
            scroll(pressX, bottomY, pressX, topY,driver);
        }

        public static void scroll(int fromX, int fromY, int toX, int toY,AndroidDriver<MobileElement> driver) {
            TouchAction touchAction = new TouchAction(driver);
            new TouchAction(driver).press(PointOption.point(fromX, fromY)).waitAction().moveTo(PointOption.point(toX, toY)).release().perform();
        }

Error showing

WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by net.sf.cglib.core.ReflectUtils$1 (file:/Users/daffolapmac-73/eclipse-workspace/Wapanda_Driver_Automation/libfiles/cglib-3.2.8.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) WARNING: Please consider reporting this to the maintainers of net.sf.cglib.core.ReflectUtils$1 WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release Print vehicle year list 2018 Print vehicle year list 2000 Print vehicle year list 1984 Print vehicle year list 1967 Print vehicle year list 1952 Print vehicle year list 1942 Print vehicle year list 1941 java.lang.IndexOutOfBoundsException: Index 7 out-of-bounds for length 7 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) at java.base/java.util.Objects.checkIndex(Objects.java:372) at java.base/java.util.ArrayList.get(ArrayList.java:440) at com.wapanda.pages.DriverSignUpPages.fetchVehicleListAndClickOnYear(DriverSignUpPages.java:261) at com.wapanda.pages.DriverSignUpPages.fillVehicleInfoOnSignup(DriverSignUpPages.java:275) at com.wapanda.tests.ValidateDriverProcess.validateDriverSignupProcess(ValidateDriverProcess.java:108) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124) at org.testng.internal.Invoker.invokeMethod(Invoker.java:580) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:648) at org.testng.TestRunner.run(TestRunner.java:505) at org.testng.SuiteRunner.runTest(SuiteRunner.java:455) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) at org.testng.SuiteRunner.run(SuiteRunner.java:364) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208) at org.testng.TestNG.runSuitesLocally(TestNG.java:1137) at org.testng.TestNG.runSuites(TestNG.java:1049) at org.testng.TestNG.run(TestNG.java:1017) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


Solution

  • Error says "java.lang.IndexOutOfBoundsException: Index 7 out-of-bounds for length 7".To fix it correct the for loop as following ("<" insdead of "<="). The code would look as following:

    for (int i=0;i<vehicleList.size();i++) {
                String actuallist = vehicleList.get(i).getText();
    

    https://docs.oracle.com/javase/7/docs/api/java/lang/IndexOutOfBoundsException.html

    Modified code to return list of displayed year. (note: code potentially could be optimized to have a single loop. As the scrolling logic is not clear I kept it as is)

    public List<String> fetchVehicleListAndClickOnYear(String year) {
    
        java.util.List<MobileElement> vehicleList =driver.findElementsById(packageName+":id/title");
    
        java.util.List<String> displayedYears = new ArrayList<>();
        //logic to add displayed years to list
        for (MobileElement yearEl: vehicleList) {
            displayedYears.add(yearEl.getText());
        }
    
        //logic to click
        for (int i=0;i< vehicleList.size();i++) {
            String actuallist = vehicleList.get(i).getText();
            System.out.println("Print vehicle year list " +actuallist);
            if(actuallist.equals(year)) {
                driver.findElementByName(year).click();
            } else {
                Utils.scrollDown(driver);
            }
        }
        return displayedYears;
    }