Using Selenium WebDriver to Test Carousel Rotation

There are several websites having carousel rotation, especially e-commerce websites, which allows quick and easy navigation of the listed items. The rotating item in the list must be tested to provide a hassle-free browsing experience to the users.

An example of carousel with rotation has been shown below.

Using Selenium WebDriver to Test Carousel Rotation

WebDriver caters the need to test and verify carousel rotation.

You can check the content against each element within the carousel and verify whether the text of the item changes on changing the item.

//To Get the Number of Items in the Carousel

String selector = “li[class^=a-carousel-card]”;

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

ArrayList<String> list1 = new ArrayList<String>();

String name;

<div style=”clear:both; margin-top:0em; margin-bottom:1em;”><a href=”http://www.testingexcellence.com/how-to-upload-files-using-selenium-and-autoit/” target=”_blank” rel=”nofollow” class=”u5f66b656a489f77a84dd8f1a1d2f8cfb”><div style=”padding-left:1em; padding-right:1em;”><span class=”ctaText”></span>  <span class=”postTitle”>How to Upload and Submit Files Using Selenium and AutoIt</span></div></a></div>

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

int index = i+1;

 //To Get the Name of Each Item in Carousel

name = driver.findElement(By.cssSelector(selector + “[“+index+”]”)).getText();

list1.add(name);

}

//Clicking on the Arrow of the Carousel

driver.findElement(By.cssSelector(“div[class^=a-carousel-col] a”)).click();

//New Items get loaded in the Carousel after the Click

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

ArrayList<String> list2 = new ArrayList<String>();

String newName;

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

     int index = i+1;

 //To Get the Name of Each Item in Carousel

  newName = driver.findElement(By.cssSelector(selector + “[“+index+”]”)).getText();

  list2.add(newName);

}

//Comparing the Two Array Lists

ArrayList commonList = CollectionUtils.retainAll(list1,list2);

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

SHARE THIS

Get A Free Quote

One thought on “Using Selenium WebDriver to Test Carousel Rotation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.