Skip to content

Commit a44be73

Browse files
authored
Merge pull request eugenp#763 from Sandeep4odesk/master
Handling Optional as per orElseThrow criteria
2 parents 92d320c + ac277fd commit a44be73

1 file changed

Lines changed: 6 additions & 7 deletions

File tree

selenium-junit-testng/src/main/java/com/baeldung/selenium/SeleniumExample.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main.java.com.baeldung.selenium;
22

33
import java.util.List;
4+
import java.util.NoSuchElementException;
45
import java.util.concurrent.TimeUnit;
56

67
import org.openqa.selenium.By;
@@ -28,16 +29,16 @@ public String getTitle() {
2829
return webDriver.getTitle();
2930
}
3031

31-
public void getAboutBaeldungPage() {
32+
public void getAboutBaeldungPage() throws Exception {
3233
closeOverlay();
3334
clickAboutLink();
3435
clickAboutUsLink();
3536
}
3637

37-
private void closeOverlay() {
38+
private void closeOverlay() throws Exception {
3839
List<WebElement> webElementList = webDriver.findElements(By.tagName("a"));
39-
if (webElementList != null && !webElementList.isEmpty()) {
40-
webElementList.stream().filter(webElement -> "Close".equalsIgnoreCase(webElement.getAttribute("title"))).findAny().get().click();
40+
if (webElementList != null && !webElementList.isEmpty() && webElementList.stream().filter(webElement -> "Close".equalsIgnoreCase(webElement.getAttribute("title"))).findAny().isPresent()) {
41+
webElementList.stream().filter(webElement -> "Close".equalsIgnoreCase(webElement.getAttribute("title"))).findAny().orElseThrow(NoSuchElementException::new).click();
4142
}
4243
}
4344

@@ -50,8 +51,6 @@ private void clickAboutUsLink() {
5051
}
5152

5253
public boolean isAuthorInformationAvailable() {
53-
return webDriver
54-
.findElement(By.xpath("//*[contains(text(), 'Eugen – an engineer')]"))
55-
.isDisplayed();
54+
return webDriver.findElement(By.xpath("//*[contains(text(), 'Eugen – an engineer')]")).isDisplayed();
5655
}
5756
}

0 commit comments

Comments
 (0)