Skip to content

Commit

Permalink
RUMM-1649 WebViewTracking integration test added
Browse files Browse the repository at this point in the history
  • Loading branch information
buranmert committed Jan 12, 2022
1 parent 317a090 commit 27b27ea
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Datadog/Datadog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,9 @@
61FF283024BC5E2D000B3D9B /* RUMEventFileOutputTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61FF282F24BC5E2D000B3D9B /* RUMEventFileOutputTests.swift */; };
61FF416225EE5FF400CE35EC /* CrashReportingWithLoggingIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61FF416125EE5FF400CE35EC /* CrashReportingWithLoggingIntegrationTests.swift */; };
61FF9A4525AC5DEA001058CC /* RUMViewIdentity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61FF9A4425AC5DEA001058CC /* RUMViewIdentity.swift */; };
9E01C59727885189008835B3 /* WebViewScenarios.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E01C59627885189008835B3 /* WebViewScenarios.swift */; };
9E01C599278851E9008835B3 /* WebViewTrackingScenario.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9E01C598278851E9008835B3 /* WebViewTrackingScenario.storyboard */; };
9E01C59B278868C8008835B3 /* WebViewTrackingFixtureViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E01C59A278868C8008835B3 /* WebViewTrackingFixtureViewController.swift */; };
9E0542CB25F8EBBE007A3D0B /* Kronos.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9E0542CA25F8EBBE007A3D0B /* Kronos.xcframework */; };
9E26E6B924C87693000B3270 /* RUMDataModels.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E26E6B824C87693000B3270 /* RUMDataModels.swift */; };
9E2EF44F2694FA14008A7DAE /* VitalInfoSamplerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E2EF44E2694FA14008A7DAE /* VitalInfoSamplerTests.swift */; };
Expand All @@ -489,6 +492,7 @@
9E53889C2773C4B300A7DC42 /* WebRUMEventConsumerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E53889B2773C4B300A7DC42 /* WebRUMEventConsumerTests.swift */; };
9E544A4F24753C6E00E83072 /* MethodSwizzler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E544A4E24753C6E00E83072 /* MethodSwizzler.swift */; };
9E544A5124753DDE00E83072 /* MethodSwizzlerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E544A5024753DDE00E83072 /* MethodSwizzlerTests.swift */; };
9E55034C278DBE57007ECEB1 /* WebViewScenarioTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E55034B278DBE57007ECEB1 /* WebViewScenarioTest.swift */; };
9E55407C25812D1C00F6E3AD /* RUMMonitor+objc.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E55407B25812D1C00F6E3AD /* RUMMonitor+objc.swift */; };
9E58E8E124615C75008E5063 /* JSONEncoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E58E8E024615C75008E5063 /* JSONEncoder.swift */; };
9E58E8E324615EDA008E5063 /* JSONEncoderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E58E8E224615EDA008E5063 /* JSONEncoderTests.swift */; };
Expand Down Expand Up @@ -1147,6 +1151,9 @@
61FF282F24BC5E2D000B3D9B /* RUMEventFileOutputTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RUMEventFileOutputTests.swift; sourceTree = "<group>"; };
61FF416125EE5FF400CE35EC /* CrashReportingWithLoggingIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CrashReportingWithLoggingIntegrationTests.swift; sourceTree = "<group>"; };
61FF9A4425AC5DEA001058CC /* RUMViewIdentity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RUMViewIdentity.swift; sourceTree = "<group>"; };
9E01C59627885189008835B3 /* WebViewScenarios.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebViewScenarios.swift; sourceTree = "<group>"; };
9E01C598278851E9008835B3 /* WebViewTrackingScenario.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = WebViewTrackingScenario.storyboard; sourceTree = "<group>"; };
9E01C59A278868C8008835B3 /* WebViewTrackingFixtureViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebViewTrackingFixtureViewController.swift; sourceTree = "<group>"; };
9E0542CA25F8EBBE007A3D0B /* Kronos.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = Kronos.xcframework; path = ../Carthage/Build/Kronos.xcframework; sourceTree = "<group>"; };
9E26E6B824C87693000B3270 /* RUMDataModels.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RUMDataModels.swift; sourceTree = "<group>"; };
9E2EF44E2694FA14008A7DAE /* VitalInfoSamplerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VitalInfoSamplerTests.swift; sourceTree = "<group>"; };
Expand All @@ -1155,6 +1162,7 @@
9E53889B2773C4B300A7DC42 /* WebRUMEventConsumerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebRUMEventConsumerTests.swift; sourceTree = "<group>"; };
9E544A4E24753C6E00E83072 /* MethodSwizzler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MethodSwizzler.swift; sourceTree = "<group>"; };
9E544A5024753DDE00E83072 /* MethodSwizzlerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MethodSwizzlerTests.swift; sourceTree = "<group>"; };
9E55034B278DBE57007ECEB1 /* WebViewScenarioTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebViewScenarioTest.swift; sourceTree = "<group>"; };
9E55407B25812D1C00F6E3AD /* RUMMonitor+objc.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RUMMonitor+objc.swift"; sourceTree = "<group>"; };
9E58E8E024615C75008E5063 /* JSONEncoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONEncoder.swift; sourceTree = "<group>"; };
9E58E8E224615EDA008E5063 /* JSONEncoderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONEncoderTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1989,6 +1997,7 @@
6111542325C992D9007C84C9 /* CrashReporting */,
611EA12B2580F40E00BC0E56 /* TrackingConsent */,
6164AE7D252B4CE2000D78C4 /* URLSession */,
9E01C59527885121008835B3 /* WebView */,
);
path = Scenarios;
sourceTree = "<group>";
Expand Down Expand Up @@ -3098,6 +3107,7 @@
61F3CD9F2511070300C816E5 /* RUM */,
61B6811D25F0E8480015B4AF /* CrashReporting */,
611EA15325815EDC00BC0E56 /* TrackingConsent */,
9E55034A278DBE0E007ECEB1 /* WebView */,
);
path = Scenarios;
sourceTree = "<group>";
Expand Down Expand Up @@ -3228,6 +3238,16 @@
path = RUMEventOutputs;
sourceTree = "<group>";
};
9E01C59527885121008835B3 /* WebView */ = {
isa = PBXGroup;
children = (
9E01C59627885189008835B3 /* WebViewScenarios.swift */,
9E01C598278851E9008835B3 /* WebViewTrackingScenario.storyboard */,
9E01C59A278868C8008835B3 /* WebViewTrackingFixtureViewController.swift */,
);
path = WebView;
sourceTree = "<group>";
};
9E06058F26EF904200F5F935 /* LongTasks */ = {
isa = PBXGroup;
children = (
Expand All @@ -3245,6 +3265,14 @@
path = include;
sourceTree = "<group>";
};
9E55034A278DBE0E007ECEB1 /* WebView */ = {
isa = PBXGroup;
children = (
9E55034B278DBE57007ECEB1 /* WebViewScenarioTest.swift */,
);
path = WebView;
sourceTree = "<group>";
};
9E5D2D4A249137E900763FE4 /* Swizzling */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -3729,6 +3757,7 @@
6137E649252DD88D00720485 /* RUMModalViewsAutoInstrumentationScenario.storyboard in Resources */,
61441C0C24616DE9003D8BB8 /* Main.storyboard in Resources */,
615AAC36251E353700C89EE9 /* RUMTabBarAutoInstrumentationScenario.storyboard in Resources */,
9E01C599278851E9008835B3 /* WebViewTrackingScenario.storyboard in Resources */,
9EC2835E26CFF57A00FACF1C /* RUMMobileVitalsScenario.storyboard in Resources */,
6167AD19251A27B80012B4D0 /* URLSessionScenario.storyboard in Resources */,
61F9CA792512593A000A5E61 /* RUMNavigationControllerScenario.storyboard in Resources */,
Expand Down Expand Up @@ -4295,6 +4324,8 @@
6164AE89252B4ECA000D78C4 /* SendThirdPartyRequestsViewController.swift in Sources */,
611EA14225810E1900BC0E56 /* TSHomeViewController.swift in Sources */,
612D8F6F25AEE6A7000E2E09 /* RUMScrubbingViewController.swift in Sources */,
9E01C59727885189008835B3 /* WebViewScenarios.swift in Sources */,
9E01C59B278868C8008835B3 /* WebViewTrackingFixtureViewController.swift in Sources */,
61163C37252DDD60007DD5BF /* RUMMVSViewController.swift in Sources */,
61441C952461A649003D8BB8 /* ConsoleOutputInterceptor.swift in Sources */,
61D50C5A2580EFF3006038A3 /* URLSessionScenarios.swift in Sources */,
Expand Down Expand Up @@ -4357,6 +4388,7 @@
6167ACFD251A22E00012B4D0 /* TracingURLSessionScenarioTests.swift in Sources */,
611EA16625825FB300BC0E56 /* TrackingConsentScenarioTests.swift in Sources */,
61F3CD9A2510D8C500C816E5 /* Environment.swift in Sources */,
9E55034C278DBE57007ECEB1 /* WebViewScenarioTest.swift in Sources */,
61F9CA9F2513978D000A5E61 /* RUMSessionMatcher.swift in Sources */,
61B6815E25F135890015B4AF /* CrashReportingWithRUMScenarioTests.swift in Sources */,
61F9CA92251266F7000A5E61 /* RUMNavigationControllerScenarioTests.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@
value = "CrashReportingCollectOrSendWithLoggingScenario"
isEnabled = "NO">
</EnvironmentVariable>
<EnvironmentVariable
key = "DD_TEST_SCENARIO_CLASS_NAME"
value = "WebViewTrackingScenario"
isEnabled = "NO">
</EnvironmentVariable>
</EnvironmentVariables>
</LaunchAction>
<ProfileAction
Expand Down
20 changes: 20 additions & 0 deletions Datadog/Example/Scenarios/WebView/WebViewScenarios.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2019-2020 Datadog, Inc.
*/

import Foundation
import Datadog

/// Scenario which uses RUM only. Blocks the main thread and expects to have non-zero MobileVitals values
final class WebViewTrackingScenario: TestScenario {
static var storyboardName: String = "WebViewTrackingScenario"

func configureSDK(builder: Datadog.Configuration.Builder) {
_ = builder
.trackUIKitRUMViews()
.enableLogging(true)
.enableRUM(true)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2019-2020 Datadog, Inc.
*/

import UIKit
import WebKit
import Datadog

class WebViewTrackingFixtureViewController: UIViewController, WKNavigationDelegate {
let html = """
<head>
<meta charset="utf-8">
<title>Mobile SDK Detection test page</title>
<link rel="icon" type="image/x-icon" href="./favicon.ico">
<script type="text/javascript">
function show(message) {
const p = document.createElement("p");
p.innerHTML = message;
if (document.body) {
document.body.appendChild(p);
} else {
window.addEventListener("DOMContentLoaded", () =>
document.body.appendChild(p)
);
}
}
function isEventBridgePresent() {
const isPresent = window.DatadogEventBridge;
if (!isPresent) {
show(`window.DatadogEventBridge absent!`);
return false;
}
if (!isViewHostAllowed()) {
show(
`This page does not respect window.DatadogEventBridge.getAllowedWebViewHosts: <br>${window.DatadogEventBridge.getAllowedWebViewHosts()}`
);
return false;
}
return true;
}
function getEventBridge() {
const datadogEventBridge = window.DatadogEventBridge;
return {
getAllowedWebViewHosts() {
try {
return JSON.parse(
window.DatadogEventBridge.getAllowedWebViewHosts()
);
} catch (e) {
show(
`allowWebViewHosts is not a valid json ${window.DatadogEventBridge.getAllowedWebViewHosts()}`
);
}
return [];
},
send(eventType, event) {
const eventStr = JSON.stringify({
eventType,
event,
tags: ["browser_sdk_version:3.6.13"],
});
datadogEventBridge.send(eventStr);
show(
`window.DatadogEventBridge: ${eventType} sent! <br><br>${eventStr}`
);
},
};
}
function sendLog() {
if (!isEventBridgePresent()) {
return;
}
try {
const log = {
date: 1635932927012,
error: { origin: "console" },
message: "console error: error",
session_id: "0110cab4-7471-480e-aa4e-7ce039ced355",
status: "error",
view: {
referrer: "",
url: "https://datadoghq.dev/browser-sdk-test-playground",
},
};
getEventBridge().send("log", log);
} catch (err) {
show(`window.DatadogEventBridge: Could not send ${err}`);
}
}
function isViewHostAllowed() {
return getEventBridge()
.getAllowedWebViewHosts()
.some((o) => o.includes(window.location.hostname));
}
show(
`window.DatadogEventBridge: <br>${JSON.stringify(
window.DatadogEventBridge
)}`
);
</script>
</head>
<body>
<button onclick="sendLog()">Send dummy log</button>
<p>window.DatadogEventBridge: <br>undefined</p></body>
"""

private var webView: WKWebView!

override func viewDidLoad() {
super.viewDidLoad()

let controller = WKUserContentController()
controller.addDatadogMessageHandler(allowedWebViewHosts: ["datadoghq.dev"])
let config = WKWebViewConfiguration()
config.userContentController = controller

webView = WKWebView(frame: view.bounds, configuration: config)
webView.navigationDelegate = self
view.addSubview(webView)
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
webView.loadHTMLString(html, baseURL: nil)
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
let js = """
window.sendLog()
"""
webView.evaluateJavaScript(js) { res, err in
assert(err == nil, "JS execution shouldn't return an error")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="19529" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="Y6W-OH-hqX">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="19519"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Web View Tracking Fixture View Controller-->
<scene sceneID="s0d-6b-0kx">
<objects>
<viewController id="Y6W-OH-hqX" customClass="WebViewTrackingFixtureViewController" customModule="Example" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="5EZ-qb-Rvc">
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<viewLayoutGuide key="safeArea" id="vDu-zF-Fre"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Ief-a0-LHa" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="114" y="80"/>
</scene>
</scenes>
<resources>
<systemColor name="systemBackgroundColor">
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
</resources>
</document>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2019-2020 Datadog, Inc.
*/

import Foundation
import HTTPServerMock
import XCTest

class WebViewScenarioTest: IntegrationTests, LoggingCommonAsserts {
func testWebViewLoggingScenario() throws {
let loggingServerSession = server.obtainUniqueRecordingSession()

let app = ExampleApplication()
app.launchWith(
testScenarioClassName: "WebViewTrackingScenario",
serverConfiguration: HTTPServerMockConfiguration(
logsEndpoint: loggingServerSession.recordingURL
)
)

// Get expected number of `LogMatchers`
let recordedRequests = try loggingServerSession.pullRecordedRequests(timeout: dataDeliveryTimeout) { requests in
try LogMatcher.from(requests: requests).count >= 1
}
let logMatchers = try LogMatcher.from(requests: recordedRequests)

// Assert common things
assertLogging(requests: recordedRequests)

logMatchers[0].assertStatus(equals: "error")
logMatchers[0].assertMessage(equals: "console error: error")
}
}

0 comments on commit 27b27ea

Please sign in to comment.