ShortWebCore - This iOS library lets you run automations on Web Views.

Related tags

Testing ShortWebCore
Overview

ShortWebCore

This iOS library lets you run automations on Web Views.

Example

(Optional) Declare class conforming to AutomationRunnerDelegate:

import ShortWebCore

class Delegate: AutomationRunnerDelegate {
    
    func automationRunner(_ automationRunner: AutomationRunner, didGet result: Any, for action: Action, at index: Int) {
        
        print("Action \(index) returned \(result)")
    }
    
    func automationRunnerDidFinishRunning(_ automationRunner: AutomationRunner) {
        print("Automation did finish running")
    }
    
    func automationRunner(_ automationRunner: AutomationRunner, willExecute action: Action, at index: Int) {
        
        print("Will run action at \(index)")
    }
}

Create an array of Action:

div:nth-of-type(2)")) ] ">
let actions = [
    Action(type: .openURL(URL(string: "https://www.google.com")!, false)),
    Action(type: .input("input[type=text]", "1 chf to clp")),
    Action(type: .click("input[type=submit][value='Google Search']")),
    Action(type: .urlChange),
    Action(type: .getResult("div[data-exchange-rate] > div:nth-of-type(2)"))
]

Actions take the path of an HTML element in the same format than document.querySelector. These are the type of actions:

/// The type of an `Action`.
public indirect enum ActionType : Codable {

    /// Click on the given HTML selector.
    case click(String)

    /// Input the given text on the given HTML selector. The first parameter is the HTML selector and the second parameter is the text to input.
    case input(String, String)

    /// Interact with an element in an iframe. The first parameter is the HTML selector of the iframe and the second one is the action to execute in the iframe.
    case iframe(String, ActionType)

    /// Get a string or an `UIImage` from the given HTML selector.
    case getResult(String)

    /// Wait until the web view finishes loading a new URL.
    case urlChange

    /// Upload a file with the given URL on the given input.
    case uploadFile(String, URL)

    /// Open the given URL. The second argument is `true` to open the URL in mobile mode.
    case openURL(URL, Bool)
}

Next, create a WebView object and an AutomationRunner to run the actions (must be done in the main thread):

let webView = ShortWebCore.WebView()

let runner = AutomationRunner(actions: actions, webView: webView)
let delegate = Delegate()
runner.delegate = delegate
runner.run { result in
    print(result)
}

The result parameter in the closure is an array of items returned by .getResult(_:) actions.

NOTE: The Web View should be in the view hierarchy to run correctly.

Inspecting

WebView contains an inspect(_:) function that can be used to inspect the HTML elements in it. It takes a block with a WebViewManager parameter that runs in a background thread.

webView.inspect { manager in
    
}

WebViewManager has the following methods:

/// Checks if the HTML element at the given selector is a text box.
///
/// - Parameters:
///     - path: The selector of the element to check.
///     - iframePath: The HTML selector of an iframe if the element is located in there.
///
/// - Returns: `true` if the given element takes text input, if not, `false`.
public func isInput(_ path: String, onIframeAt iframePath: String? = nil) -> Bool

/// Checks if the HTML element at the given selector is an input for uploading a file.
///
/// - Parameters:
///     - path: The selector of the element to check.
///     - iframePath: The HTML selector of an iframe if the element is located in there.
///
/// - Returns: `true` if the given element takes a file as input, if not, `false`.
public func isFileInput(_ path: String, onIframeAt iframePath: String? = nil) -> Bool

/// Checks if the HTML element at the given selector is an iframe.
///
/// - Parameters:
///     - path: The selector of the element to check.
///
/// - Returns: `true` if the given element is an iframe.
public func isIframe(_ path: String) -> Bool

/// Returns the location of the element at the given path.
///
/// - Parameters:
///     - path: The selector of the element to check.
///
/// - Returns: The location of the element.
public func location(ofElementAt path: String) -> CGPoint

/// Get the HTML element at the given location in the given web view.
///
/// - Parameters:
///
///     - location: The location of the element.
///     - iframePath: The HTML selector of an iframe if the element is located in there.
///
/// - Returns: The selector of the element or an empty string if it does not exist.
public func element(at location: CGPoint, onIframeAt iframePath: String? = nil) -> String
You might also like...
The XCTest Project, A Swift core library for providing unit test support

