Skip to content

Instantly share code, notes, and snippets.

@amy
Last active November 15, 2016 06:00
Show Gist options
  • Select an option

  • Save amy/e914ad86bbf535b3b53a9aa2a8b990ef to your computer and use it in GitHub Desktop.

Select an option

Save amy/e914ad86bbf535b3b53a9aa2a8b990ef to your computer and use it in GitHub Desktop.
// creates a New Session
// returns Session ID
func createNewSession() ->String {
let urlString = "http://" + self.host + ":" + self.port + "/session"
guard let url = URL(string: urlString) else {
print("Error: cannot create URL")
return ""
}
var request = URLRequest(url: url)
request.httpMethod = "POST"
////////////////////
// HTTP POST Body //
////////////////////
let httpBodyDict = [
"desiredCapabilities" : [
"browserName": "chrome",
"version": "",
"platform": "MAC"
]
]
do {
try request.httpBody = JSONSerialization.data(withJSONObject: httpBodyDict)
} catch {
print("ERROR WITH JSON")
}
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
// set up session
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
// create wait group
let group = DispatchGroup()
// make the request
group.enter()
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
if let error = error {
print(error)
}
if let response = response {
print(response)
}
group.leave()
})
task.resume()
group.wait()
return ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment