Skip to content

Commit

Permalink
Merge branch 'main' into squid/execute-scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Squidonomics authored Nov 15, 2023
2 parents 25d1fc3 + 7754084 commit ad2d044
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 6 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
name: Build & Test

on: [push, workflow_dispatch]
on:
push:
workflow_dispatch:
pull_request:
branches:
- main

jobs:
build:
Expand Down
8 changes: 4 additions & 4 deletions Docs/SupportedAPIs.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ Contributions to expand support to unimplemented functionality are always welcom
| POST | `/session/:sessionId/touch/up` | Supported | `Session.touchUp()` |
| GET | `/session/:sessionId/url` | Not supported| `Session.url` |
| POST | `/session/:sessionId/url` | Not supported| `Session.url()` |
| DELETE | `/session/:sessionId/window` | Supported | Not implemented |
| POST | `/session/:sessionId/window` | Supported | Not implemented |
| DELETE | `/session/:sessionId/window` | Supported | `Session.close()` |
| POST | `/session/:sessionId/window` | Supported | `Session.focus()` |
| POST | `/session/:sessionId/window/maximize` | Supported | Not implemented |
| POST | `/session/:sessionId/window/size` | Supported | Not implemented |
| GET | `/session/:sessionId/window/size` | Supported | Not implemented |
| POST | `/session/:sessionId/window/:windowHandle/size` | Supported | Not implemented |
| GET | `/session/:sessionId/window/:windowHandle/size` | Supported | Not implemented |
| POST | `/session/:sessionId/window/:windowHandle/size` | Supported | `Session.resize()` |
| GET | `/session/:sessionId/window/:windowHandle/size` | Supported | `Session.size()` |
| POST | `/session/:sessionId/window/:windowHandle/position` | Supported | Not implemented |
| GET | `/session/:sessionId/window/:windowHandle/position` | Supported | Not implemented |
| POST | `/session/:sessionId/window/:windowHandle/maximize` | Supported | Not implemented |
Expand Down
61 changes: 60 additions & 1 deletion Sources/WebDriver/Requests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ public enum Requests {
}
}

// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidexecute
// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidexecute
public enum SessionScript {
public struct Post: Request {
public var session: String
Expand Down Expand Up @@ -494,6 +494,65 @@ public enum Requests {
public struct Body: Codable {
public var script: String
public var args: [String]

// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidwindow
public enum SessionWindow {
public struct Post: Request {
public var session: String
public var name: String

public var pathComponents: [String] { ["session", session, "window"] }
public var method: HTTPMethod { .post }
public var body: Body { .init(name: name) }

public struct Body: Codable {
public var name: String
}
}

public struct Delete: Request {
public var session: String
public var name: String

public var pathComponents: [String] { ["session", session, "window"] }
public var method: HTTPMethod { .delete }
public var body: Body { .init(name: name) }

public struct Body: Codable {
public var name: String
}
}
}

// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidwindowwindowhandlesize
public enum SessionWindowSize {
public struct Post: Request {
public var session: String
public var windowHandle: String
public var width: Int
public var height: Int

public var pathComponents: [String] { ["session", session, "window", windowHandle, "size"] }
public var method: HTTPMethod { .post }
public var body: Body { .init(width: width, height: height) }

public struct Body: Codable {
public var width: Int
public var height: Int
}
}

public struct Get: Request {
public var session: String
public var windowHandle: String

public var pathComponents: [String] { ["session", session, "window", windowHandle, "size"] }
public var method: HTTPMethod { .get }

public typealias Response = ResponseWithValue<ResponseValue>
public struct ResponseValue: Codable {
public var width: Int
public var height: Int
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions Sources/WebDriver/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,31 @@ public class Session {
try webDriver.send(Requests.SessionKeys(session: id, value: value))
}

/// Change focus to another window
/// - Parameter name: The window to change focus to
public func focus(window name: String) throws {
try webDriver.send(Requests.SessionWindow.Post(session: id, name: name))
}

/// Close selected window
/// - Parameter name: The selected window to close
public func close(window name: String) throws {
try webDriver.send(Requests.SessionWindow.Delete(session: id, name: name))
}

public func size(window handle: String) throws -> (width: Int, height: Int) {
let response = try webDriver.send(Requests.SessionWindowSize.Get(session: id, windowHandle: handle))
return (width: response.value.width, height: response.value.height)
}

/// Change the size of the specified window
/// - Parameter name: URL parameter is "current", the currently active window will be resized.
/// - Parameter width: The new window width.
/// - Parameter height: The new window height
public func resize(window handle: String, width: Int, height: Int) throws {
try webDriver.send(Requests.SessionWindowSize.Post(session: id, windowHandle: handle, width: width, height: height))
}

/// Deletes the current session.
public func delete() throws {
guard shouldDelete else { return }
Expand Down
21 changes: 21 additions & 0 deletions Tests/UnitTests/APIToRequestMappingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,26 @@ class APIToRequestMappingTests: XCTestCase {
XCTAssertEqual($0.args, [])
return CodableNone()
}

func testWindow() throws {
let mockWebDriver: MockWebDriver = MockWebDriver()
let session = Session(webDriver: mockWebDriver, existingId: "mySession")
mockWebDriver.expect(path: "session/mySession/window", method: .post)
try session.focus(window: "myWindow")

mockWebDriver.expect(path: "session/mySession/window", method: .delete)
try session.close(window: "myWindow")
}

func testWindowHandleSize() throws {
let mockWebDriver: MockWebDriver = MockWebDriver()
let session = Session(webDriver: mockWebDriver, existingId: "mySession")
mockWebDriver.expect(path: "session/mySession/window/myWindow/size", method: .post)
try session.resize(window: "myWindow", width: 500, height: 500)

mockWebDriver.expect(path: "session/mySession/window/myWindow/size", method: .get, type: Requests.SessionWindowSize.Get.self) {
ResponseWithValue(.init(width: 500, height: 500))
}
XCTAssert(try session.size(window: "myWindow") == (width: 500, height: 500))
}
}

0 comments on commit ad2d044

Please sign in to comment.