Skip to content

Commit

Permalink
Added URLCharityScraperTest
Browse files Browse the repository at this point in the history
Test file for URLCharityScraper with as much coverage as possible.
  • Loading branch information
roaraf committed Apr 7, 2026
1 parent 39aedba commit 0ec340b
Showing 1 changed file with 171 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}


}

0 comments on commit 0ec340b

Please sign in to comment.