From 19d8ee53126f23087504f508b501f281fa41af0c Mon Sep 17 00:00:00 2001 From: Mathieu Soysal <43273304+MathieuSoysal@users.noreply.github.com> Date: Wed, 26 Apr 2023 14:58:51 +0000 Subject: [PATCH] Add script to get html page --- .../java/io/github/mathieusoysal/App.java | 2 +- .../java/io/github/mathieusoysal/Roboto.java | 22 ++++++++++--------- .../io/github/mathieusoysal/RobotoTest.java | 13 ++++++++++- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/main/java/io/github/mathieusoysal/App.java b/src/main/java/io/github/mathieusoysal/App.java index dffb6be..7f515f0 100644 --- a/src/main/java/io/github/mathieusoysal/App.java +++ b/src/main/java/io/github/mathieusoysal/App.java @@ -17,7 +17,7 @@ public static void main(String[] args) { try (Roboto roboto = new Roboto()) { checkAllEnvVariables(ENV_NAME_MAIL, ENV_NAME_PASSWORD); roboto.connect(System.getenv(ENV_NAME_MAIL), System.getenv(ENV_NAME_PASSWORD)); - while (!roboto.checkinButtonIsPresent()) + while (roboto.checkinButtonIsNotPresent()) roboto.refreshPage(); roboto.clickOnSubscription(); } catch (RobotoException e) { diff --git a/src/main/java/io/github/mathieusoysal/Roboto.java b/src/main/java/io/github/mathieusoysal/Roboto.java index 4c4d1fd..687227a 100644 --- a/src/main/java/io/github/mathieusoysal/Roboto.java +++ b/src/main/java/io/github/mathieusoysal/Roboto.java @@ -42,6 +42,8 @@ public class Roboto implements AutoCloseable { private boolean telemetry = true; + private String pageContent = ""; + public Roboto(final String admissionURL, final boolean telemetry) { this.telemetry = telemetry; playwright = Playwright.create(); @@ -81,10 +83,10 @@ public void connect(String email, String password) fillPasswordField(password); clickSubmitButton(); page.waitForLoadState(); - String stateAfterConnection = page.url(); - if (stateBeforeConnection.equals(stateAfterConnection)) { + String currentState = page.url(); + if (stateBeforeConnection.equals(currentState)) { trace = "RefusedConnection"; - LOGGER.error(() -> "Connection refused"); + LOGGER.error(() -> "Connection refused check your credentials"); throw new RefusedConnectionException(); } LOGGER.info(() -> "Roboto is signed in to " + admissionURL); @@ -95,11 +97,11 @@ public void refreshPage() { LOGGER.info(() -> "Page refreshed"); } - public boolean checkinButtonIsPresent() { + public boolean checkinButtonIsNotPresent() { page.waitForLoadState(); - return page.locator( - "input[type='submit'] :not([value='Enregistrement impossible']) :not([value='Can not subscribe'])") - .last().isVisible(); + if (pageContent.equals("")) + pageContent = page.content(); + return pageContent.equals(page.content()); } public void subcribeToCheckIn() { @@ -171,9 +173,9 @@ void validateCaptcha() { } void clickOnSubscription() { - page.locator("input[type='submit'] :not([value='Enregistrement impossible']) :not([value='Can not subscribe'])") - .last().click(); - LOGGER.info(() -> "Subscription clicked"); + // page.locator("input[type='submit'] :not([value='Enregistrement impossible']) + // :not([value='Can not subscribe'])").last().click(); + // LOGGER.info(() -> "Subscription clicked"); // TODO: Add a check to see if the subscription is successful } diff --git a/src/test/java/io/github/mathieusoysal/RobotoTest.java b/src/test/java/io/github/mathieusoysal/RobotoTest.java index c136b7f..7c7cea5 100644 --- a/src/test/java/io/github/mathieusoysal/RobotoTest.java +++ b/src/test/java/io/github/mathieusoysal/RobotoTest.java @@ -6,6 +6,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; @@ -111,7 +112,7 @@ void checkinButtonIsPresent_test() throws EmailFieldNotFoundException, PasswordF ConnectionButtonNotFoundException, RefusedConnectionException { Roboto roboto = new Roboto(false); roboto.connect(System.getenv("TEST_EMAIL"), System.getenv("TEST_PASSWORD")); - assertFalse(roboto.checkinButtonIsPresent()); + assertFalse(roboto.checkinButtonIsNotPresent()); roboto.close(); // TODO: find a way to test the checkin button } @@ -126,4 +127,14 @@ void captachaTest() throws EmailFieldNotFoundException, PasswordFieldNotFoundExc // TODO: find a way to test the captcha } + @Test + void testCheckinButtonIsNotPresent() throws EmailFieldNotFoundException, PasswordFieldNotFoundException, + ConnectionButtonNotFoundException, RefusedConnectionException { + Roboto roboto = new Roboto(false); + roboto.getPage().setDefaultTimeout(1000); + roboto.connect(System.getenv("TEST_EMAIL"), System.getenv("TEST_PASSWORD")); + assertTrue(roboto.checkinButtonIsNotPresent()); + assertTrue(roboto.checkinButtonIsNotPresent()); + } + }