Hey Hussain, you can use following lines of code to write automation script for testing Carousel Rotation in Selenium:
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();
list1.add(name);
}
//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();
list2.add(newName);
}
//Then we compare the two arrayLists are not the same
ArrayList commonList = CollectionUtils.retainAll(list1, list2);
Assert.assertTrue(commonList.size() == 0);
}
}
Hope this helps!
Check out this automation testing training and learn more.
Thanks!