Skip to content

Commit

Permalink
test(selenium): word learning events (#1856)
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-elimu authored Aug 25, 2024
2 parents 1e5443e + 28ba380 commit 691d177
Show file tree
Hide file tree
Showing 24 changed files with 242 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public String handleRequest(Model model) {
List<StoryBookLearningEvent> storyBookLearningEvents = storyBookLearningEventDao.readAllOrderedByTime();
model.addAttribute("storyBookLearningEvents", storyBookLearningEvents);

// Prepare data for chart in UI
// Prepare chart data
List<String> monthList = new ArrayList<>();
List<Integer> eventCountList = new ArrayList<>();
if (!storyBookLearningEvents.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

import ai.elimu.dao.WordLearningEventDao;
import ai.elimu.model.analytics.WordLearningEvent;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -27,6 +34,36 @@ public String handleRequest(Model model) {
List<WordLearningEvent> wordLearningEvents = wordLearningEventDao.readAll();
model.addAttribute("wordLearningEvents", wordLearningEvents);

// Prepare chart data
List<String> monthList = new ArrayList<>();
List<Integer> eventCountList = new ArrayList<>();
if (!wordLearningEvents.isEmpty()) {
// Group event count by month (e.g. "Aug-2024", "Sep-2024")
Map<String, Integer> eventCountByMonthMap = new HashMap<>();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM-yyyy");
for (WordLearningEvent event : wordLearningEvents) {
String eventMonth = simpleDateFormat.format(event.getTimestamp().getTime());
eventCountByMonthMap.put(eventMonth, eventCountByMonthMap.getOrDefault(eventMonth, 0) + 1);
}

// Iterate each month from 4 years ago until now
Calendar calendar4YearsAgo = Calendar.getInstance();
calendar4YearsAgo.add(Calendar.YEAR, -4);
Calendar calendarNow = Calendar.getInstance();
Calendar month = calendar4YearsAgo;
while (!month.after(calendarNow)) {
String monthAsString = simpleDateFormat.format(month.getTime());
monthList.add(monthAsString);

eventCountList.add(eventCountByMonthMap.getOrDefault(monthAsString, 0));

// Increase the date by 1 month
month.add(Calendar.MONTH, 1);
}
}
model.addAttribute("monthList", monthList);
model.addAttribute("eventCountList", eventCountList);

return "analytics/word-learning-event/list";
}
}
2 changes: 1 addition & 1 deletion src/main/webapp/WEB-INF/jsp/analytics/main.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<span class="card-title"><i class="material-icons">sms</i> <fmt:message key="words" /></span>
</div>
<div class="card-action">
<a href="<spring:url value='/analytics/word-learning-event/list' />"><fmt:message key="view.list" /> (${wordLearningEventCount})</a>
<a id="wordLearningEventsLink" href="<spring:url value='/analytics/word-learning-event/list' />"><fmt:message key="view.list" /> (${wordLearningEventCount})</a>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@

<table class="bordered highlight">
<thead>
<th>timestamp</th>
<th>android_id</th>
<th>package_name</th>
<th>storybook_id</th>
<th>storybook_title</th>
<th>learning_event_type</th>
<th><code>timestamp</code></th>
<th><code>android_id</code></th>
<th><code>package_name</code></th>
<th><code>storybook_id</code></th>
<th><code>storybook_title</code></th>
<th><code>learning_event_type</code></th>
</thead>
<tbody>
<c:forEach var="storyBookLearningEvent" items="${storyBookLearningEvents}">
Expand Down
38 changes: 17 additions & 21 deletions src/main/webapp/WEB-INF/jsp/analytics/word-learning-event/list.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,30 @@
<content:section cssId="wordLearningEventsPage">
<div class="section row">
<div class="card-panel">
<script src="<spring:url value='/static/js/chart.bundle.min-2.8.0.js' />"></script>
<link rel="stylesheet" href="<spring:url value='/static/css/chart.min-2.8.0.css' />" />
<canvas id="myChart" width="400" height="100"></canvas>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.min.js"></script>
<canvas id="chart"></canvas>
<script>
const labels = [
'January',
'February',
'March',
'April',
'May',
'June',
<c:forEach var="month" items="${monthList}">'${month}',</c:forEach>
];
const data = {
labels: labels,
datasets: [{
label: 'My First dataset',
backgroundColor: 'rgba(149,117,205, 0.5)',
borderColor: 'rgba(149,117,205, 0.5)',
data: [0, 10, 5, 2, 20, 30, 45],
data: <c:out value="${eventCountList}" />,
label: 'Learning events',
backgroundColor: 'rgba(149,117,205, 0.5)', // #9575cd deep-purple lighten-2
borderColor: 'rgba(149,117,205, 0.5)', // #9575cd deep-purple lighten-2
tension: 0.5,
fill: true
}]
};
const config = {
type: 'line',
data: data,
options: {}
};
var ctx = document.getElementById('myChart');
var myRadarChart = new Chart(ctx, config);
var ctx = document.getElementById('chart');
new Chart(ctx, config);
</script>
</div>
</div>
Expand All @@ -53,12 +49,12 @@

<table class="bordered highlight">
<thead>
<th><fmt:message key="time" /></th>
<th>Android ID</th>
<th><fmt:message key="application" /></th>
<th><fmt:message key="word" /></th>
<th><fmt:message key="word.text" /></th>
<th><fmt:message key="learning.event.type" /></th>
<th><code>timestamp</code></th>
<th><code>android_id</code></th>
<th><code>package_name</code></th>
<th><code>word_id</code></th>
<th><code>word_text</code></th>
<th><code>learning_event_type</code></th>
</thead>
<tbody>
<c:forEach var="wordLearningEvent" items="${wordLearningEvents}">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package ai.elimu.rest.v2.content;

import ai.elimu.util.JsonLoader;
import selenium.util.DomainHelper;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import selenium.DomainHelper;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package ai.elimu.rest.v2.content;

import ai.elimu.util.JsonLoader;
import selenium.util.DomainHelper;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import selenium.DomainHelper;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package ai.elimu.rest.v2.content;

import ai.elimu.util.JsonLoader;
import selenium.util.DomainHelper;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import selenium.DomainHelper;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package ai.elimu.rest.v2.content;

import ai.elimu.util.JsonLoader;
import selenium.util.DomainHelper;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import selenium.DomainHelper;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package ai.elimu.rest.v2.content;

import ai.elimu.util.JsonLoader;
import selenium.util.DomainHelper;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import selenium.DomainHelper;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package ai.elimu.rest.v2.content;

import ai.elimu.util.JsonLoader;
import selenium.util.DomainHelper;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import selenium.DomainHelper;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package ai.elimu.rest.v2.content;

import ai.elimu.util.JsonLoader;
import selenium.util.DomainHelper;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import selenium.DomainHelper;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package selenium.web;
package selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import selenium.ErrorHelper;
import selenium.util.ErrorHelper;

public class WelcomePage {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package selenium.web;
package selenium;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -9,7 +9,7 @@
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import selenium.DomainHelper;
import selenium.util.DomainHelper;

public class WelcomePageTest {

Expand All @@ -24,7 +24,7 @@ public void setUp() {
ChromeOptions chromeOptions = new ChromeOptions();

// Read "headless" property set on the command line:
// mvn clean verify -P regression-testing-ui -D headless=true -D base.url=https://eng.test.elimu.ai
// mvn clean verify -P regression-testing-ui -D headless=true
String headlessSystemProperty = System.getProperty("headless");
logger.info("headlessSystemProperty: \"" + headlessSystemProperty + "\"");
if ("true".equals(headlessSystemProperty)) {
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/selenium/analytics/MainAnalyticsPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package selenium.analytics;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import selenium.util.ErrorHelper;

public class MainAnalyticsPage {

private WebDriver driver;

public MainAnalyticsPage(WebDriver driver) {
this.driver = driver;

driver.findElement(By.id("mainAnalyticsPage"));

ErrorHelper.verifyNoScriptOrMarkupError(driver);
}

public void pressWordLearningEventsLink() {
WebElement link = driver.findElement(By.id("wordLearningEventsLink"));
link.click();
}
}
52 changes: 52 additions & 0 deletions src/test/java/selenium/analytics/MainAnalyticsPageTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package selenium.analytics;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import selenium.util.DomainHelper;

public class MainAnalyticsPageTest {

private final Logger logger = LogManager.getLogger();

private WebDriver driver;

@BeforeEach
public void setUp() {
logger.info("setUp");

ChromeOptions chromeOptions = new ChromeOptions();

// Read "headless" property set on the command line:
// mvn clean verify -P regression-testing-ui -D headless=true
String headlessSystemProperty = System.getProperty("headless");
logger.info("headlessSystemProperty: \"" + headlessSystemProperty + "\"");
if ("true".equals(headlessSystemProperty)) {
chromeOptions.addArguments("headless");
}

driver = new ChromeDriver(chromeOptions);

driver.get(DomainHelper.getBaseUrl() + "/analytics");
}

@AfterEach
public void tearDown() {
logger.info("tearDown");

driver.quit();
}

@Test
public void testMainAnalyticsPage() {
logger.info("testMainAnalyticsPage");

MainAnalyticsPage mainAnalyticsPage = new MainAnalyticsPage(driver);
}
}
19 changes: 19 additions & 0 deletions src/test/java/selenium/analytics/WordLearningEventsPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package selenium.analytics;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import selenium.util.ErrorHelper;

public class WordLearningEventsPage {

private WebDriver driver;

public WordLearningEventsPage(WebDriver driver) {
this.driver = driver;

driver.findElement(By.id("wordLearningEventsPage"));

ErrorHelper.verifyNoScriptOrMarkupError(driver);
}
}
Loading

0 comments on commit 691d177

Please sign in to comment.