- Notifications
You must be signed in to change notification settings - Fork47
Erik is an headless browser based on WebKit. An headless browser allow to run functional tests, to access and manipulate webpages using javascript.
License
phimage/Erik
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Erik is aheadless browser based onWebKit and HTML parserKanna.
An headless browser allow to run functional tests, to access and manipulate webpages using javascript.
letbrowser=Erik.visit(url: url){ document, errorin // browse HTML element, click, submit form and more}
Go to an url
Erik.visit(url: url){ object, erroriniflet e= error{}elseiflet doc= object{ // HTML Inspection}}
How to get current url?
iflet url=Erik.currentURL{..}
For multiple browsing you can create an instance of headless browser and use same functions
letbrowser=Erik()browser.visitURL...
Search for nodes byCSS selector
forlinkin doc.querySelectorAll("a, link"){print(link.text)print(link["href"])}
iflet input= doc.querySelectorAll("input[name=\"user\"]").first{input["value"]="Eric"}
iflet form= doc.querySelector("form[id='search']")as?Form{ form.submit()}
letjavaScriptSource="console.log("test");"Erik.evaluate(#"\(error)") } } else if let capturedValue = obj { // do something according to result }}
capturedValue
is the content of JavaScript variableresultErik
Affect this variable in your JavaScript code.
letjavaScriptSource="console.log('test'); var resultErik = 1 + 1;"
Document
object and its childrenElement
.
You must usecurrentContent
to get a refreshedDocument
object
Erik.currentContent{(obj, err)->Voidiniflet error= err{}elseiflet document= obj{ // HTML Inspection}}
As an optional feature, you can useFuture/Promise ( Erik use frameworksBrightFutures &Result)
Example to submit a google search
leturl=NSURL(string:"https://www.google.com")!letvalue="Erik The Phantom of Opera"// visitvarfuture:Future<Document,NSError>=Erik.visitFuture(url: url)// fill input fieldfuture= future.flatMap{ document->Future<Document,NSError>iniflet input= document.querySelector("input[name='q']"){input["value"]= value}iflet form= document.querySelector("form[name=\"f\"]")as?Form{ form.submit()}returnErik.currentContentFuture()}// finally get final result as success or errorfuture.onSuccess{ documentin // parse result}future.onFailure{ errorinprint("\(error)")}
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 aboutApp Transport Security.
Usingcocoapods
CocoaPods is a centralized dependency manager forObjective-C and Swift. Gohereto learn more.
Add the project to yourPodfile.
use_frameworks!pod'Erik'//orspecifictargettarget:testdopod'Erik'end
Run
pod install
and open the.xcworkspace
file to launch Xcode.
Addpod 'Erik/Future'
to yourPodfile
and runpod install
.
Carthage is a decentralized dependency manager for Objective-C and Swift.
Add the project to yourCartfile.
github "phimage/Erik"
- (WIP) WKWebView screenshot (webkit view privates api?)
A well known headless browser is namedPhantomJS and a very well known browser isOpera.
As a tribute I useErik, firstname of the title character from Gaston Leroux's novel Le Fantôme de l'Opéra best known to English speakers asThe 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 SolidV and the creator Hideo Kojima name appears over 100 times in the game. Coincidentally the full name of the game is Metal Gear SolidV : ThePhantom Pain.
The MIT License. See the LICENSE file for more information.
About
Erik is an headless browser based on WebKit. An headless browser allow to run functional tests, to access and manipulate webpages using javascript.