|
| 1 | +/* |
| 2 | + * Copyright 2016-2026 Qameta Software Inc |
| 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 | +package io.qameta.allure.seleniumbidi; |
| 17 | + |
| 18 | +import io.qameta.allure.Allure; |
| 19 | +import io.qameta.allure.AllureLifecycle; |
| 20 | +import org.openqa.selenium.WebDriver; |
| 21 | +import org.openqa.selenium.support.events.EventFiringDecorator; |
| 22 | +import org.openqa.selenium.support.events.WebDriverListener; |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | + |
| 26 | +import java.lang.reflect.Method; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.IdentityHashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.concurrent.locks.Lock; |
| 32 | +import java.util.concurrent.locks.ReentrantLock; |
| 33 | + |
| 34 | +/** |
| 35 | + * Selenium WebDriver BiDi listener that captures browser log and network events |
| 36 | + * as aggregated Allure attachments. |
| 37 | + */ |
| 38 | +@SuppressWarnings("unused") |
| 39 | +public class AllureWebDriverBiDi implements WebDriverListener, AutoCloseable { |
| 40 | + |
| 41 | + private static final Logger LOGGER = LoggerFactory.getLogger(AllureWebDriverBiDi.class); |
| 42 | + |
| 43 | + private final AllureLifecycle lifecycle; |
| 44 | + private final BiDiSessionFactory sessionFactory; |
| 45 | + private final BiDiConfiguration configuration = new BiDiConfiguration(); |
| 46 | + private final Map<WebDriver, BiDiSessionState> sessions = new IdentityHashMap<>(); |
| 47 | + private final Lock sessionsLock = new ReentrantLock(); |
| 48 | + |
| 49 | + public AllureWebDriverBiDi() { |
| 50 | + this(Allure.getLifecycle(), new SeleniumBiDiSessionFactory()); |
| 51 | + } |
| 52 | + |
| 53 | + AllureWebDriverBiDi(final AllureLifecycle lifecycle, |
| 54 | + final BiDiSessionFactory sessionFactory) { |
| 55 | + this.lifecycle = lifecycle; |
| 56 | + this.sessionFactory = sessionFactory; |
| 57 | + } |
| 58 | + |
| 59 | + public <T extends WebDriver> T decorate(final T driver) { |
| 60 | + return new EventFiringDecorator<T>(this).decorate(driver); |
| 61 | + } |
| 62 | + |
| 63 | + public AllureWebDriverBiDi logs(final boolean enabled) { |
| 64 | + configuration.setLogsEnabled(enabled); |
| 65 | + return this; |
| 66 | + } |
| 67 | + |
| 68 | + public AllureWebDriverBiDi network(final boolean enabled) { |
| 69 | + configuration.setNetworkEnabled(enabled); |
| 70 | + return this; |
| 71 | + } |
| 72 | + |
| 73 | + public AllureWebDriverBiDi maxLogEntries(final int maxLogEntries) { |
| 74 | + configuration.setMaxLogEntries(maxLogEntries); |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + public AllureWebDriverBiDi maxNetworkEvents(final int maxNetworkEvents) { |
| 79 | + configuration.setMaxNetworkEvents(maxNetworkEvents); |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + public AllureWebDriverBiDi redactHeaders(final String... headerNames) { |
| 84 | + configuration.redactHeaders(headerNames); |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void beforeAnyWebDriverCall(final WebDriver driver, |
| 90 | + final Method method, |
| 91 | + final Object[] args) { |
| 92 | + captureActiveAllureContext(driver); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void afterAnyWebDriverCall(final WebDriver driver, |
| 97 | + final Method method, |
| 98 | + final Object[] args, |
| 99 | + final Object result) { |
| 100 | + captureActiveAllureContext(driver); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void beforeQuit(final WebDriver driver) { |
| 105 | + closeSession(driver); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void afterQuit(final WebDriver driver) { |
| 110 | + closeSession(driver); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void close() { |
| 115 | + final List<BiDiSessionState> activeSessions = new ArrayList<>(); |
| 116 | + sessionsLock.lock(); |
| 117 | + try { |
| 118 | + activeSessions.addAll(sessions.values()); |
| 119 | + sessions.clear(); |
| 120 | + } finally { |
| 121 | + sessionsLock.unlock(); |
| 122 | + } |
| 123 | + activeSessions.forEach(this::safeFlushAndClose); |
| 124 | + } |
| 125 | + |
| 126 | + private void captureActiveAllureContext(final WebDriver driver) { |
| 127 | + if (!configuration.isAnyEnabled()) { |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + if (!lifecycle.getCurrentTestCaseOrStep().isPresent()) { |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + getOrCreateSession(driver); |
| 136 | + } |
| 137 | + |
| 138 | + private BiDiSessionState getOrCreateSession(final WebDriver driver) { |
| 139 | + sessionsLock.lock(); |
| 140 | + try { |
| 141 | + final BiDiSessionState existing = sessions.get(driver); |
| 142 | + if (existing != null) { |
| 143 | + return existing; |
| 144 | + } |
| 145 | + |
| 146 | + try { |
| 147 | + final BiDiSessionState created = BiDiSessionState.start(driver, configuration, sessionFactory); |
| 148 | + if (created != null) { |
| 149 | + sessions.put(driver, created); |
| 150 | + } |
| 151 | + return created; |
| 152 | + } catch (RuntimeException e) { |
| 153 | + LOGGER.debug("Could not start WebDriver BiDi capture", e); |
| 154 | + return null; |
| 155 | + } |
| 156 | + } finally { |
| 157 | + sessionsLock.unlock(); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private void closeSession(final WebDriver driver) { |
| 162 | + final BiDiSessionState state; |
| 163 | + sessionsLock.lock(); |
| 164 | + try { |
| 165 | + state = sessions.remove(driver); |
| 166 | + } finally { |
| 167 | + sessionsLock.unlock(); |
| 168 | + } |
| 169 | + safeFlushAndClose(state); |
| 170 | + } |
| 171 | + |
| 172 | + private void safeFlushAndClose(final BiDiSessionState state) { |
| 173 | + if (state == null) { |
| 174 | + return; |
| 175 | + } |
| 176 | + try { |
| 177 | + state.flushAndClose(lifecycle); |
| 178 | + } catch (RuntimeException e) { |
| 179 | + LOGGER.debug("Could not flush WebDriver BiDi capture", e); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments