Latest post from this blog

Test Scenarios vs. Test Cases: Understanding the Basics

What Are Test Scenarios Test scenarios represent high-level ideas or conditions that need to be validated to ensure the application works as expected. They provide a broader perspective and are typically used during the test planning phase. Purpose: To capture the "what to test" without going into granular details. Example of a Test Scenario: Verify that a user can successfully log in to the application using valid credentials. Verify the behavior of the login page when invalid credentials are entered. Verify the application behavior when the login button is clicked without entering any credentials. What Are Test Cases Test cases are detailed documents that define the specific steps to execute a test. They cover inputs, execution steps, expected results, and actual outcomes. Purpose: To guide the tester step-by-step on "how to test." Example of a Test Case (for the first scenario): Test Case ID TC_01_Login_Valid_Credentials Test Scenario Verify user login with val...

Synchronization In Selenium

What is Synchronization: ensures that the test script interacts with web elements only when they are fully loaded and ready. This helps avoid errors that occur when elements are not yet available or intractable.

Synchronization is needed in Selenium
Synchronization is important because web elements might not be ready when test script tries to interact with them. Web applications can have varying load times due to factors like network issues, server delays, or dynamic content changes (such as AJAX), which can cause test failures if not handled properly.

Problem without synchronization:
StaleElementReferenceException: This error occurs if the web page updates or refreshes, causing Selenium to lose track of the element.

Timeout Exception: This error happens when the test script attempts to interact with an element that is not yet loaded or visible.

Synchronization solves these problems:
- Ensuring the web element is fully loaded and ready before interacting.
- Handling dynamic content (like AJAX updates) more reliably.
- Reducing test failures caused by timing issues.

Synchronization is achieved by using waits (like Implicit, Explicit, or Fluent Waits)

What is the use of wait: The use of wait in Selenium is to pause the test until the webpage or element is ready, so the test script doesn’t try to interact with things before they fully load. This helps prevent errors when pages are slow to respond.

1.Static wait(Thread.Sleep): Even though the locator is found, it will wait for maximum time
Thread.sleep(milliseconds);--->throws InterruptedException

Example:
Thread.sleep(5000);--->wait for 5 second

2.Dynamic wait: If the locator is found within some seconds,it will not wait till the maximum time. The main types of dynamic waits are:

Explicit Wait: you can wait for a certain condition, such as an element becoming clickable, visible, or present on the DOM, before taking action.

When to Use Explicit Wait:
✓When some elements take longer to load than others.
✓When you want to wait for specific conditions like visibility, clickability, or presence of an element.

Example:
WebDriver driver = new ChromeDriver();
driver.get("https://facebook.com");

// Create an explicit wait object with a maximum wait time of 15 seconds
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));

// Wait until a specific element is clickable before interacting with it
WebElement buttonElement = wait.until(ExpectedConditions.elementToBeClickable(By.id("element")));
buttonElement.click();

Implicit Wait: implicit wait commands selenium to wait for a certain amount of time before throwing a "No such element" exception

When to Use It:
✓When all elements on your page might take time to load.
✓If you don’t need special conditions for different elements.

Example:
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
 // Wait for 10 seconds
driver.get("https://facebook.com");

WebElement element = driver.findElement(By.id("buttonElement"));
// This will wait up to 10 seconds if needed

Fluent Wait:
✓Set a maximum wait time.
✓Specify how often to check for the               condition (polling frequency).
✓Ignore certain exceptions like NoSuchElementException while waiting     for the condition.

When to Use Fluent Wait:
✓When elements may not load consistently and you want to periodically check for their availability.
✓When you need to ignore specific exceptions while waiting for an element.

difference between implict wait, explict wait and Fluent wait:

Implict wait:
• It is applicable for all the locators in the webpage.

• driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

• No options to ignore exceptions

Explict wait:
• We can set ExplicitWait for particular locator/condition.

• WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); WebElement element = wait.until(ExpectedConditions.elementToBeClickable

• Handles specific conditions; no option to ignore exceptions

Fluent wait:
• Polls at defined intervals until the condition is met or timeout occurs

• Wait<WebDriver> wait = new FluentWait<>(driver).withTimeout(Duration.ofSeconds(10)).pollingEvery(Duration.ofSeconds(1)).ignoring(NoSuchElementException.class)
WebElement element = wait.until(driver -> driver.findElement(By.id("elementId")));

• Can ignore specific exceptions (e.g., NoSuchElementException)

Comments

Popular posts from this blog

Common Exception in Selenium

Test Scenarios vs. Test Cases: Understanding the Basics

Handling Shadow DOM in Selenium WebDriver

Handling HTTPS Websites and SSL Certificate Errors in Selenium

Normalize-space Function In Xpath