Last active
November 15, 2016 06:00
-
-
Save amy/e914ad86bbf535b3b53a9aa2a8b990ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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