XCTest The XCTest library is designed to provide a common framework for writing unit tests in Swift, for Swift packages and applications. This version

An elegant library for stubbing HTTP requests with ease in Swift

Mockingjay An elegant library for stubbing HTTP requests in Swift, allowing you to stub any HTTP/HTTPS using NSURLConnection or NSURLSession. That inc

MockSwift is a Mock library written in Swift.

Welcome to MockSwift MockSwift allows you to write mocks and make better tests. Because MockSwift is an open source library 100% written in Swift, it

Test Library for Swift's Error Handling

CatchingFire CatchingFire is a Swift test framework, which helps making expectations against the error handling of your code. It provides for this pur

A simple and lightweight matching library for XCTest framework.
A simple and lightweight matching library for XCTest framework.

Match A simple and lightweight matching library for XCTest framework. Getting started Swift Package Manager You can add Match to your project by addin

AppiumLibrary is an appium testing library for RobotFramework

Appium library for RobotFramework Introduction AppiumLibrary is an appium testing library for Robot Framework. Library can be downloaded from PyPI. It

Lightweight touch visualization library in Swift. A single line of code and visualize your touches!
Lightweight touch visualization library in Swift. A single line of code and visualize your touches!

TouchVisualizer is a lightweight pure Swift implementation for visualising touches on the screen. Features Works with just a single line of code! Supp

UITest helper library for creating readable and maintainable tests

UITestHelper General information When creating UI tests you will see that you are often repeating the same pieces of code. The UITestHelper library wi

A Swift test double library. Guava - looks like an apple but it's not.

Guava Guava helps you to make your unit tests more flexible. It allows you to replace parts of your system under test with a test double objects. Tabl

Releases(v1.0)
Owner
Emma Cold
I code developer tools for iOS.
Emma Cold
PlaygroundTester enables you to easily run tests for your iPad Playgrounds 4 project.

PlaygroundTester PlaygroundTester is a package that enables you to add tests to your iPad Swift Playgrounds project. Installation Just add PlaygroundT

Paweł Łopusiński 36 Dec 13, 2022
Small library to easily run your tests directly within a Playground

[] (https://developer.apple.com/swift/) Build Status Branch Status master develop About PlaygroundTDD enables you to use TDD directly on Xcode Playgro

Whiskerz AB 317 Nov 22, 2022
PinpointKit is an open-source iOS library in Swift that lets your testers and users send feedback with annotated screenshots using a simple gesture.

PinpointKit is an open-source iOS library in Swift that lets your testers and users send feedback with annotated screenshots using a simple gesture. F

Lickability 1.1k Jan 6, 2023
Erik is an headless browser based on WebKit. An headless browser allow to run functional tests, to access and manipulate webpages using javascript.

Erik Erik is a headless browser based on WebKit and HTML parser Kanna. An headless browser allow to run functional tests, to access and manipulate web

Eric Marchand 544 Dec 30, 2022
Runtime introspection and unit testing of SwiftUI views

ViewInspector ??️‍♂️ for SwiftUI ViewInspector is a library for unit testing SwiftUI views. It allows for traversing a view hierarchy at runtime provi

Alexey Naumov 1.5k Jan 8, 2023
AutoMocker is a Swift framework that leverages the type system to let you easily create mocked instances of your data types.

AutoMocker Context AutoMocker is a Swift framework that leverages the type system to let you easily create mocked instances of your data types. Here's

Vincent Pradeilles 39 May 19, 2022
Library for unifying the approach to network mocking in iOS unit- & UI-tests.

TinkoffMockStrapping Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Installati

Online financial ecosystem 22 Jan 3, 2023
A Mac and iOS Playgrounds Unit Testing library based on Nimble.

Spry Spry is a Swift Playgrounds Unit Testing library based on Nimble. The best thing about Spry is that the API matches Nimble perfectly. Which means

Quick 327 Jul 24, 2022
This is a Swift port of Ruby's Faker library that generates fake data.

This is a Swift port of Ruby's Faker library that generates fake data. Are you still bothered with meaningless randomly character strings? Just relax

Vadym Markov 1.6k Jan 3, 2023
SwiftCheck is a testing library that automatically generates random data for testing of program properties

SwiftCheck QuickCheck for Swift. For those already familiar with the Haskell library, check out the source. For everybody else, see the Tutorial Playg

TypeLift 1.4k Dec 21, 2022