|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.selenium.devtools.v85; |
| 19 | + |
| 20 | +import com.google.common.collect.ImmutableList; |
| 21 | +import org.openqa.selenium.JavascriptException; |
| 22 | +import org.openqa.selenium.devtools.Command; |
| 23 | +import org.openqa.selenium.devtools.DevTools; |
| 24 | +import org.openqa.selenium.devtools.Event; |
| 25 | +import org.openqa.selenium.devtools.events.ConsoleEvent; |
| 26 | +import org.openqa.selenium.devtools.idealized.Events; |
| 27 | +import org.openqa.selenium.devtools.idealized.runtime.model.RemoteObject; |
| 28 | +import org.openqa.selenium.devtools.v85.runtime.Runtime; |
| 29 | +import org.openqa.selenium.devtools.v85.runtime.model.ConsoleAPICalled; |
| 30 | +import org.openqa.selenium.devtools.v85.runtime.model.ExceptionDetails; |
| 31 | +import org.openqa.selenium.devtools.v85.runtime.model.ExceptionThrown; |
| 32 | +import org.openqa.selenium.devtools.v85.runtime.model.StackTrace; |
| 33 | + |
| 34 | +import java.math.BigDecimal; |
| 35 | +import java.time.Instant; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Optional; |
| 38 | + |
| 39 | +public class V85Events extends Events<ConsoleAPICalled, ExceptionThrown> { |
| 40 | + |
| 41 | + public V85Events(DevTools devtools) { |
| 42 | + super(devtools); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected Command<Void> enableRuntime() { |
| 47 | + return Runtime.enable(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + protected Command<Void> disableRuntime() { |
| 52 | + return Runtime.disable(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + protected Event<ConsoleAPICalled> consoleEvent() { |
| 57 | + return Runtime.consoleAPICalled(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + protected Event<ExceptionThrown> exceptionThrownEvent() { |
| 62 | + return Runtime.exceptionThrown(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected ConsoleEvent toConsoleEvent(ConsoleAPICalled event) { |
| 67 | + long ts = new BigDecimal(event.getTimestamp().toJson()).longValue(); |
| 68 | + |
| 69 | + List<Object> modifiedArgs = event.getArgs().stream() |
| 70 | + .map(obj -> new RemoteObject( |
| 71 | + obj.getType().toString(), |
| 72 | + obj.getValue().orElse(null))) |
| 73 | + .collect(ImmutableList.toImmutableList()); |
| 74 | + |
| 75 | + return new ConsoleEvent( |
| 76 | + event.getType().toString(), |
| 77 | + Instant.ofEpochMilli(ts), |
| 78 | + modifiedArgs); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + protected JavascriptException toJsException(ExceptionThrown event) { |
| 83 | + ExceptionDetails details = event.getExceptionDetails(); |
| 84 | + Optional<StackTrace> maybeTrace = details.getStackTrace(); |
| 85 | + Optional<org.openqa.selenium.devtools.v85.runtime.model.RemoteObject> |
| 86 | + maybeException = details.getException(); |
| 87 | + |
| 88 | + String message = maybeException |
| 89 | + .flatMap(obj -> obj.getDescription().map(String::toString)) |
| 90 | + .orElseGet(details::getText); |
| 91 | + |
| 92 | + JavascriptException exception = new JavascriptException(message); |
| 93 | + |
| 94 | + if (!maybeTrace.isPresent()) { |
| 95 | + StackTraceElement element = new StackTraceElement( |
| 96 | + "unknown", |
| 97 | + "unknown", |
| 98 | + details.getUrl().orElse("unknown"), |
| 99 | + details.getLineNumber()); |
| 100 | + exception.setStackTrace(new StackTraceElement[]{element}); |
| 101 | + return exception; |
| 102 | + } |
| 103 | + |
| 104 | + StackTrace trace = maybeTrace.get(); |
| 105 | + |
| 106 | + exception.setStackTrace(trace.getCallFrames().stream() |
| 107 | + .map(frame -> new StackTraceElement( |
| 108 | + "", |
| 109 | + frame.getFunctionName(), |
| 110 | + frame.getUrl(), |
| 111 | + frame.getLineNumber())) |
| 112 | + .toArray(StackTraceElement[]::new)); |
| 113 | + |
| 114 | + return exception; |
| 115 | + } |
| 116 | +} |
0 commit comments