How to Test Carousel Rotation with Selenium Webdriver

Some websites have carousel with rotating items in the list. In this webdriver tutorial we look at how to test and verify carousel rotation with webdriver.

For this tutorial, we are going to use to demonstrate the testing of carousel.


when we inspect the elements in carousel, the structure is like:


When we click on the arrows, the content of the carousel changes and then we can verify that the text of the items have changed.

Each <li> element is an item of the carousel, so we need to get the text (item’s name) of each item.

public class CarouselTest {

    public void test() {

        //This will get the number of items in the carousel
        String selector = "li[class^=a-carousel-card]";
        ArrayList items = driver.findElements(By.cssSelector(selector));

        ArrayList list1 = new ArrayList();
        String name;

        for (int i = 0; i < items; i++) {
            int index = i + 1;

            //This will get the name of each item in carousel
            name = driver.findElement(By.cssSelector(selector + "[" + index + "]")).getText();

        //Next we click on the arrow of the carousel
        driver.findElement(By.cssSelector("div[class^=a-carousel-col] a")).click();

        //Then we new items are loaded in the carousel following the click,
        //we get the names again

        ArrayList nextItems = driver.findElements(By.cssSelector(selector));

        ArrayList list2 = new ArrayList();

        String newName;

        for (int i = 0; i < nextItems; i++) {
            int index = i + 1;

            //This will get the name of each item in carousel
            newName = driver.findElement(By.cssSelector(selector + "[" + index + "]")).getText();

        //Then we compare the two arrayLists are not the same
        ArrayList commonList = CollectionUtils.retainAll(list1, list2);

        Assert.assertTrue(commonList.size() == 0);


Leave a Reply