Skip to content

Commit

Permalink
#2172 Add sample usage for "step" method
Browse files Browse the repository at this point in the history
"step" feature was released in Selenide 6.14.0, see https://selenide.org/2023/05/08/selenide-6.14.0/#add-step-method
  • Loading branch information
asolntsev committed Sep 12, 2024
1 parent cc51b00 commit e5ead2e
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public EventStatus getStatus() {
return this.status;
}

protected void setStatus(EventStatus status) {
protected final void setStatus(EventStatus status) {
this.status = status;
endNs = System.nanoTime();
}
Expand Down Expand Up @@ -67,7 +67,7 @@ public Throwable getError() {
return error;
}

public void setError(Throwable error) {
public final void setError(Throwable error) {
this.error = error;
}

Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/codeborne/selenide/logevents/SimpleReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,18 @@ public void finish(String title) {
return;
}

List<LogEvent> events = new ArrayList<>(logEventListener.events());

events.sort(comparingLong(LogEvent::getStartTime));

String report = generateReport(title, events);
String report = generateReport(title, logEventListener.events());
log.info(report);

logEventListener.clear();
}

@Nonnull
@CheckReturnValue
String generateReport(String title, List<LogEvent> events) {
protected String generateReport(String title, List<LogEvent> rawEvents) {
List<LogEvent> events = new ArrayList<>(rawEvents);

events.sort(comparingLong(LogEvent::getStartTime));
var eventsWithNestingLevel = escapeSubjectAndComputeNestingLevel(events);

int firstColumnWidth = Math.max(maxLocatorLength(eventsWithNestingLevel), MIN_FIRST_COLUMN_WIDTH);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.codeborne.selenide.logevents;

import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import java.io.IOException;
import java.util.List;
Expand All @@ -16,14 +15,6 @@
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.apache.commons.io.IOUtils.resourceToString;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.anyList;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

final class SimpleReportTest {
private static final Pattern REGEX_LINE_SEPARATOR = Pattern.compile("\\n");
Expand Down Expand Up @@ -101,39 +92,31 @@ void reportWithNestingEvents() throws IOException {
@Test
void sortEventsBeforeGenerateReport() {
Log event1 = new Log("event 1", "click", PASS, millisToNanos(200), millisToNanos(300));
Log event2 = new Log("event 2", "click", PASS, millisToNanos(300), millisToNanos(400));
Log event3 = new Log("event 3", "click", PASS, millisToNanos(400), millisToNanos(500));
Log event2 = new Log("event 2", "click", PASS, millisToNanos(300), millisToNanos(500));
Log event3 = new Log("event 3", "click", PASS, millisToNanos(500), millisToNanos(800));
Log innerWrapper = new Log("inner wrapper", "click", PASS, millisToNanos(100), millisToNanos(800));
Log mainWrapper = new Log("main wrapper", "", PASS, 0, millisToNanos(1000));

SimpleReport spiedReport = spy(new SimpleReport());
EventsCollector collector = mock(EventsCollector.class);

SelenideLogger.addListener("simpleReport", collector);
when(collector.events()).thenReturn(asList(
List<String> reportLines = new SimpleReport().generateReport("SORTING EVENTS", asList(
event1,
event2,
event3,
innerWrapper,
mainWrapper
));

ArgumentCaptor<List<LogEvent>> captor = ArgumentCaptor.forClass(List.class);
doReturn("report").when(spiedReport).generateReport(anyString(), anyList());

spiedReport.finish("Test");

verify(spiedReport).generateReport(eq("Test"), captor.capture());

List<LogEvent> events = captor.getValue();

assertThat(events).isEqualTo(asList(
mainWrapper,
innerWrapper,
event1,
event2,
event3
));
)).lines().toList();

assertThat(reportLines).containsExactly(
"Report for SORTING EVENTS",
"+----------------------+---------+------------+------------+",
"| Element | Subject | Status | ms. |",
"+----------------------+---------+------------+------------+",
"| main wrapper | | PASS | 1000 |",
"| inner wrapper | click | PASS | 700 |",
"| event 1 | click | PASS | 100 |",
"| event 2 | click | PASS | 200 |",
"| event 3 | click | PASS | 300 |",
"+----------------------+---------+------------+------------+"
);
}

@Test
Expand Down
3 changes: 1 addition & 2 deletions src/test/resources/page_with_date_input.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ <h1>Page with date input</h1>
<form>
<fieldset title="User registration form">
<label> Birthday:
<input id="birthday" name="birthday" type="date" />
<input id="birthday" name="birthday" type="date" value="1997-08-29" />
</label>
<br/>
</fieldset>
Expand All @@ -23,7 +23,6 @@ <h1>Page with date input</h1>

<script>
document.getElementById('birthday').onchange = function (e) {
//console.log('e', e);
document.getElementById('user-summary').textContent = 'User birthday: ' + e.target.value;
}
</script>
Expand Down
93 changes: 93 additions & 0 deletions statics/src/test/java/integration/StepsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package integration;

import com.codeborne.selenide.logevents.EventsCollector;
import com.codeborne.selenide.logevents.LogEvent;
import com.codeborne.selenide.logevents.SelenideLogger;
import com.codeborne.selenide.logevents.SimpleReport;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import java.time.LocalDate;
import java.util.List;

import static com.codeborne.selenide.Condition.text;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.SetValueOptions.withDate;
import static com.codeborne.selenide.logevents.SelenideLogger.step;
import static org.assertj.core.api.Assertions.assertThat;

public class StepsTest extends IntegrationTest {
private final EventsCollector events = new EventsCollector();

@Test
void canUseInnerSteps() {
step("Edit CV", () -> {
String previousBirthday = step("open CV page", () -> {
openFile("page_with_date_input.html");
return $("#birthday").val();
});

step("invalid birthday", () -> {
$("#birthday").setValue(withDate(LocalDate.parse("2099-01-01")));
$("#user-summary").shouldHave(text("User birthday: 2099-01-01"));
});

step("valid birthday", () -> {
$("#birthday").setValue(withDate(LocalDate.parse(previousBirthday)));
$("#user-summary").shouldHave(text("User birthday: " + previousBirthday));
});
});

List<String> steps = stepsFromReport();

assertThat(steps).containsExactly(
"| Element |",
"| Edit CV |",
"| open CV page |",
"| open |",
"| #birthday |",
"| invalid birthday |",
"| #birthday |",
"| #user-summary |",
"| valid birthday |",
"| #birthday |",
"| #user-summary |"
);
}

@Nonnull
@CheckReturnValue
private List<String> stepsFromReport() {
String report = SimpleReportTester.report(events.events());

return report.lines()
.filter(line -> line.startsWith("|"))
.map(line -> line.replaceFirst("(\\|.+?\\|).*", "$1"))
.toList();
}

@ParametersAreNonnullByDefault
private static class SimpleReportTester extends SimpleReport {
@Nonnull
@CheckReturnValue
static String report(List<LogEvent> events) {
return new SimpleReportTester().generateReport("", events);
}
}

@BeforeEach
void setUp() {
open();
SelenideLogger.addListener("StepsTest.listener", events);
}

@AfterEach
void tearDown() {
SelenideLogger.removeListener("StepsTest.listener");
}
}

0 comments on commit e5ead2e

Please sign in to comment.