generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
191 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.context.event.ApplicationReadyEvent; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDateTime; | ||
|
@@ -31,22 +32,29 @@ public class InsertSampleDataService { | |
private final CategoryService categoryService; | ||
private final QuestionRepository questionRepository; | ||
private final GameSessionRepository gameSessionRepository; | ||
private Environment environment; | ||
|
||
private Logger log = LoggerFactory.getLogger(InsertSampleDataService.class);; | ||
|
||
public InsertSampleDataService(PlayerService playerService, QuestionService questionService, | ||
CategoryService categoryService, QuestionRepository questionRepository, | ||
GameSessionRepository gameSessionRepository) { | ||
GameSessionRepository gameSessionRepository, Environment environment) { | ||
this.playerService = playerService; | ||
this.questionService = questionService; | ||
this.categoryService = categoryService; | ||
this.questionRepository = questionRepository; | ||
this.gameSessionRepository = gameSessionRepository; | ||
this.environment = environment; | ||
} | ||
|
||
@Transactional | ||
@EventListener(ApplicationReadyEvent.class) // Uncomment this line to insert sample data on startup | ||
public void insertSampleQuestions() { | ||
if (Arrays.stream(environment.getActiveProfiles()).anyMatch(env -> (env.equalsIgnoreCase("test")))) { | ||
log.info("Test profile active, skipping sample data insertion"); | ||
return; | ||
} | ||
|
||
if (!playerService.getUserByEmail("[email protected]").isPresent()) { | ||
PlayerDto player = new PlayerDto(); | ||
player.setEmail("[email protected]"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,51 @@ | ||
package com.uniovi; | ||
|
||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.Test; | ||
import io.github.bonigarcia.wdm.WebDriverManager; | ||
import org.junit.jupiter.api.*; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.chrome.ChromeDriver; | ||
import org.openqa.selenium.firefox.FirefoxDriver; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
@SpringBootTest | ||
@Tag("integration") | ||
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) | ||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) | ||
@ActiveProfiles("test") | ||
class Wiq_IntegrationTests { | ||
static final String URL = "http://localhost:3000/"; | ||
|
||
@Test | ||
void contextLoads() { | ||
static WebDriver driver; | ||
|
||
@BeforeAll | ||
static public void begin() { | ||
WebDriverManager.firefoxdriver().setup(); | ||
driver = new FirefoxDriver(); | ||
driver.navigate().to(URL); | ||
} | ||
|
||
@BeforeEach | ||
void setup() { | ||
driver.navigate().to(URL); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
driver.manage().deleteAllCookies(); | ||
} | ||
|
||
@AfterAll | ||
static public void end() { | ||
//Cerramos el navegador al finalizar las pruebas | ||
driver.quit(); | ||
} | ||
|
||
@Test | ||
@Order(1) | ||
void testHome() { | ||
// Check the title | ||
Assertions.assertEquals("Wikigame", driver.getTitle()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package com.uniovi.util; | ||
|
||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.WebElement; | ||
import org.openqa.selenium.support.ui.ExpectedConditions; | ||
import org.openqa.selenium.support.ui.WebDriverWait; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
|
||
public class SeleniumUtils { | ||
/** | ||
* Aborta si el "texto" no está presente en la página actual | ||
* @param driver: apuntando al navegador abierto actualmente. | ||
* @param text: texto a buscar | ||
*/ | ||
static public void textIsPresentOnPage(WebDriver driver, String text) | ||
{ | ||
List<WebElement> list = driver.findElements(By.xpath("//*[contains(text(),'" + text + "')]")); | ||
Assertions.assertTrue(list.size() > 0, "Texto " + text + " no localizado!"); | ||
} | ||
|
||
/** | ||
* Aborta si el "texto" está presente en la página actual | ||
* @param driver: apuntando al navegador abierto actualmente. | ||
* @param text: texto a buscar | ||
*/ | ||
static public void textIsNotPresentOnPage(WebDriver driver, String text) | ||
{ | ||
List<WebElement> list = driver.findElements(By.xpath("//*[contains(text(),'" + text + "')]")); | ||
Assertions.assertEquals(0, list.size(), "Texto " + text + " no está presente !"); | ||
} | ||
|
||
/** | ||
* Aborta si el "texto" está presente en la página actual tras timeout segundos. | ||
* @param driver: apuntando al navegador abierto actualmente. | ||
* @param text: texto a buscar | ||
* @param timeout: el tiempo máximo que se esperará por la aparición del texto a buscar | ||
*/ | ||
static public void waitTextIsNotPresentOnPage(WebDriver driver, String text, int timeout) | ||
{ | ||
Boolean resultado = | ||
(new WebDriverWait(driver, Duration.ofSeconds(timeout))).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//*[contains(text(),'" + text + "')]"))); | ||
|
||
Assertions.assertTrue(resultado); | ||
} | ||
|
||
|
||
/** | ||
* Espera por la visibilidad de un elemento/s en la vista actualmente cargandose en driver. Para ello se empleará una consulta xpath. | ||
* @param driver: apuntando al navegador abierto actualmente. | ||
* @param xpath: consulta xpath. | ||
* @param timeout: el tiempo máximo que se esperará por la aparición del elemento a buscar con xpath | ||
* @return Se retornará la lista de elementos resultantes de la búsqueda con xpath. | ||
*/ | ||
static public List<WebElement> waitLoadElementsByXpath(WebDriver driver, String xpath, int timeout) | ||
{ | ||
WebElement result = | ||
(new WebDriverWait(driver, Duration.ofSeconds(timeout))).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath))); | ||
Assertions.assertNotNull(result); | ||
return driver.findElements(By.xpath(xpath)); | ||
} | ||
|
||
/** | ||
* Espera por la visibilidad de un elemento/s en la vista actualmente cargandose en driver. Para ello se empleará una consulta xpath | ||
* según varios criterios.. | ||
* | ||
* @param driver: apuntando al navegador abierto actualmente. | ||
* @param criterio: "id" or "class" or "text" or "@attribute" or "free". Si el valor de criterio es free es una expresion xpath completa. | ||
* @param text: texto correspondiente al criterio. | ||
* @param timeout: el tiempo máximo que se esperará por la apareción del elemento a buscar con criterio/text. | ||
* @return Se retornará la lista de elementos resultantes de la búsqueda. | ||
*/ | ||
static public List<WebElement> waitLoadElementsBy(WebDriver driver, String criterio, String text, int timeout) | ||
{ | ||
String searchCriterio; | ||
switch (criterio) { | ||
case "id": | ||
searchCriterio = "//*[contains(@id,'" + text + "')]"; | ||
break; | ||
case "class": | ||
searchCriterio = "//*[contains(@class,'" + text + "')]"; | ||
break; | ||
case "text": | ||
searchCriterio = "//*[contains(text(),'" + text + "')]"; | ||
break; | ||
case "free": | ||
searchCriterio = text; | ||
break; | ||
default: | ||
searchCriterio = "//*[contains(" + criterio + ",'" + text + "')]"; | ||
break; | ||
} | ||
|
||
return waitLoadElementsByXpath(driver, searchCriterio, timeout); | ||
} | ||
|
||
|
||
/** | ||
* PROHIBIDO USARLO PARA VERSIÓN FINAL. | ||
* Esperar "segundos" durante la ejecucion del navegador | ||
* @param driver: apuntando al navegador abierto actualmente. | ||
* @param seconds: Segundos de bloqueo de la ejecución en el navegador. | ||
*/ | ||
static public void waitSeconds(WebDriver driver, int seconds){ | ||
|
||
//noinspection SynchronizationOnLocalVariableOrMethodParameter | ||
synchronized(driver){ | ||
try { | ||
driver.wait(seconds * 1000L); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} |