Skip to content

Commit

Permalink
Adding custom setDate method to solve sessionTest flakiness closes #132
Browse files Browse the repository at this point in the history
Uses sendKeys, if it doesn't work tries with a JS executor to submit the proper date. Tries up to  3 times the prior strategy
  • Loading branch information
augustocristian committed Jul 28, 2024
1 parent b0b8f19 commit f9997f2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ stage('TEARDOWN-Infrastructure') {
}// EndStepsTearDownInf
}// EndStageTearDown
}// EndStagesPipeline
post {
post {
always {
archiveArtifacts artifacts: 'artifacts/*.csv', onlyIfSuccessful: true
archiveArtifacts artifacts: 'target/testlogs/**/*.*', onlyIfSuccessful: false
archiveArtifacts artifacts: 'target/containerlogs/**/*.*', onlyIfSuccessful: false
}//EndAlways
}//EndPostActions
}// EndPipeline
}// EndPipeline
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.junit.jupiter.api.TestInfo;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.support.ui.ExpectedConditions;
Expand All @@ -29,8 +30,7 @@

import static com.fullteaching.e2e.no_elastest.common.Constants.*;
import static java.lang.invoke.MethodHandles.lookup;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
import static org.openqa.selenium.logging.LogType.BROWSER;
import static org.slf4j.LoggerFactory.getLogger;

Expand Down Expand Up @@ -111,7 +111,7 @@ private void createNewSession(String sessionName) throws ElementNotFoundExceptio
WebElement modal = Wait.notTooMuch(user.getDriver()).until(ExpectedConditions.visibilityOfElementLocated(SESSION_LIST_NEW_SESSION_MODAL));
modal.findElement(SESSION_LIST_NEW_SESSION_MODAL_TITLE).sendKeys(sessionName);
modal.findElement(SESSION_LIST_NEW_SESSION_MODAL_CONTENT).sendKeys(sessionDescription);
modal.findElement(SESSION_LIST_NEW_SESSION_MODAL_DATE).sendKeys(sessionDate);
introduceSessionDate(user,modal,sessionDate);
modal.findElement(SESSION_LIST_NEW_SESSION_MODAL_TIME).sendKeys(sessionHour);
Click.element(user.getDriver(), modal.findElement(SESSION_LIST_NEW_SESSION_MODAL_POST_BUTTON));
Wait.notTooMuch(user.getDriver());
Expand All @@ -121,6 +121,35 @@ private void createNewSession(String sessionName) throws ElementNotFoundExceptio
List<String> session_titles = SessionNavigationUtilities.getFullSessionList(user.getDriver());
assertTrue(session_titles.contains(sessionName), "Session has not been created");
}

private void introduceSessionDate(BrowserUser user, WebElement element, String date) {
boolean success = false;
int attempts = 3;
log.info("The date is going to be:{}",date);
for (int i = 0; i < attempts; i++) {
try {
user.getDriver().findElement(SESSION_LIST_NEW_SESSION_MODAL_DATE).clear();
String[] splitDate = date.split("-");
user.getDriver().findElement(SESSION_LIST_NEW_SESSION_MODAL_DATE).sendKeys(date);
String invertedDate = splitDate[2] + "-" + splitDate[1] + "-" + splitDate[0];
String actualValue= user.getDriver().findElement(SESSION_LIST_NEW_SESSION_MODAL_DATE).getAttribute("value");
if(!actualValue.equals(invertedDate)) { // Use JavaScript to set the value of the date input
log.info("Selenium sendKeys dont working properly, using JS..");
user.getDriver().findElement(SESSION_LIST_NEW_SESSION_MODAL_DATE).clear();
JavascriptExecutor js = (JavascriptExecutor) user.getDriver();
js.executeScript("arguments[0].value = arguments[1];", user.getDriver().findElement(SESSION_LIST_NEW_SESSION_MODAL_DATE), invertedDate);
}
user.getWaiter().until(ExpectedConditions.textToBePresentInElementValue(element.findElement(SESSION_LIST_NEW_SESSION_MODAL_DATE), invertedDate));
success = true;
break; //
} catch (TimeoutException e) {
log.info("Attempt {} failed. Cleaning the dateChooser and retrying...", i + 1);
}
}
if (!success) {
throw new TimeoutException("Failed to set the date after " + attempts + " attempts.");
}
}
/**
* This method gets the current date in the format DDMMYYYY. for the video session name
*/
Expand All @@ -130,7 +159,7 @@ private String getCurrentDate() {
int mYear = calendar.get(Calendar.YEAR);
int mMonth = calendar.get(Calendar.MONTH);
int mDay = calendar.get(Calendar.DAY_OF_MONTH);
return "" + (mDay < 10 ? "0" + mDay : mDay) + (mMonth < 10 ? "0" + mMonth : mMonth) + mYear;
return "" + (mDay < 10 ? "0" + mDay : mDay)+"-" + (mMonth < 10 ? "0" + mMonth : mMonth)+"-"+ mYear;
}
/**
* This method gets the current time in the format HHmmA/P, where A/P indicates AM or PM for the video session name
Expand Down Expand Up @@ -240,7 +269,7 @@ void tearDown(TestInfo testInfo) {
log.info("##### Finish test: {} - Driver {}", TEST_NAME, this.user.getDriver());
log.info("Browser console at the end of the test");
LogEntries logEntries = user.getDriver().manage().logs().get(BROWSER);
logEntries.forEach((entry) -> log.info("[{}] {} {}",
logEntries.forEach(entry -> log.info("[{}] {} {}",
new Date(entry.getTimestamp()), entry.getLevel(),
entry.getMessage()));
if (user.isOnSession()) {
Expand Down

0 comments on commit f9997f2

Please sign in to comment.