Skip to content

Commit

Permalink
Fix: Updated to display categroies
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianBalunan committed Apr 14, 2026
2 parents 7c624ad + f3ab384 commit 2c7ba8a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<WebElement> findElements(By by) {
public List<WebElement> findElements(By by) {
return driver.findElements(by);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -39,49 +40,48 @@ 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();

String result = scraper.getDescription();

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() {
Expand All @@ -104,9 +104,9 @@ void updateCategoriesShouldReturnCorrectCategories() {
when(c1.getText()).thenReturn("Health");
when(c2.getText()).thenReturn("Education");

List<String> categories = new ArrayList<>();
categories.add("Health");
categories.add("Education");
List<String> categories = new ArrayList<>();
categories.add("Health");
categories.add("Education");

when(scraper.findElements(By.cssSelector(".tag-label"))).thenReturn(List.of(c1, c2));

Expand All @@ -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
Expand Down Expand Up @@ -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.");
}
}

0 comments on commit 2c7ba8a

Please sign in to comment.