DEV Community

Mangai Ram
Mangai Ram

Posted on • Updated on

Part :7 My Automation Interview Questions in one of leading MNC

You encounter a web page with dynamic elements whose attributes change on each reload. How would you automate the testing of such elements?

Explanation:

Dynamic elements on a web page are elements whose attributes or properties change dynamically, making them challenging to locate consistently using static locators. These changes can occur on each page reload or due to interactions on the page. Automating the testing of such elements requires strategies to handle their dynamic nature.

Use Case:

In our project, we had a scenario where a web page displays a set of dynamically changing promotional banners. To automate testing, I implemented a strategy using partial attribute matching and dynamic wait times to locate and interact with these banners irrespective of their changing attributes.

Code Snippet:

// Assume the promotional banners have a dynamic ID that changes on each reload

// Use a partial attribute match to identify the banners

// Custom method to wait for the presence of an element with a dynamic ID

public WebElement waitForDynamicElement(String partialId)
{
String dynamicLocator =
"//*[contains(@id, '" + partialId + "')]";
WebDriverWait wait = new WebDriverWait(driver, 10);
return wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(dynamicLocator)));
}

// Example usage to click on a dynamically changing banner

String dynamicBannerId = "promoBanner";

// Replace with the actual partial ID

WebElement dynamicBanner = waitForDynamicElement(dynamicBannerId);
dynamicBanner.click();

This code snippet demonstrates a custom method (waitForDynamicElement) that uses a partial attribute match to locate a dynamically changing element.

The method returns a _WebElement _that can be interacted with in subsequent steps.

Exception:

An exception that might occur is a _TimeoutException _if the expected dynamic element is not present within the specified wait time. Proper exception handling and adjusting wait times based on the application’s responsiveness are essential.

Challenges:

One challenge was identifying a reliable partial attribute that could be used for matching. To address this, I collaborated with the development team to introduce consistent partial attributes for dynamically changing elements. Another challenge was ensuring the stability of tests when elements changed rapidly.

To overcome this, I implemented dynamic waits with flexible conditions, adapting to the changing nature of the elements without causing test failures due to timing issues.

In your point of view any add on required means let discuss

To know more selenium Interview questions

these automation interview questions are published in testleaf blog with on behalf of them am sharing with my inputs.

Thank you

Top comments (0)