From 70d9d73a15f5e83115b5e30b2e58bb9f27abcda9 Mon Sep 17 00:00:00 2001 From: Igor Bari Date: Tue, 17 Oct 2023 08:01:12 +0000 Subject: [PATCH] Add strava tests --- .../core/WebDriverConfiguration.java | 2 +- test/docker-compose.js | 11 +- test/mock_strava/activities.js | 141 +++--------------- .../workout/test/BaseIntegrationTest.java | 2 + .../github/mucsi96/workout/test/RideTest.java | 78 +++++----- .../mucsi96/workout/test/StravaTest.java | 46 ++++++ .../mucsi96/workout/test/WeightTest.java | 54 ++++--- .../mucsi96/workout/test/WithingsTest.java | 17 ++- 8 files changed, 170 insertions(+), 181 deletions(-) create mode 100644 test/src/test/java/io/github/mucsi96/workout/test/StravaTest.java diff --git a/server/src/main/java/mucsi96/traininglog/core/WebDriverConfiguration.java b/server/src/main/java/mucsi96/traininglog/core/WebDriverConfiguration.java index d382b4f..36bd89e 100644 --- a/server/src/main/java/mucsi96/traininglog/core/WebDriverConfiguration.java +++ b/server/src/main/java/mucsi96/traininglog/core/WebDriverConfiguration.java @@ -39,7 +39,7 @@ public WebDriver getLocalWebDriver() { public WebDriver getRemoteWebDriver() throws MalformedURLException { ChromeOptions options = new ChromeOptions().addArguments("--headless", "--disable-gpu", "--no-sandbox", "--disable-dev-shm-usage", - "--window-size=1920,1080", "--remote-allow-origins=*"); + "--remote-allow-origins=*"); RemoteWebDriver driver = new RemoteWebDriver(new URL(apiUri), options, false); driver.setLogLevel(Level.WARNING); diff --git a/test/docker-compose.js b/test/docker-compose.js index d08febe..7714af8 100644 --- a/test/docker-compose.js +++ b/test/docker-compose.js @@ -46,7 +46,7 @@ const config = { SPRING_ACTUATOR_PORT: 8082, SPRING_ADMIN_SERVER_HOST: "localhost", SPRING_ADMIN_SERVER_PORT: 9090, - WEBDRIVER_API_URI: "http://chrome:4444/wd/hub", + WEBDRIVER_API_URI: "http://chrome:4444/", WITHINGS_ACCOUNTS_URI: `http://${ dockerNetwork ? "reverse-proxy" : "localhost:9780" }/withings`, @@ -83,6 +83,15 @@ const config = { arch === "arm64" ? "seleniarm/standalone-chromium:116.0-chromedriver-116.0-grid-4.10.0-20230828" : "selenium/standalone-chrome:116.0-chromedriver-116.0-grid-4.12.1-20230912", + environment: { + SE_SCREEN_WIDTH: 1920, + SE_SCREEN_HEIGHT: 1080, + SE_SESSION_RETRY_INTERVAL: 1, + SE_NODE_MAX_SESSIONS: 10, + SE_NODE_OVERRIDE_MAX_SESSIONS: true, + START_XVFB: false, + SE_DRAIN_AFTER_SESSION_COUNT: 5, + }, healthcheck: { test: [ "CMD", diff --git a/test/mock_strava/activities.js b/test/mock_strava/activities.js index f708dbc..a8774d2 100644 --- a/test/mock_strava/activities.js +++ b/test/mock_strava/activities.js @@ -1,3 +1,8 @@ +const { getSearchParams } = require("./request"); + +let activity1Date = new Date(); +let activity2Date = new Date(); + function getActivities(request, response) { const authorization = request.headers.authorization; if (authorization !== "Bearer test-access-token") { @@ -7,6 +12,14 @@ function getActivities(request, response) { return; } + const searchParams = getSearchParams(request); + const after = parseInt(searchParams.get("after")); + const before = parseInt(searchParams.get("before")); + activity1Date = new Date(1000 * (after + (before - after) / 2)); + activity1Date.setUTCHours(12, 35); + activity2Date = new Date(1000 * (after + (before - after) / 2)); + activity2Date.setUTCHours(12, 45); + response.writeHead(200, { "Content-Type": "application/json", }); @@ -163,8 +176,12 @@ function getActivity(request, response) { total_elevation_gain: 516, type: "Ride", sport_type: "MountainBikeRide", - start_date: "2018-02-16T14:52:54Z", - start_date_local: "2018-02-16T06:52:54Z", + start_date: request.url.endsWith("1") + ? activity1Date.toISOString() + : activity2Date.toISOString(), + start_date_local: request.url.endsWith("1") + ? activity1Date.toISOString() + : activity2Date.toISOString(), timezone: "(GMT-08:00) America/Los_Angeles", utc_offset: -28800, start_latlng: [37.83, -122.26], @@ -174,14 +191,6 @@ function getActivity(request, response) { comment_count: 0, athlete_count: 1, photo_count: 0, - map: { - id: "a1410355832", - polyline: - "ki{eFvqfiVqAWQIGEEKAYJgBVqDJ{BHa@j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HnCvBdA", - }, trainer: false, commute: false, manual: false, @@ -208,96 +217,6 @@ function getActivity(request, response) { suffer_score: null, description: "", calories: 870.2, - segment_efforts: [ - { - id: 12345678987654321, - resource_state: 2, - name: "Tunnel Rd.", - activity: { - id: 12345678987654321, - resource_state: 1, - }, - athlete: { - id: 134815, - resource_state: 1, - }, - elapsed_time: 2038, - moving_time: 2038, - start_date: "2018-02-16T14:56:25Z", - start_date_local: "2018-02-16T06:56:25Z", - distance: 9434.8, - start_index: 211, - end_index: 2246, - average_cadence: 78.6, - device_watts: true, - average_watts: 237.6, - segment: { - id: 673683, - resource_state: 2, - name: "Tunnel Rd.", - activity_type: "Ride", - distance: 9220.7, - average_grade: 4.2, - maximum_grade: 25.8, - elevation_high: 426.5, - elevation_low: 43.4, - start_latlng: [37.8346153, -122.2520872], - end_latlng: [37.8476261, -122.2008944], - climb_category: 3, - city: "Oakland", - state: "CA", - country: "United States", - private: false, - hazardous: false, - starred: false, - }, - kom_rank: null, - pr_rank: null, - achievements: [], - hidden: false, - }, - ], - splits_metric: [ - { - distance: 1001.5, - elapsed_time: 141, - elevation_difference: 4.4, - moving_time: 141, - split: 1, - average_speed: 7.1, - pace_zone: 0, - }, - ], - laps: [ - { - id: 4479306946, - resource_state: 2, - name: "Lap 1", - activity: { - id: 1410355832, - resource_state: 1, - }, - athlete: { - id: 134815, - resource_state: 1, - }, - elapsed_time: 1573, - moving_time: 1569, - start_date: "2018-02-16T14:52:54Z", - start_date_local: "2018-02-16T06:52:54Z", - distance: 8046.72, - start_index: 0, - end_index: 1570, - total_elevation_gain: 276, - average_speed: 5.12, - max_speed: 9.5, - average_cadence: 78.6, - device_watts: true, - average_watts: 233.1, - lap_index: 1, - split: 1, - }, - ], gear: { id: "b12345678987654321", primary: true, @@ -306,28 +225,6 @@ function getActivity(request, response) { distance: 32547610, }, partner_brand_tag: null, - photos: { - primary: { - id: null, - unique_id: "3FDGKL3-204E-4867-9E8D-89FC79EAAE17", - urls: { - 100: "https://dgtzuqphqg23d.cloudfront.net/Bv93zv5t_mr57v0wXFbY_JyvtucgmU5Ym6N9z_bKeUI-128x96.jpg", - 600: "https://dgtzuqphqg23d.cloudfront.net/Bv93zv5t_mr57v0wXFbY_JyvtucgmU5Ym6N9z_bKeUI-768x576.jpg", - }, - source: 1, - }, - use_primary_photo: true, - count: 2, - }, - highlighted_kudosers: [ - { - destination_url: "strava://athletes/12345678987654321", - display_name: "Marianne V.", - avatar_url: - "https://dgalywyr863hv.cloudfront.net/pictures/athletes/12345678987654321/12345678987654321/3/medium.jpg", - show_name: true, - }, - ], hide_from_home: false, device_name: "Garmin Edge 1030", embed_token: "18e4615989b47dd4ff3dc711b0aa4502e4b311a9", diff --git a/test/src/test/java/io/github/mucsi96/workout/test/BaseIntegrationTest.java b/test/src/test/java/io/github/mucsi96/workout/test/BaseIntegrationTest.java index 5a62a74..12934c6 100644 --- a/test/src/test/java/io/github/mucsi96/workout/test/BaseIntegrationTest.java +++ b/test/src/test/java/io/github/mucsi96/workout/test/BaseIntegrationTest.java @@ -34,9 +34,11 @@ public class BaseIntegrationTest { JdbcTemplate jdbcTemplate; WebDriverWait wait; + WebDriverWait longWait; public void setupMocks() { wait = new WebDriverWait(webDriver, Duration.ofSeconds(5)); + longWait = new WebDriverWait(webDriver, Duration.ofSeconds(15)); cleanupDB(); jdbcTemplate.execute("INSERT INTO oauth2_authorized_client (" + // "client_registration_id," + // diff --git a/test/src/test/java/io/github/mucsi96/workout/test/RideTest.java b/test/src/test/java/io/github/mucsi96/workout/test/RideTest.java index 7c5b715..5085eda 100644 --- a/test/src/test/java/io/github/mucsi96/workout/test/RideTest.java +++ b/test/src/test/java/io/github/mucsi96/workout/test/RideTest.java @@ -10,6 +10,7 @@ import org.junit.jupiter.api.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.ui.ExpectedConditions; class RideTest extends BaseIntegrationTest { @@ -169,38 +170,42 @@ void beforeEach() { """, Timestamp.from(Instant.now().minus(1, ChronoUnit.DAYS)))); } - // @Test - // void display_todays_weight() { - // open(); - // WebElement weighElement = webDriver - // .findElement(By.xpath("//h2[contains(text(), - // \"Weight\")]/following-sibling::*")); - // assertThat(weighElement.getText()).isEqualToIgnoringWhitespace("87.2 kg"); - // WebElement bodyFatElement = webDriver - // .findElement(By.xpath("//h2[contains(text(), \"Body - // fat\")]/following-sibling::*")); - // assertThat(bodyFatElement.getText()).isEqualToIgnoringWhitespace("21.8 kg"); - // WebElement fatRatioElement = webDriver - // .findElement(By.xpath("//h2[contains(text(), \"Fat - // ratio\")]/following-sibling::*")); - // assertThat(fatRatioElement.getText()).isEqualToIgnoringWhitespace("35.3 %"); - // } + @Test + void display_todays_ride_stats() { + open(); + wait.until(ExpectedConditions + .visibilityOfElementLocated(By.xpath("//h2[contains(text(),\"Calories\")]"))); + WebElement caloriesElement = webDriver + .findElement(By.xpath("//h2[contains(text(),\"Calories\")]/following-sibling::*")); + assertThat(caloriesElement.getText()).isEqualTo("1 740"); + WebElement elevationGainElement = webDriver + .findElement(By.xpath("//h2[contains(text(), \"Elevation gain\")]/following-sibling::*")); + assertThat(elevationGainElement.getText()).isEqualTo("1 032 m"); + WebElement distanceElement = webDriver + .findElement(By.xpath("//h2[contains(text(), \"Distance\")]/following-sibling::*")); + assertThat(distanceElement.getText()).isEqualTo("56 km"); + WebElement timeElement = webDriver + .findElement(By.xpath("//h2[contains(text(), \"Time\")]/following-sibling::*")); + assertThat(timeElement.getText()).isEqualTo("2 h 20 min"); + } @Test void display_ride_stats_for_week() { open(); + wait.until(ExpectedConditions + .visibilityOfElementLocated(By.xpath("//h2[contains(text(),\"Calories\")]"))); WebElement caloriesElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Calories\")]/following-sibling::*/following-sibling::*")); - assertThat(caloriesElement.getText()).isEqualToIgnoringWhitespace("1 938"); + assertThat(caloriesElement.getText()).isEqualTo("3 678"); WebElement elevationGainElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Elevation gain\")]/following-sibling::*/following-sibling::*")); - assertThat(elevationGainElement.getText()).isEqualToIgnoringWhitespace("1 224 m"); + assertThat(elevationGainElement.getText()).isEqualTo("2 256 m"); WebElement distanceElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Distance\")]/following-sibling::*/following-sibling::*")); - assertThat(distanceElement.getText()).isEqualToIgnoringWhitespace("35 km"); + assertThat(distanceElement.getText()).isEqualTo("91 km"); WebElement timeElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Time\")]/following-sibling::*/following-sibling::*")); - assertThat(timeElement.getText()).isEqualToIgnoringWhitespace("2 h 34 min"); + assertThat(timeElement.getText()).isEqualTo("4 h 54 min"); } @Test @@ -211,19 +216,20 @@ void display_ride_stats_for_month() { By.xpath("//a[contains(text(), \"Month\")]")); button.click(); waitForLoad(); - + wait.until(ExpectedConditions + .visibilityOfElementLocated(By.xpath("//h2[contains(text(),\"Calories\")]"))); WebElement caloriesElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Calories\")]/following-sibling::*/following-sibling::*")); - assertThat(caloriesElement.getText()).isEqualToIgnoringWhitespace("2 584"); + assertThat(caloriesElement.getText()).isEqualTo("4 324"); WebElement elevationGainElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Elevation gain\")]/following-sibling::*/following-sibling::*")); - assertThat(elevationGainElement.getText()).isEqualToIgnoringWhitespace("1 632 m"); + assertThat(elevationGainElement.getText()).isEqualTo("2 664 m"); WebElement distanceElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Distance\")]/following-sibling::*/following-sibling::*")); - assertThat(distanceElement.getText()).isEqualToIgnoringWhitespace("47 km"); + assertThat(distanceElement.getText()).isEqualTo("103 km"); WebElement timeElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Time\")]/following-sibling::*/following-sibling::*")); - assertThat(timeElement.getText()).isEqualToIgnoringWhitespace("3 h 25 min"); + assertThat(timeElement.getText()).isEqualTo("5 h 45 min"); } @Test @@ -234,19 +240,20 @@ void display_ride_stats_for_year() { By.xpath("//a[contains(text(), \"Year\")]")); button.click(); waitForLoad(); - + wait.until(ExpectedConditions + .visibilityOfElementLocated(By.xpath("//h2[contains(text(),\"Calories\")]"))); WebElement caloriesElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Calories\")]/following-sibling::*/following-sibling::*")); - assertThat(caloriesElement.getText()).isEqualToIgnoringWhitespace("3 230"); + assertThat(caloriesElement.getText()).isEqualTo("4 970"); WebElement elevationGainElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Elevation gain\")]/following-sibling::*/following-sibling::*")); - assertThat(elevationGainElement.getText()).isEqualToIgnoringWhitespace("2 040 m"); + assertThat(elevationGainElement.getText()).isEqualTo("3 072 m"); WebElement distanceElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Distance\")]/following-sibling::*/following-sibling::*")); - assertThat(distanceElement.getText()).isEqualToIgnoringWhitespace("59 km"); + assertThat(distanceElement.getText()).isEqualTo("115 km"); WebElement timeElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Time\")]/following-sibling::*/following-sibling::*")); - assertThat(timeElement.getText()).isEqualToIgnoringWhitespace("4 h 16 min"); + assertThat(timeElement.getText()).isEqualTo("6 h 36 min"); } @Test @@ -257,19 +264,20 @@ void display_ride_stats_for_all_time() { By.xpath("//a[contains(text(), \"All time\")]")); button.click(); waitForLoad(); - + wait.until(ExpectedConditions + .visibilityOfElementLocated(By.xpath("//h2[contains(text(),\"Calories\")]"))); WebElement caloriesElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Calories\")]/following-sibling::*/following-sibling::*")); - assertThat(caloriesElement.getText()).isEqualToIgnoringWhitespace("4746"); + assertThat(caloriesElement.getText()).isEqualTo("5 616"); WebElement elevationGainElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Elevation gain\")]/following-sibling::*/following-sibling::*")); - assertThat(elevationGainElement.getText()).isEqualToIgnoringWhitespace("2 964 m"); + assertThat(elevationGainElement.getText()).isEqualTo("3 480 m"); WebElement distanceElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Distance\")]/following-sibling::*/following-sibling::*")); - assertThat(distanceElement.getText()).isEqualToIgnoringWhitespace("99 km"); + assertThat(distanceElement.getText()).isEqualTo("127 km"); WebElement timeElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Time\")]/following-sibling::*/following-sibling::*")); - assertThat(timeElement.getText()).isEqualToIgnoringWhitespace("6 h 18 min"); + assertThat(timeElement.getText()).isEqualTo("7 h 28 min"); } } diff --git a/test/src/test/java/io/github/mucsi96/workout/test/StravaTest.java b/test/src/test/java/io/github/mucsi96/workout/test/StravaTest.java new file mode 100644 index 0000000..2f53e78 --- /dev/null +++ b/test/src/test/java/io/github/mucsi96/workout/test/StravaTest.java @@ -0,0 +1,46 @@ +package io.github.mucsi96.workout.test; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.ui.ExpectedConditions; + +public class StravaTest extends BaseIntegrationTest { + + @Test + void authorizes_strava() { + setupMocks(); + jdbcTemplate.execute("DELETE FROM oauth2_authorized_client WHERE client_registration_id = 'strava-client';"); + webDriver.get(baseUrl); + wait.until(ExpectedConditions + .visibilityOfElementLocated(By.xpath("//h1[contains(text(), \"Mock Strava\")]"))); + webDriver.findElement(By.xpath("//a[contains(text(), \"Authorize\")]")).click(); + longWait.until(ExpectedConditions.urlToBe(baseUrl + "/week")); + String userName = jdbcTemplate.queryForObject( + "SELECT principal_name FROM oauth2_authorized_client WHERE client_registration_id = 'strava-client';", + String.class); + assertThat(userName).isEqualTo("rob"); + } + + @Test + void pulls_todays_ride_stats_from_strava_to_db() { + setupMocks(); + open(); + wait.until(ExpectedConditions + .visibilityOfElementLocated(By.xpath("//h2[contains(text(),\"Calories\")]"))); + WebElement caloriesElement = webDriver + .findElement(By.xpath("//h2[contains(text(),\"Calories\")]/following-sibling::*")); + assertThat(caloriesElement.getText()).isEqualTo("1 740"); + WebElement elevationGainElement = webDriver + .findElement(By.xpath("//h2[contains(text(), \"Elevation gain\")]/following-sibling::*")); + assertThat(elevationGainElement.getText()).isEqualTo("1 032 m"); + WebElement distanceElement = webDriver + .findElement(By.xpath("//h2[contains(text(), \"Distance\")]/following-sibling::*")); + assertThat(distanceElement.getText()).isEqualTo("56 km"); + WebElement timeElement = webDriver + .findElement(By.xpath("//h2[contains(text(), \"Time\")]/following-sibling::*")); + assertThat(timeElement.getText()).isEqualTo("2 h 20 min"); + } +} diff --git a/test/src/test/java/io/github/mucsi96/workout/test/WeightTest.java b/test/src/test/java/io/github/mucsi96/workout/test/WeightTest.java index 301a138..3c4a7b6 100644 --- a/test/src/test/java/io/github/mucsi96/workout/test/WeightTest.java +++ b/test/src/test/java/io/github/mucsi96/workout/test/WeightTest.java @@ -19,56 +19,68 @@ void beforeEach() { setupMocks(); jdbcTemplate.execute("DELETE FROM weight;"); jdbcTemplate.execute( - String.format("INSERT INTO weight (created_at, weight, fat_ratio, fat_mass_weight) VALUES ('%s', 108.9, 35.4, 38.6);", + String.format( + "INSERT INTO weight (created_at, weight, fat_ratio, fat_mass_weight) VALUES ('%s', 108.9, 35.4, 38.6);", Timestamp.from(Instant.now().minus(400, ChronoUnit.DAYS)))); jdbcTemplate - .execute(String.format("INSERT INTO weight (created_at, weight, fat_ratio, fat_mass_weight) VALUES ('%s', 98, 35.2, 34.5);", + .execute(String.format( + "INSERT INTO weight (created_at, weight, fat_ratio, fat_mass_weight) VALUES ('%s', 98, 35.2, 34.5);", Timestamp.from(Instant.now().minus(355, ChronoUnit.DAYS)))); jdbcTemplate.execute( - String.format("INSERT INTO weight (created_at, weight, fat_ratio, fat_mass_weight) VALUES ('%s', 89.4, 34.5, 30.8);", + String.format( + "INSERT INTO weight (created_at, weight, fat_ratio, fat_mass_weight) VALUES ('%s', 89.4, 34.5, 30.8);", Timestamp.from(Instant.now().minus(14, ChronoUnit.DAYS)))); jdbcTemplate.execute( - String.format("INSERT INTO weight (created_at, weight, fat_ratio, fat_mass_weight) VALUES ('%s', 88.3, 34.2, 30.2);", + String.format( + "INSERT INTO weight (created_at, weight, fat_ratio, fat_mass_weight) VALUES ('%s', 88.3, 34.2, 30.2);", Timestamp.from(Instant.now().minus(6, ChronoUnit.DAYS)))); jdbcTemplate.execute( - String.format("INSERT INTO weight (created_at, weight, fat_ratio, fat_mass_weight) VALUES ('%s', 87.7, 33.2, 29.1);", + String.format( + "INSERT INTO weight (created_at, weight, fat_ratio, fat_mass_weight) VALUES ('%s', 87.7, 33.2, 29.1);", Timestamp.from(Instant.now().minus(5, ChronoUnit.DAYS)))); jdbcTemplate.execute( - String.format("INSERT INTO weight (created_at, weight, fat_ratio, fat_mass_weight) VALUES ('%s', 87.5, 32.8 , 29.0);", + String.format( + "INSERT INTO weight (created_at, weight, fat_ratio, fat_mass_weight) VALUES ('%s', 87.5, 32.8 , 29.0);", Timestamp.from(Instant.now().minus(1, ChronoUnit.DAYS)))); } @Test void display_todays_weight() { open(); + wait.until(ExpectedConditions + .visibilityOfElementLocated(By.xpath("//h2[contains(text(),\"Weight\")]"))); WebElement weighElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Weight\")]/following-sibling::*")); - assertThat(weighElement.getText()).isEqualToIgnoringWhitespace("87.2 kg"); + assertThat(weighElement.getText()).isEqualTo("87.2 kg"); WebElement bodyFatElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Body fat\")]/following-sibling::*")); - assertThat(bodyFatElement.getText()).isEqualToIgnoringWhitespace("21.8 kg"); + assertThat(bodyFatElement.getText()).isEqualTo("21.8 kg"); WebElement fatRatioElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Fat ratio\")]/following-sibling::*")); - assertThat(fatRatioElement.getText()).isEqualToIgnoringWhitespace("35.3 %"); + assertThat(fatRatioElement.getText()).isEqualTo("35.3 %"); } @Test void display_weight_diff_for_week() { open(); + wait.until(ExpectedConditions + .visibilityOfElementLocated(By.xpath("//h2[contains(text(),\"Weight\")]"))); WebElement weighElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Weight\")]/following-sibling::*/following-sibling::*")); - assertThat(weighElement.getText()).isEqualToIgnoringWhitespace("↓ 1.2 %"); + assertThat(weighElement.getText()).isEqualTo("↓ 1.2 %"); WebElement bodyFatElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Body fat\")]/following-sibling::*/following-sibling::*")); - assertThat(bodyFatElement.getText()).isEqualToIgnoringWhitespace("↓ 27.8 %"); + assertThat(bodyFatElement.getText()).isEqualTo("↓ 27.8 %"); WebElement fatRatioElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Fat ratio\")]/following-sibling::*/following-sibling::*")); - assertThat(fatRatioElement.getText()).isEqualToIgnoringWhitespace("↑ 3.2 %"); + assertThat(fatRatioElement.getText()).isEqualTo("↑ 3.2 %"); } @Test void display_weight_chart_for_week() { open(); + wait.until(ExpectedConditions + .visibilityOfElementLocated(By.xpath("//h2[contains(text(),\"Weight\")]"))); WebElement chart = webDriver .findElement(By.xpath("//h2[contains(text(), \"Weight\")]/ancestor::section//*[@role=\"img\"]")); wait.until(ExpectedConditions.attributeContains(chart, "aria-label", "This is a chart with type Line chart.")); @@ -87,7 +99,8 @@ void display_weight_chart_for_month() { button.click(); waitForLoad(); - + wait.until(ExpectedConditions + .visibilityOfElementLocated(By.xpath("//h2[contains(text(),\"Weight\")]"))); WebElement chart = webDriver .findElement(By.xpath("//h2[contains(text(), \"Weight\")]/ancestor::section//*[@role=\"img\"]")); wait.until(ExpectedConditions.attributeContains(chart, "aria-label", "This is a chart with type Line chart.")); @@ -106,7 +119,8 @@ void display_weight_chart_for_year() { button.click(); waitForLoad(); - + wait.until(ExpectedConditions + .visibilityOfElementLocated(By.xpath("//h2[contains(text(),\"Weight\")]"))); WebElement chart = webDriver .findElement(By.xpath("//h2[contains(text(), \"Weight\")]/ancestor::section//*[@role=\"img\"]")); wait.until(ExpectedConditions.attributeContains(chart, "aria-label", "This is a chart with type Line chart.")); @@ -125,16 +139,17 @@ void display_weight_diff_for_all_time() { button.click(); waitForLoad(); - + wait.until(ExpectedConditions + .visibilityOfElementLocated(By.xpath("//h2[contains(text(),\"Weight\")]"))); WebElement weighElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Weight\")]/following-sibling::*/following-sibling::*")); - assertThat(weighElement.getText()).isEqualToIgnoringWhitespace("↓ 19.9 %"); + assertThat(weighElement.getText()).isEqualTo("↓ 19.9 %"); WebElement bodyFatElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Body fat\")]/following-sibling::*/following-sibling::*")); - assertThat(bodyFatElement.getText()).isEqualToIgnoringWhitespace("↓ 43.5 %"); + assertThat(bodyFatElement.getText()).isEqualTo("↓ 43.5 %"); WebElement fatRatioElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Fat ratio\")]/following-sibling::*/following-sibling::*")); - assertThat(fatRatioElement.getText()).isEqualToIgnoringWhitespace("↓ 0.3 %"); + assertThat(fatRatioElement.getText()).isEqualTo("↓ 0.3 %"); } @Test @@ -147,7 +162,8 @@ void display_weight_chart_for_all_time() { button.click(); waitForLoad(); - + wait.until(ExpectedConditions + .visibilityOfElementLocated(By.xpath("//h2[contains(text(),\"Weight\")]"))); WebElement chart = webDriver .findElement(By.xpath("//h2[contains(text(), \"Weight\")]/ancestor::section//*[@role=\"img\"]")); wait.until(ExpectedConditions.attributeContains(chart, "aria-label", "This is a chart with type Line chart.")); diff --git a/test/src/test/java/io/github/mucsi96/workout/test/WithingsTest.java b/test/src/test/java/io/github/mucsi96/workout/test/WithingsTest.java index 899a910..57381d6 100644 --- a/test/src/test/java/io/github/mucsi96/workout/test/WithingsTest.java +++ b/test/src/test/java/io/github/mucsi96/workout/test/WithingsTest.java @@ -17,14 +17,25 @@ void authorizes_withings() { wait.until(ExpectedConditions .visibilityOfElementLocated(By.xpath("//h1[contains(text(), \"Mock Withings\")]"))); webDriver.findElement(By.xpath("//a[contains(text(), \"Authorize\")]")).click(); + longWait.until(ExpectedConditions.urlToBe(baseUrl + "/week")); + String userName = jdbcTemplate.queryForObject("SELECT principal_name FROM oauth2_authorized_client WHERE client_registration_id = 'withings-client';", String.class); + assertThat(userName).isEqualTo("rob"); } @Test - void pulls_todays_weigth_from_withings_to_db() { + void pulls_todays_weigth_measurements_from_withings_to_db() { setupMocks(); open(); - WebElement element = webDriver + wait.until(ExpectedConditions + .visibilityOfElementLocated(By.xpath("//h2[contains(text(),\"Weight\")]"))); + WebElement weighElement = webDriver .findElement(By.xpath("//h2[contains(text(), \"Weight\")]/following-sibling::*")); - assertThat(element.getText()).isEqualToIgnoringWhitespace("87.2 kg"); + assertThat(weighElement.getText()).isEqualTo("87.2 kg"); + WebElement bodyFatElement = webDriver + .findElement(By.xpath("//h2[contains(text(), \"Body fat\")]/following-sibling::*")); + assertThat(bodyFatElement.getText()).isEqualTo("21.8 kg"); + WebElement fatRatioElement = webDriver + .findElement(By.xpath("//h2[contains(text(), \"Fat ratio\")]/following-sibling::*")); + assertThat(fatRatioElement.getText()).isEqualTo("35.3 %"); } }