Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

1 like 0 dislike
446 views
by Contributing Tester (22 points)
edited by
How do I scroll the gallery screen to the end and select the last media during automation?
Scenario: I want to scroll the gallery till last and need to select the last media showing on the gallery screen.
by Expert (572 points)
Can you post the code you have tried so far?

2 Answers

0 like 0 dislike
by Expert (572 points)
You can use JavascriptExecutor to imitate the scroll with below code.

JavascriptExecutor Scrool = (JavascriptExecutor) driver;
Scrool.executeScript("window.scrollBy(0,300)", "");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

You can put above code in loop and wait for the element expected.

E.g.

WebDriverWait wait = new WebDriverWait(Scenario1Test.driver, 10);

while(!(wait.until(ExpectedConditions.elementToBeClickable(By.xpath(""))) == null)){

JavascriptExecutor Scrool = (JavascriptExecutor) driver;

Scrool.executeScript("window.scrollBy(0,300)", "");

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

}
by Contributing Tester (22 points)
@SunilBhaskar
Thanks for answer.

But I don't have any expected element in last.
As the locators for all the media in gallery screen are showing same. So I am not able to distinguish between first and last.
by Contributing Tester (22 points)
public static String actionSwipeTopToBottomAndVerifyComponent(HashMap<String, Object> driverDetailsMap,HashMap<String, String> inputParamMap) throws InterruptedException{
       
        String componentType = inputParamMap.get("componentType");
        String componentValue = inputParamMap.get("componentValue");
        String deviceName = (String) inputParamMap.get("deviceToExecute");
        String devicePlatformName = (String) inputParamMap.get("devicePlatformName");
       
        AndroidDriver driver = (AndroidDriver) Setup.getDriver(deviceName, devicePlatformName, driverDetailsMap);
        
        try{
            By identifierFindMethod = getIdentifierFindMethod(componentType, componentValue);
            driver.findElement(identifierFindMethod).isDisplayed();
        }catch(Exception e){
            try {
                Utility.swipingTopToBottom(driver);
                actionSwipeTopToBottomAndVerifyComponent(driverDetailsMap, inputParamMap);
            }catch(Exception e1){
            }
        }
        return CommonConstant.DISPLAYED;
        
    }
by Expert (572 points)
Okay. So you are doing it for Android. It looks like you already have a method to swipe to bottom. I understand that you are looking for a method to identify the last element. But, are you able to scroll down?
by Contributing Tester (22 points)
Yes Sunil, I am able to scroll down. But I need to provide the scroll count. So if it takes 5 scroll to reach the last, I need to provide 5 in my script.
by Expert (572 points)
In that case, first you can use findElements to identify number of items inside gallery. Find out what's common way to identify each  element and use findElements method to identify number of items.

Once you have number of items counted, you can calculate how many scrolls to make.
by Contributing Tester (22 points)
Thanks for answer.
by Master (1.2k points)
If your query is resolved, kindly select the best answer.
by Contributing Tester (22 points)
@mayur
Definitely, I will.
0 like 0 dislike
by
I had a similar situation. But, I had the number of elements fixed in the gallery.

I know I had 12 images, and I am supposed to select 11th Image.

I simply used key down events to press down arrow key and reached till the last image, i.e. 10th image. The gallery had the 3x3 view.

So, after reaching 10th image, you can key press left and select 11th Image.

That worked like charm.
by Contributing Tester (22 points)
Ya I got, but my scenario is different. I need to reach last and the count is not static.


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...