Showing posts with label exception. Show all posts
Showing posts with label exception. Show all posts

Friday, 2 August 2019

what is stale element exception - how to fix it


Stale = left over / old

if an element changes its position and then if you are trying to perform some event on top of it then we get stale element exception.

below are the fixes

wait.until(ExpectedConditions.visiblityofElementToBeClickable(By.id(“id”)))

wait.until(ExpectedConditions.presenceOfElementLocated(By.id("id")));

wait.until(ExpectedConditions.refreshed(ExpectedConditions.stalenessOf("table")));

Tuesday, 7 May 2019

Interview | How can you solve StaleElementReferenceException

How can you solve StaleElementReferenceException

Answer : ExpectedConditions.refreshed

Stale means old, decayed, no longer fresh. Stale Element means an old element or no longer available element. Assume there is an element that is found on a web page referenced as a WebElement in WebDriver. If the DOM changes then the WebElement goes stale. If we try to interact with an element which is staled then the StaleElementReferenceException is thrown.


public WebElement waitForElementToBeRefreshedAndClickable(WebDriver driver, By by) {
    return new WebDriverWait(driver, 30)
            .until(ExpectedConditions.refreshed(
                    ExpectedConditions.elementToBeClickable(by)));
}

So basically, this is a method that waits until a DOM manipulation is finished on an object.

When the DOM has manipulated, and say after clicking a button, adds a class to that element. If you try to perform an action on said element, it will throw StaleElementReferenceException since now the  WebElement returned now does not represent the updated element.

You'll use refreshed when you expect DOM manipulation to occur, and you want to wait until it's done being manipulated in the DOM.

Interview | Can we have multiple finally blocks

Answer is NO.



try (1)
catch (0 or more)
finally (0 or 1)


- try block must be followed by either catch or finally block.
- When an exception occurs, that exception occurred is handled by catch block associated with it.
- Java catch block is used to handle the Exception. ...
- This ensures that the finally block is executed even if an unexpected exception occurs.

Monday, 17 September 2018

Testng - how to skip a test case at run time

@Test
public void m1(){
if(aboveMethodFailed)
throw new SkipException("Skipped");
}

using
throw new SkipException("Skipped"); statement we can skip an test case.