diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/scraper/scraperComponents/URLCharityScraper.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/scraper/scraperComponents/URLCharityScraper.java index f6216cf..f9e98b3 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/scraper/scraperComponents/URLCharityScraper.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/scraper/scraperComponents/URLCharityScraper.java @@ -80,7 +80,7 @@ protected WebDriverWait createWait() { * @param by a selector for {@code WebElement} objects * @return a list of found {@code WebElement} objects matching the given selector */ - protected List findElements(By by) { + public List findElements(By by) { return driver.findElements(by); } diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/scraper/scraperComponents/URLCharityScraperTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/scraper/scraperComponents/URLCharityScraperTest.java index 8c5ca6f..973aca3 100644 --- a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/scraper/scraperComponents/URLCharityScraperTest.java +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/scraper/scraperComponents/URLCharityScraperTest.java @@ -1,11 +1,12 @@ package ntnu.systemutvikling.team6.scraper.scraperComponents; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; import java.util.ArrayList; import java.util.List; + +import ntnu.systemutvikling.team6.scraper.scraperComponents.URLCharityScraper; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openqa.selenium.By; @@ -39,14 +40,15 @@ void updateDescriptionShouldReturnCorrectDescriptionWithoutReadMore() { WebElement p1 = mock(WebElement.class); WebElement p2 = mock(WebElement.class); + when(p1.getText()).thenReturn("Short description"); when(p2.getText()).thenReturn(""); - when(scraper.findElements(By.cssSelector(".information"))).thenReturn(List.of(p1, p2)); - - when(scraper.findElements(By.cssSelector("a.read-more"))).thenReturn(List.of()); + when(scraper.findElements(By.cssSelector("a.read-more"))) + .thenReturn(List.of()); - when(scraper.findElements(By.cssSelector(".extra-info p"))).thenReturn(List.of()); + when(scraper.findElements(By.cssSelector(".information"))) + .thenReturn(List.of(p1, p2)); scraper.updateDescription(); @@ -54,34 +56,32 @@ void updateDescriptionShouldReturnCorrectDescriptionWithoutReadMore() { assertTrue( result.contains("Short description"), "First paragraph should be 'Short description'"); - assertFalse(result.isBlank(), "Second paragraph should be blank."); + assertFalse(result.isBlank(), "Blank paragraph should not be included."); } - @Test - void updateDescriptionShouldReturnCorrectDescriptionWithReadMore() { - WebElement p1 = mock(WebElement.class); - WebElement extra = mock(WebElement.class); - WebElement readMore = mock(WebElement.class); + @Test + void updateDescriptionShouldReturnCorrectDescriptionWithReadMore() { + WebElement p1 = mock(WebElement.class); + WebElement extra = mock(WebElement.class); + WebElement readMore = mock(WebElement.class); - when(p1.getText()).thenReturn("Intro"); - when(extra.getText()).thenReturn("Extra info"); + when(p1.getText()).thenReturn("Intro"); + when(extra.getText()).thenReturn("Extra info"); - when(scraper.findElements(By.cssSelector(".information"))).thenReturn(List.of(p1, extra)); + when(scraper.findElements(By.cssSelector("a.read-more"))).thenReturn(List.of(readMore)); - when(scraper.findElements(By.cssSelector("a.read-more"))).thenReturn(List.of(readMore)); + when(scraper.findElement(By.cssSelector("a.read-more"))).thenReturn(readMore); - when(scraper.findElement(By.cssSelector("a.read-more"))).thenReturn(readMore); + when(scraper.findElements(By.cssSelector(".information"))).thenReturn(List.of(p1, extra)); - when(scraper.findElements(By.cssSelector(".extra-info"))).thenReturn(List.of(extra)); + scraper.updateDescription(); - scraper.updateDescription(); + String result = scraper.getDescription(); - String result = scraper.getDescription(); - - verify(readMore).click(); - assertTrue(result.contains("Intro"), "First paragraph should be 'Intro'"); - assertTrue(result.contains("Extra info"), "Second paragraph should be 'Extra info'"); - } + verify(readMore).click(); + assertTrue(result.contains("Intro")); + assertTrue(result.contains("Extra info")); + } @Test void updateLogoShouldReturnCorrectLogoURL() { @@ -104,9 +104,9 @@ void updateCategoriesShouldReturnCorrectCategories() { when(c1.getText()).thenReturn("Health"); when(c2.getText()).thenReturn("Education"); - List categories = new ArrayList<>(); - categories.add("Health"); - categories.add("Education"); + List categories = new ArrayList<>(); + categories.add("Health"); + categories.add("Education"); when(scraper.findElements(By.cssSelector(".tag-label"))).thenReturn(List.of(c1, c2)); @@ -115,7 +115,7 @@ void updateCategoriesShouldReturnCorrectCategories() { assertEquals( categories, scraper.getCategories(), - "Should return an list that contain two string elements 'Health' and 'Education'."); + "Should return a string that lists categories with ',' as a delimiter."); } @Test @@ -162,4 +162,43 @@ void scrapeCharityPageShouldCallAllRelevantMethods() { // UpdateKeyValues should run verify(spyScraper).updateKeyValues(); } + + @Test + void updateLogoShouldHandleException() { + when(scraper.findElement(By.cssSelector(".logo > img"))).thenThrow(new RuntimeException("Not found")); + + scraper.updateLogo(); + + assertNull(scraper.getLogoURL(), "Program should handle the exception and the logo should be null."); + } + + @Test + void updateCategoriesShouldHandleException() { + when(scraper.findElements(By.cssSelector(".tag-label"))).thenThrow(new RuntimeException("Fail")); + + scraper.updateCategories(); + + assertEquals(new ArrayList<>(), scraper.getCategories(), "Program should handle the exception " + + "and the categories should be null."); + } + + @Test + void updateKeyValuesShouldHandleException() { + when(scraper.findElement(any(By.class))).thenThrow(new RuntimeException("Fail")); + + scraper.updateKeyValues(); + + assertEquals("", scraper.getKeyValues(), "Program should handle the exception and the " + + "exception and the key values should be empty."); + } + + @Test + void updateDescriptionShouldHandleException() { + when(scraper.findElements(any(By.class))).thenThrow(new RuntimeException("Fail")); + + scraper.updateDescription(); + + assertNull(scraper.getDescription(), "Program should handle the exception and the " + + "description should be null."); + } }