Skip to content

Commit

Permalink
- Added implicit waits
Browse files Browse the repository at this point in the history
- Fixed logging
  • Loading branch information
yannisf committed Oct 25, 2021
1 parent 39f84d2 commit 7822ce7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 27 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<artifactId>selenium-chrome-driver</artifactId>
<version>${selenium-chrome-driver.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
<version>${selenium-chrome-driver.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
Expand Down
68 changes: 41 additions & 27 deletions src/main/java/eu/frlab/SchoolCard.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
package eu.frlab;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class SchoolCard {

private static final long WAIT_TIME = 1000;
private static final long MAX_WAIT_TIME = 3000;

private static final Logger LOG = LoggerFactory.getLogger(SchoolCard.class);

static {
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
}

public static void main(String[] args) throws Exception {
String home = System.getenv("HOME");
Path gsisPath = Paths.get(home + "/.gsis");
Expand All @@ -29,18 +39,6 @@ public static void main(String[] args) throws Exception {
LOG.info("Preparing COVID school card for [{}]", kid[0]);
SchoolCard schoolCard = new SchoolCard();
schoolCard.execute(gsis, kid);
System.exit(0);
}

private WebDriver getDriver() {
if (System.getenv("CHROME_DRIVER") != null)
System.setProperty("webdriver.chrome.driver", System.getenv("CHROME_DRIVER"));

System.setProperty("webdriver.chrome.silentOutput", "true");

SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
return new ChromeDriver();
}

private static void gsisCheck(Path gsisPath) {
Expand All @@ -64,43 +62,43 @@ private static void infoCheck(String[] kid) {
}
}

private void execute(String[] gsis, String[] kid) throws Exception {
private void execute(String[] gsis, String[] kid) {

WebDriver driver = getDriver();
FluentWait<WebDriver> wait = getWait(driver);

LOG.info("Open gov.gr COVID19 School Card web page");
String url = "https://covid19-self-test.services.gov.gr/templates/COVID19-SCHOOL-CARD2/create/";
driver.manage().window().maximize();
driver.get(url);
Thread.sleep(WAIT_TIME);

LOG.info("Accept cookies");
driver.findElement(By.xpath("//button[span[text()[contains(.,'Αποδοχή')]]]")).click();
Thread.sleep(WAIT_TIME);
By acceptCookiesControl = By.xpath("//button[span[text()[contains(.,'Αποδοχή')]]]");
wait.until(ExpectedConditions.elementToBeClickable(acceptCookiesControl));
driver.findElement(acceptCookiesControl).click();

LOG.info("Login on service");
driver.findElement(By.cssSelector("a[href='/api/login/']")).click();
Thread.sleep(WAIT_TIME);

LOG.info("Select login authenticator (GSIS)");
driver.findElement(By.xpath("//button[span[text()[contains(.,'ΓΓΠΣΔΔ')]]]")).click();
Thread.sleep(WAIT_TIME);

LOG.info("Enter credentials");
driver.findElement(By.name("j_username")).sendKeys(gsis[0]);
driver.findElement(By.name("j_password")).sendKeys(gsis[1]);
driver.findElement(By.id("btn-login-submit")).click();
Thread.sleep(WAIT_TIME);

LOG.info("Review GSIS data");
try {
driver.findElement(By.id("btn-submit")).click();
Thread.sleep(WAIT_TIME);
} catch (Exception ignored) {
LOG.info("Review GSIS data (skipped)");
}

LOG.info("Review personal information");
driver.findElement(By.cssSelector("button[label='Συνέχεια']")).click();
Thread.sleep(WAIT_TIME);
By reviewPersonalInfControl = By.cssSelector("button[label='Συνέχεια']");
wait.until(ExpectedConditions.elementToBeClickable(reviewPersonalInfControl));
driver.findElement(reviewPersonalInfControl).click();

LOG.info("Enter kids personal information");
driver.findElement(By.name("child_firstname")).sendKeys(kid[0]);
Expand All @@ -111,7 +109,6 @@ private void execute(String[] gsis, String[] kid) throws Exception {
driver.findElement(By.name("child_birth_date-month")).sendKeys(kid[5]);
driver.findElement(By.name("child_birth_date-year")).sendKeys(kid[6]);
driver.findElement(By.cssSelector("form")).submit();
Thread.sleep(WAIT_TIME);

LOG.info("Enter kids COVID test information");
LocalDate date = LocalDate.now();
Expand All @@ -123,12 +120,29 @@ private void execute(String[] gsis, String[] kid) throws Exception {
driver.findElement(By.cssSelector("#menu-test_result > div.MuiPaper-root.MuiMenu-paper.MuiPopover-paper.MuiPaper-elevation8.MuiPaper-rounded > ul > li:nth-child(1)")).click();

LOG.info("Submit form");
Thread.sleep(WAIT_TIME);
driver.findElement(By.cssSelector("form")).submit();

LOG.info("Request print");
Thread.sleep(WAIT_TIME);
driver.findElement(By.cssSelector("button[label='Εκτύπωση']")).click();
By printButton = By.cssSelector("button[label='Εκτύπωση']");
wait.until(ExpectedConditions.elementToBeClickable(printButton));
driver.findElement(printButton).click();
}

private WebDriver getDriver() {
if (System.getenv("CHROME_DRIVER") != null)
System.setProperty("webdriver.chrome.driver", System.getenv("CHROME_DRIVER"));

System.setProperty("webdriver.chrome.silentOutput", "true");

return new ChromeDriver();
}

private FluentWait<WebDriver> getWait(WebDriver driver) {
FluentWait<WebDriver> wait = new FluentWait<>(driver);
wait.withTimeout(Duration.of(MAX_WAIT_TIME, ChronoUnit.MILLIS));
wait.pollingEvery(Duration.of(100, ChronoUnit.MILLIS));
wait.ignoring(NoSuchElementException.class);
return wait;
}

}

0 comments on commit 7822ce7

Please sign in to comment.