Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RUMM-1649 WebViewTracking integration test added #714

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Datadog/Datadog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,10 @@
9EA3CA6926775A3500B16871 /* VitalRefreshRateReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EA3CA6826775A3500B16871 /* VitalRefreshRateReader.swift */; };
9EA8A7F1275E1518007D6FDB /* HostsSanitizerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EA8A7F0275E1518007D6FDB /* HostsSanitizerTests.swift */; };
9EA8A7F82768A72B007D6FDB /* WebLogEventConsumerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EA8A7F72768A72B007D6FDB /* WebLogEventConsumerTests.swift */; };
9EA95C1C2791C9BE00F6C1F3 /* WebViewTrackingFixtureViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EA95C192791C9BE00F6C1F3 /* WebViewTrackingFixtureViewController.swift */; };
9EA95C1D2791C9BE00F6C1F3 /* WebViewTrackingScenario.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9EA95C1A2791C9BE00F6C1F3 /* WebViewTrackingScenario.storyboard */; };
9EA95C1E2791C9BE00F6C1F3 /* WebViewScenarios.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EA95C1B2791C9BE00F6C1F3 /* WebViewScenarios.swift */; };
9EA95C212791C9E200F6C1F3 /* WebViewScenarioTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EA95C202791C9E200F6C1F3 /* WebViewScenarioTest.swift */; };
9EAF0CF6275A21100044E8CA /* WKUserContentController+DatadogTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EAF0CF5275A21100044E8CA /* WKUserContentController+DatadogTests.swift */; };
9EAF0CF8275A2FDC0044E8CA /* HostsSanitizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EAF0CF7275A2FDC0044E8CA /* HostsSanitizer.swift */; };
9EB4B862274E79D50041CD03 /* WKUserContentController+Datadog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EB4B861274E79D50041CD03 /* WKUserContentController+Datadog.swift */; };
Expand Down Expand Up @@ -1213,6 +1217,10 @@
9EA3CA6826775A3500B16871 /* VitalRefreshRateReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VitalRefreshRateReader.swift; sourceTree = "<group>"; };
9EA8A7F0275E1518007D6FDB /* HostsSanitizerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HostsSanitizerTests.swift; sourceTree = "<group>"; };
9EA8A7F72768A72B007D6FDB /* WebLogEventConsumerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebLogEventConsumerTests.swift; sourceTree = "<group>"; };
9EA95C192791C9BE00F6C1F3 /* WebViewTrackingFixtureViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WebViewTrackingFixtureViewController.swift; sourceTree = "<group>"; };
9EA95C1A2791C9BE00F6C1F3 /* WebViewTrackingScenario.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = WebViewTrackingScenario.storyboard; sourceTree = "<group>"; };
9EA95C1B2791C9BE00F6C1F3 /* WebViewScenarios.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WebViewScenarios.swift; sourceTree = "<group>"; };
9EA95C202791C9E200F6C1F3 /* WebViewScenarioTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WebViewScenarioTest.swift; sourceTree = "<group>"; };
9EAF0CF5275A21100044E8CA /* WKUserContentController+DatadogTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WKUserContentController+DatadogTests.swift"; sourceTree = "<group>"; };
9EAF0CF7275A2FDC0044E8CA /* HostsSanitizer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HostsSanitizer.swift; sourceTree = "<group>"; };
9EB4B861274E79D50041CD03 /* WKUserContentController+Datadog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WKUserContentController+Datadog.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2055,6 +2063,7 @@
6111542325C992D9007C84C9 /* CrashReporting */,
611EA12B2580F40E00BC0E56 /* TrackingConsent */,
6164AE7D252B4CE2000D78C4 /* URLSession */,
9EA95C182791C9BE00F6C1F3 /* WebView */,
);
path = Scenarios;
sourceTree = "<group>";
Expand Down Expand Up @@ -3197,6 +3206,7 @@
61F3CD9F2511070300C816E5 /* RUM */,
61B6811D25F0E8480015B4AF /* CrashReporting */,
611EA15325815EDC00BC0E56 /* TrackingConsent */,
9EA95C1F2791C9E200F6C1F3 /* WebView */,
);
path = Scenarios;
sourceTree = "<group>";
Expand Down Expand Up @@ -3389,6 +3399,24 @@
path = ../Sources/_Datadog_Private;
sourceTree = "<group>";
};
9EA95C182791C9BE00F6C1F3 /* WebView */ = {
isa = PBXGroup;
children = (
9EA95C192791C9BE00F6C1F3 /* WebViewTrackingFixtureViewController.swift */,
9EA95C1A2791C9BE00F6C1F3 /* WebViewTrackingScenario.storyboard */,
9EA95C1B2791C9BE00F6C1F3 /* WebViewScenarios.swift */,
);
path = WebView;
sourceTree = "<group>";
};
9EA95C1F2791C9E200F6C1F3 /* WebView */ = {
isa = PBXGroup;
children = (
9EA95C202791C9E200F6C1F3 /* WebViewScenarioTest.swift */,
);
path = WebView;
sourceTree = "<group>";
};
9EB4B860274E79620041CD03 /* WebView */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -3899,6 +3927,7 @@
D2F5BB36271831C200BDE2A4 /* RUMSwiftUIInstrumentationScenario.storyboard in Resources */,
611EA13C2580F77400BC0E56 /* TrackingConsentScenario.storyboard in Resources */,
61441C1124616DEC003D8BB8 /* LaunchScreen.storyboard in Resources */,
9EA95C1D2791C9BE00F6C1F3 /* WebViewTrackingScenario.storyboard in Resources */,
61337039250F852E00236D58 /* RUMManualInstrumentationScenario.storyboard in Resources */,
6193DCA4251B5691009B8011 /* RUMTapActionScenario.storyboard in Resources */,
6167ACC7251A0BCE0012B4D0 /* NSURLSessionScenario.storyboard in Resources */,
Expand Down Expand Up @@ -4492,6 +4521,7 @@
D2F5BB382718331800BDE2A4 /* SwiftUIRootViewController.swift in Sources */,
618DCFE124C766F500589570 /* SendRUMFixture2ViewController.swift in Sources */,
61441C982461A649003D8BB8 /* DebugTracingViewController.swift in Sources */,
9EA95C1E2791C9BE00F6C1F3 /* WebViewScenarios.swift in Sources */,
61F9CA8025125C01000A5E61 /* RUMNCSScreen3ViewController.swift in Sources */,
6164AE89252B4ECA000D78C4 /* SendThirdPartyRequestsViewController.swift in Sources */,
611EA14225810E1900BC0E56 /* TSHomeViewController.swift in Sources */,
Expand Down Expand Up @@ -4528,6 +4558,7 @@
617247AF25DA9BEA007085B3 /* CrashReportingObjcHelpers.m in Sources */,
6193DCE1251B692C009B8011 /* RUMTASTableViewController.swift in Sources */,
6111544825C9A88B007C84C9 /* PersistenceHelper.swift in Sources */,
9EA95C1C2791C9BE00F6C1F3 /* WebViewTrackingFixtureViewController.swift in Sources */,
61D50C462580EF19006038A3 /* TracingScenarios.swift in Sources */,
61776CED273BEA5500F93802 /* DebugRUMSessionViewController.swift in Sources */,
618DCFE324C766FB00589570 /* SendRUMFixture3ViewController.swift in Sources */,
Expand Down Expand Up @@ -4557,6 +4588,7 @@
61441C4124617013003D8BB8 /* LoggingScenarioTests.swift in Sources */,
612D8F8125AF1C74000E2E09 /* RUMScrubbingScenarioTests.swift in Sources */,
613B77382521E80800155458 /* RUMTabBarControllerScenarioTests.swift in Sources */,
9EA95C212791C9E200F6C1F3 /* WebViewScenarioTest.swift in Sources */,
61441C4B24618052003D8BB8 /* SpanMatcher.swift in Sources */,
6167ACFD251A22E00012B4D0 /* TracingURLSessionScenarioTests.swift in Sources */,
611EA16625825FB300BC0E56 /* TrackingConsentScenarioTests.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@
value = "CrashReportingCollectOrSendWithLoggingScenario"
isEnabled = "NO">
</EnvironmentVariable>
<EnvironmentVariable
key = "DD_TEST_SCENARIO_CLASS_NAME"
value = "WebViewTrackingScenario"
isEnabled = "NO">
</EnvironmentVariable>
</EnvironmentVariables>
</LaunchAction>
<ProfileAction
Expand Down
1 change: 0 additions & 1 deletion Datadog/Example/Debugging/DebugWebviewViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ class WebviewViewController: UIViewController {
super.viewDidLoad()

let controller = WKUserContentController()
// TODO: RUMM-1794 remove internal method call and use public API
controller.addDatadogMessageHandler(allowedWebViewHosts: ["datadoghq.dev"])
let config = WKWebViewConfiguration()
config.userContentController = controller
Expand Down
31 changes: 31 additions & 0 deletions Datadog/Example/Scenarios/WebView/WebViewScenarios.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 Datadog

private struct WebViewTrackingScenarioPredicate: UIKitRUMViewsPredicate {
private let defaultPredicate = DefaultUIKitRUMViewsPredicate()

func rumView(for viewController: UIViewController) -> RUMView? {
if viewController is ShopistWebviewViewController {
return nil // do not consider the webview itself as RUM view
} else {
return defaultPredicate.rumView(for: viewController)
}
}
}

final class WebViewTrackingScenario: TestScenario {
static var storyboardName: String = "WebViewTrackingScenario"

func configureSDK(builder: Datadog.Configuration.Builder) {
_ = builder
.trackUIKitRUMViews(using: WebViewTrackingScenarioPredicate())
.enableLogging(true)
.enableRUM(true)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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 {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

// An action sent from native iOS SDK.
Global.rum.addUserAction(type: .custom, name: "Native action")

// Opens a webview configured to pass all its Browser SDK events to native iOS SDK.
show(ShopistWebviewViewController(), sender: nil)
}
}

class ShopistWebviewViewController: UIViewController {
private let request = URLRequest(url: URL(string: "https://shopist.io")!)
private var webView: WKWebView!

override func viewDidLoad() {
super.viewDidLoad()

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

webView = WKWebView(frame: UIScreen.main.bounds, configuration: config)
view.addSubview(webView)
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
webView.load(request)
}
}
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
Expand Up @@ -33,21 +33,9 @@
},
"testTargets" : [
{
"skippedTests" : [
"IntegrationTests",
"LoggingScenarioTests",
"RUMManualInstrumentationScenarioTests",
"RUMMobileVitalsScenarioTests",
"RUMModalViewsScenarioTests",
"RUMNavigationControllerScenarioTests",
"RUMResourcesScenarioTests",
"RUMScrubbingScenarioTests",
"RUMSwiftUIScenarioTests",
"RUMTabBarControllerScenarioTests",
"RUMTapActionScenarioTests",
"TracingManualInstrumentationScenarioTests",
"TracingURLSessionScenarioTests",
"TrackingConsentScenarioTests"
"selectedTests" : [
"CrashReportingWithLoggingScenarioTests\/testCrashReportingCollectOrSendWithLoggingScenario()",
"CrashReportingWithRUMScenarioTests\/testCrashReportingCollectOrSendWithRUMScenario()"
Comment on lines -36 to +38
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is on purpose. By default, all new integration tests are added to both XCTest plans (integration and crash reporting), meaning we will run it twice on CI. To avoid this, I disabled "add new tests automatically" in crash reporting XCTest plan. Apparently, this means changing from opt-out to opt-in in .xctestplan 👍.

],
"target" : {
"containerPath" : "container:Datadog.xcodeproj",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,16 @@ extension RUMCommonAsserts {

extension RUMSessionMatcher {
/// Retrieves single RUM Session from given `requests`.
class func singleSession(from requests: [HTTPServerMock.Request]) throws -> RUMSessionMatcher? {
return try sessions(maxCount: 1, from: requests).first
/// - Parameter eventsPatch: optional transformation to apply on each event within the payload before instantiating matcher (default: `nil`)
class func singleSession(from requests: [HTTPServerMock.Request], eventsPatch: ((Data) throws -> Data)? = nil) throws -> RUMSessionMatcher? {
return try sessions(maxCount: 1, from: requests, eventsPatch: eventsPatch).first
}

/// Retrieves `maxCount` RUM Sessions from given `requests`.
class func sessions(maxCount: Int, from requests: [HTTPServerMock.Request]) throws -> [RUMSessionMatcher] {
/// - Parameter eventsPatch: optional transformation to apply on each event within the payload before instantiating matcher (default: `nil`)
class func sessions(maxCount: Int, from requests: [HTTPServerMock.Request], eventsPatch: ((Data) throws -> Data)? = nil) throws -> [RUMSessionMatcher] {
let eventMatchers = try requests
.flatMap { request in try RUMEventMatcher.fromNewlineSeparatedJSONObjectsData(request.httpBody) }
.flatMap { request in try RUMEventMatcher.fromNewlineSeparatedJSONObjectsData(request.httpBody, eventsPatch: eventsPatch) }
let sessionMatchers = try RUMSessionMatcher.groupMatchersBySessions(eventMatchers)

if sessionMatchers.count > maxCount {
Expand Down
Loading