logo

The best IT Trainig Institute In Gurgaon

Resize Widget in Selenium

Resizing a widget in Selenium refers to changing the size of a UI element, typically a resizable box, slider, or similar interactive component. Selenium WebDriver, combined with the Actions class, allows for simulating such actions, including dragging the corners or sides of an element to resize it.

How to Perform Resize Actions in Selenium

To resize an element, you generally follow these steps:

1. Locate the Resizable Handle:

  • This is the part of the element that the user would click and drag to resize, often represented by a small icon or a corner of the element.

2. Use the Actions Class:

  • The Actions class in Selenium provides methods like click And Hold(), move By Offset(), and release() to simulate the resizing action.
resize
package asc;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ResizeAndWidget {

public static void main(String[] args) {
		
WebDriverManager.chromedriver().setup();
ChromeDriver driver = new ChromeDriver();

driver.get("https://jqueryui.com/resizable/");

driver.manage().window().maximize();

WebElement frame = driver.findElement(By.xpath("//*[@id=\"content\"]/iframe"));
driver.switchTo().frame(frame);

WebElement resize = driver.findElement(By.xpath("//*[@id=\"resizable\"]/div[3]"));

Actions action = new Actions(driver);
action.dragAndDropBy(resize, 300, 100).perform();
}

}
                
code
Output
code
Code Breakdown and Explanation:

1. Setup and Initialization:

                    
WebDriverManager.chromedriver().setup();
ChromeDriver driver = new ChromeDriver();
                    
                
  • WebDriverManager.chromedriver().setup();:
    Configures WebDriverManager to handle the correct version of the ChromeDriver automatically.
  • new ChromeDriver();:
    Initializes a new instance of the ChromeDriver, which controls the Chrome browser.

2. Navigating to the Web Page:

                    
driver.get("https://jqueryui.com/resizable/");
driver.manage().window().maximize();
                    
                
  • driver.get("https://jqueryui.com/resizable/");:
    Navigates to the jQuery UI Resizable demo page, which contains a widget that can be resized.
  • driver.manage().window().maximize();:
    Maximizes the browser window to ensure that all elements are fully visible and interactable.

3. Handling Frames:

                    
WebElement frame = driver.findElement(By.xpath("//*[@id=\"content\"]/iframe"));
driver.switchTo().frame(frame);
                    
                
  • The resizable widget is inside an iframe on the jQuery UI demo page. Selenium requires switching to the iframe context to interact with elements inside it.
  • driver.findElement(By.xpath("//*[@id=\"content\"]/iframe"));:
    Finds the iframe element by its XPath.
  • driver.switchTo().frame(frame);
    Switches the WebDriver's context to the iframe, allowing interactions with elements within it.

4. Locating the Resizable Handle:

                    
WebElement resize = driver.findElement(By.xpath("//*[@id=\"resizable\"]/div[3]"));
                    
                
  • driver.findElement(By.xpath("//*[@id=\"resizable\"]/div[3]"));:
    Locates the handle of the resizable widget using its XPath. The handle is typically a small area in the corner of the widget used to resize it.

5. Performing the Resize Action:

                    
Actions action = new Actions(driver);
action.dragAndDropBy(resize, 300, 100).perform();                        
                    
                
  • new Actions(driver);:
    Creates an instance of the Actions class, which is used for performing advanced user interactions.
  • action.dragAndDropBy(resize, 300, 100).perform();:
    This method simulates dragging the resize element by a specified offset. Here, it drags the handle 300 pixels to the right and 100 pixels down from its current position, effectively resizing the widget.
Key Points
    Frame Handling:
  • Since the resizable widget is within an iframe, you need to switch to the iframe before interacting with elements inside it. Always switch back to the default content if further interactions are needed outside the iframe.
  • Locating the Resizable Handle:
  • Accurate identification of the resizable handle is crucial. The XPath used in the code targets the specific handle (often a small square or dot) used for resizing.
  • Using dragAndDropBy:
  • The drag And Drop By method provides a straightforward way to perform drag-and-drop actions with specified offsets. It directly moves the element by the given x and y coordinates relative to its current position.
Considerations:
  • Synchronization:
    Ensure that the iframe and resizable handle are fully loaded and visible before performing the action. Use explicit waits if needed to wait for elements to become interactable.
  • Cross-Browser Testing:
    Drag-and-drop interactions may vary slightly between browsers. Testing across different browsers helps ensure consistent behavior.
  • Element Locators:
    The XPath used should be checked for stability. Changes in the web page structure might affect the XPath, so always verify locators if the page changes.
  • Resizing Precision:
    The offset values (300 and 100 in this case) should be adjusted based on the actual resizing needs and dimensions of the widget.