|
| 1 | +/* |
| 2 | + * Copyright (c) Microsoft Corporation. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.microsoft.playwright.junit; |
| 18 | + |
| 19 | +import com.microsoft.playwright.Browser; |
| 20 | +import com.microsoft.playwright.Page; |
| 21 | +import org.junit.jupiter.api.AfterAll; |
| 22 | +import org.junit.jupiter.api.RepeatedTest; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.api.io.TempDir; |
| 25 | +import org.junit.jupiter.params.ParameterizedTest; |
| 26 | +import org.junit.jupiter.params.provider.ValueSource; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.nio.file.Files; |
| 30 | +import java.nio.file.Path; |
| 31 | +import java.util.List; |
| 32 | +import java.util.stream.Collectors; |
| 33 | +import java.util.stream.Stream; |
| 34 | + |
| 35 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 36 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 37 | + |
| 38 | +@FixtureTest |
| 39 | +@UsePlaywright(TestFixtureTraceOutputDir.TraceOptions.class) |
| 40 | +public class TestFixtureTraceOutputDir { |
| 41 | + @TempDir |
| 42 | + static Path outputDir; |
| 43 | + private static String browserName; |
| 44 | + |
| 45 | + public static class TraceOptions implements OptionsFactory { |
| 46 | + @Override |
| 47 | + public Options getOptions() { |
| 48 | + return new Options().setTrace(Options.Trace.ON).setOutputDir(outputDir); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void shouldSaveTrace(Browser browser, Page page) { |
| 54 | + browserName = browser.browserType().name(); |
| 55 | + page.setContent("<div>test</div>"); |
| 56 | + } |
| 57 | + |
| 58 | + @ParameterizedTest(name = "[{index}] {0}") |
| 59 | + @ValueSource(strings = {"foo", "bar"}) |
| 60 | + void shouldSaveTracePerParameterizedInvocation(String value, Page page) { |
| 61 | + page.setContent("<div>" + value + "</div>"); |
| 62 | + } |
| 63 | + |
| 64 | + @RepeatedTest(2) |
| 65 | + void shouldSaveTracePerRepetition(Page page) { |
| 66 | + page.setContent("<div>repeated</div>"); |
| 67 | + } |
| 68 | + |
| 69 | + @AfterAll |
| 70 | + static void shouldWriteTraceOfEachInvocationToSeparateDir() throws IOException { |
| 71 | + String prefix = TestFixtureTraceOutputDir.class.getName() + "."; |
| 72 | + List<String> expected = Stream.of( |
| 73 | + "shouldSaveTrace-" + browserName, |
| 74 | + "shouldSaveTracePerParameterizedInvocation-" + browserName + "--1-foo", |
| 75 | + "shouldSaveTracePerParameterizedInvocation-" + browserName + "--2-bar", |
| 76 | + "shouldSaveTracePerRepetition-" + browserName + "-repetition-1-of-2", |
| 77 | + "shouldSaveTracePerRepetition-" + browserName + "-repetition-2-of-2" |
| 78 | + ).map(name -> prefix + name).sorted().collect(Collectors.toList()); |
| 79 | + List<String> actual; |
| 80 | + try (Stream<Path> dirs = Files.list(outputDir)) { |
| 81 | + actual = dirs.map(dir -> dir.getFileName().toString()).sorted().collect(Collectors.toList()); |
| 82 | + } |
| 83 | + assertEquals(expected, actual); |
| 84 | + for (String dir : actual) { |
| 85 | + assertTrue(Files.exists(outputDir.resolve(dir).resolve("trace.zip")), dir); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments