Skip to content

Commit

Permalink
Merge pull request #32 from healenium/IS-31
Browse files Browse the repository at this point in the history
[IS-31] Fix incorrect exception catching
Trying to heal with ExpectedConditions.invisibilityOf #31
  • Loading branch information
GannaChernyshova authored Jan 29, 2020
2 parents faeaab9 + c3321ba commit 2f4cd11
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies {
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.12.1'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.12.1'
compile group: 'org.eclipse.jetty', name: 'jetty-server', version: '9.4.19.v20190610'
compile group: 'org.jetbrains', name: 'annotations', version: '18.0.0'
compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.10'
annotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.10'
testCompile group: 'com.codeborne', name: 'selenide', version: '5.5.1'
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/com/epam/healenium/handlers/proxy/BaseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.NotFoundException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
Expand All @@ -62,9 +62,8 @@ public BaseHandler(SelfHealingEngine engine) {
.build(
new CacheLoader<PageAwareBy, WebElement>() {
@Override
public WebElement load(PageAwareBy key) {
WebElement result = lookUp(key);
return Optional.ofNullable(result).orElseThrow(NotFoundException::new);
public WebElement load(@NotNull PageAwareBy key) {
return lookUp(key);
}
});
}
Expand All @@ -77,11 +76,8 @@ protected WebElement findElement(By by) {
return driver.findElement(inner);
}
return stash.get(pageBy);
} catch (NoSuchElementException ex) {
throw ex;
} catch (Exception ex) {
log.warn("Failed to find element", ex);
return null;
throw new NoSuchElementException("Failed to find element using " + by.toString(), ex);
}
}

Expand All @@ -97,7 +93,7 @@ private WebElement lookUp(PageAwareBy key) {
return element;
} catch (NoSuchElementException e) {
log.warn("Failed to find an element using locator {}\nReason: {}\nTrying to heal...", key.getBy().toString(), e.getMessage());
return heal(key, e).orElse(null);
return heal(key, e).orElseThrow(() -> e);
}
}

Expand Down Expand Up @@ -176,7 +172,7 @@ private Optional<By> healLocator(PageAwareBy pageBy) {
/**
* Create screenshot of healed element
* @param by - healed locator
* @return
* @return path to screenshot location
*/
private String captureScreen(By by) {
WebElement element = findElement(by);
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/epam/healenium/AbsentLocatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
Expand All @@ -36,12 +37,11 @@ public void createDriver() {
driver = SelfHealingDriver.create(delegate);
}

@Test
@Test(expected = NoSuchElementException.class)
public void name() {
driver.get("https://google.com/");
PageAwareBy by = PageAwareBy.by(PAGE_NAME, By.tagName("nonexistenttag"));
WebElement input = driver.findElement(by);
Assert.assertNull(input);
driver.findElement(by);
}

@After
Expand Down

0 comments on commit 2f4cd11

Please sign in to comment.