From 0ec340b88012447d6b46bac30b7dfd7c0ff18ea5 Mon Sep 17 00:00:00 2001 From: Roar Date: Tue, 7 Apr 2026 08:51:45 +0200 Subject: [PATCH] Added URLCharityScraperTest Test file for URLCharityScraper with as much coverage as possible. --- .../team6/scraper/URLCharityScraperTest.java | 171 ++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/scraper/URLCharityScraperTest.java diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/scraper/URLCharityScraperTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/scraper/URLCharityScraperTest.java new file mode 100644 index 0000000..d437891 --- /dev/null +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/scraper/URLCharityScraperTest.java @@ -0,0 +1,171 @@ +package ntnu.systemutvikling.team6.scraper; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.ui.WebDriverWait; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +class URLCharityScraperTest { + + private WebDriver driver; + private URLCharityScraper scraper; + + @BeforeEach + void setup() { + driver = mock(WebDriver.class); + + scraper = new URLCharityScraper("http://test", driver) { + @Override + protected WebDriverWait createWait() { + return mock(WebDriverWait.class); + } + + @Override + protected void closeDriver() { + } + }; + } + + @Test + 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 div p"))) + .thenReturn(List.of(p1, p2)); + + when(scraper.findElements(By.cssSelector("a.read-more"))) + .thenReturn(List.of()); + + when(scraper.findElements(By.cssSelector(".extra-info p"))) + .thenReturn(List.of()); + + scraper.updateDescription(); + + String result = scraper.getDescription(); + + assertTrue(result.contains("Short description"), + "First paragraph should be 'Short description'"); + assertFalse(result.isBlank(), + "Second paragraph should be blank."); + } + + @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(scraper.findElements(By.cssSelector(".information div p"))) + .thenReturn(List.of(p1)); + + when(scraper.findElements(By.cssSelector("a.read-more"))) + .thenReturn(List.of(readMore)); + + when(scraper.findElement(By.cssSelector("a.read-more"))) + .thenReturn(readMore); + + when(scraper.findElements(By.cssSelector(".extra-info p"))) + .thenReturn(List.of(extra)); + + scraper.updateDescription(); + + 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'"); + } + + @Test + void updateLogoShouldReturnCorrectLogoURL() { + WebElement logo = mock(WebElement.class); + + when(scraper.findElement(By.cssSelector(".logo > img"))) + .thenReturn(logo); + + when(logo.getAttribute("src")).thenReturn("logo.png"); + + scraper.updateLogo(); + + assertEquals("logo.png", scraper.getLogoURL()); + } + + @Test + void updateCategoriesShouldReturnCorrectCategories() { + WebElement c1 = mock(WebElement.class); + WebElement c2 = mock(WebElement.class); + + when(c1.getText()).thenReturn("Health"); + when(c2.getText()).thenReturn("Education"); + + when(scraper.findElements(By.cssSelector(".tag-label"))) + .thenReturn(List.of(c1, c2)); + + scraper.updateCategories(); + + assertEquals("Health,Education", scraper.getCategories(), + "Should return a string that lists categories with ',' as a delimiter."); + } + + @Test + void updateKeyNumbersShouldReturnCorrectNumbers() { + WebElement e1 = mock(WebElement.class); + WebElement e2 = mock(WebElement.class); + WebElement e3 = mock(WebElement.class); + + when(e1.getAttribute("data-percentage")).thenReturn("80"); + when(e2.getAttribute("data-percentage")).thenReturn("10"); + when(e3.getAttribute("data-percentage")).thenReturn("90"); + + when(scraper.findElement(any(By.class))) + .thenReturn(e1, e2, e3); + + scraper.updateKeyValues(); + + System.out.println(scraper.getKeyValues()); + + assertEquals("80:10:90", scraper.getKeyValues(), + "Should return a string that lists key values with ':' as a delimiter."); + } + + @Test + void scrapeCharityPageShouldCallAllRelevantMethods() { + URLCharityScraper spyScraper = spy(scraper); + + doNothing().when(spyScraper).updateDescription(); + doNothing().when(spyScraper).updateLogo(); + doNothing().when(spyScraper).updateCategories(); + doNothing().when(spyScraper).updateKeyValues(); + + spyScraper.scrapeCharityPage(); + + // Url should be correct + verify(driver).get("http://test"); + // UpdateDescription should run + verify(spyScraper).updateDescription(); + // UpdateLogo should run + verify(spyScraper).updateLogo(); + // UpdateCategories should run + verify(spyScraper).updateCategories(); + // UpdateKeyValues should run + verify(spyScraper).updateKeyValues(); + } + + +} \ No newline at end of file