View on GitHub

Erik

Erik is an headless browser based on WebKit. An headless browser allow to run functional tests, to access and manipulate webpages using javascript.

Erik

Join the chat at https://gitter.im/phimage/Erik License Platform Language Issues CocoapodCarthage compatible

Become a Patron! Buy me a coffee

Erik is a headless browser based on WebKit and HTML parser Kanna.

An headless browser allow to run functional tests, to access and manipulate webpages using javascript.

let browser = Erik.visit(url: url) { document, error in
    // browse HTML element, click, submit form and more
}

Go to an url

Erik.visit(url: url) { object, error in
    if let e = error {

    } else if let doc = object {
        // HTML Inspection
    }
}

How to get current url?

if let url = Erik.currentURL {..}

For multiple browsing you can create an instance of headless browser and use same functions

let browser = Erik()
browser.visitURL...

HTML Inspection

Search for nodes by CSS selector

for link in doc.querySelectorAll("a, link") {
    print(link.text)
    print(link["href"])
}

Edit first input field with name “user”

if let input = doc.querySelectorAll("input[name=\"user\"]").first {
    input["value"] = "Eric"
}

Submit a form

if let form = doc.querySelector("form[id='search']") as? Form {
    form.submit()
}

Evaluate some JavaScript

let javaScriptSource = "console.log("test");"
Erik.evaluate(javaScript:javaScriptSource) { (obj, err) -> Void in
    if let error = err {
        switch error {
            case ErikError.javaScriptError(let message):
            print(message)
            default :
            print("\(error)")
        }
    }
    else if let capturedValue = obj {
        // do something according to result
    }
}

capturedValue is the content of JavaScript variable resultErik Affect this variable in your JavaScript code.

let javaScriptSource = "console.log('test'); var resultErik = 1 + 1;"

Warning about DOM change

:warning: All action on Dom use JavaScript and do not modify the actual Document object and its children Element.

You must use currentContent to get a refreshed Document object

Get current content

Erik.currentContent { (obj, err) -> Void in
    if let error = err {
    }
    else if let document = obj {
       // HTML Inspection
    }
}

Using Future

As an optional feature, you can use Future/Promise ( Erik use frameworks BrightFutures & Result)

Example to submit a google search

let url = NSURL(string:"https://www.google.com")!
let value = "Erik The Phantom of Opera"
// visit
var future: Future<Document, NSError> = Erik.visitFuture(url: url)
// fill input field
future = future.flatMap { document -> Future<Document, NSError> in
    if let input = document.querySelector("input[name='q']") {
        input["value"] = value
    }
    if let form = document.querySelector("form[name=\"f\"]") as? Form {
        form.submit()
    }
    return Erik.currentContentFuture()
}
// finally get final result as success or error
future.onSuccess { document in
    // parse result
}
future.onFailure { error in
    print("\(error)")
}

Limitation

On iOS 9 and macOS 10.11, you need to ensure you use https://, because iOS 9 and macOS 10.11 do not like apps sending or receiving data insecurely. If this something you want to override, click here to read about App Transport Security.

Setup

Using cocoapods

CocoaPods is a centralized dependency manager for Objective-C and Swift. Go here to learn more.

  1. Add the project to your Podfile.

     use_frameworks!
    
     pod 'Erik'
     // or specific target
     target :test do
        pod 'Erik'
     end
    
  2. Run pod install and open the .xcworkspace file to launch Xcode.

Optional Future

Add pod 'Erik/Future' to your Podfile and run pod install.

Using carthage

Carthage is a decentralized dependency manager for Objective-C and Swift.

  1. Add the project to your Cartfile.

     github "phimage/Erik"
    

Roadmap

Why Erik?

A well known headless browser is named PhantomJS and a very well known browser is Opera.

As a tribute I use Erik, firstname of the title character from Gaston Leroux’s novel Le Fantôme de l’Opéra best known to English speakers as The Phantom of Opera

My name is also Erik. So egotistical to call a project using its firstname isn’t it.

My only justification is that I was playing Metal Gear Solid V and the creator Hideo Kojima name appears over 100 times in the game. Coincidentally the full name of the game is Metal Gear Solid V : The Phantom Pain.

License

The MIT License. See the LICENSE file for more information.