-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathElement.swift
122 lines (106 loc) · 5.09 KB
/
Element.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import struct Foundation.TimeInterval
// Represents an element in the WebDriver protocol.
public struct Element {
var webDriver: WebDriver { session.webDriver }
public let session: Session
public let id: String
public init(session: Session, id: String) {
self.session = session
self.id = id
}
/// The element's textual contents.
public var text: String {
get throws {
try webDriver.send(Requests.ElementText(
session: session.id, element: id)).value
}
}
/// The x and y location of the element relative to the screen top left corner.
public var location: (x: Int, y: Int) {
get throws {
let responseValue = try webDriver.send(Requests.ElementLocation(
session: session.id, element: id)).value
return (responseValue.x, responseValue.y)
}
}
/// Gets the width and height of this element in pixels.
public var size: (width: Int, height: Int) {
get throws {
let responseValue = try webDriver.send(Requests.ElementSize(
session: session.id, element: id)).value
return (responseValue.width, responseValue.height)
}
}
/// Gets a value indicating whether this element is currently displayed.
public var displayed: Bool {
get throws {
try webDriver.send(Requests.ElementDisplayed(
session: session.id, element: id)).value
}
}
/// Gets a value indicating whether this element is currently enabled.
public var enabled: Bool {
get throws {
try webDriver.send(Requests.ElementEnabled(
session: session.id, element: id)).value
}
}
/// Clicks this element.
public func click(retryTimeout: TimeInterval? = nil) throws {
let request = Requests.ElementClick(session: session.id, element: id)
try session.sendInteraction(request, retryTimeout: retryTimeout)
}
/// Clicks this element via touch.
public func touchClick(kind: TouchClickKind = .single, retryTimeout: TimeInterval? = nil) throws {
let request = Requests.SessionTouchClick(session: session.id, kind: kind, element: id)
try session.sendInteraction(request, retryTimeout: retryTimeout)
}
/// Double clicks an element by id.
public func doubleClick(retryTimeout: TimeInterval? = nil) throws {
let request = Requests.SessionTouchDoubleClick(session: session.id, element: id)
try session.sendInteraction(request, retryTimeout: retryTimeout)
}
/// - Parameters:
/// - retryTimeout: The amount of time to retry the operation. Overrides the implicit interaction retry timeout.
/// - element: Element id to click
/// - xOffset: The x offset in pixels to flick by.
/// - yOffset: The y offset in pixels to flick by.
/// - speed: The speed in pixels per seconds.
public func flick(xOffset: Double, yOffset: Double, speed: Double, retryTimeout: TimeInterval? = nil) throws {
let request = Requests.SessionTouchFlickElement(session: session.id, element: id, xOffset: xOffset, yOffset: yOffset, speed: speed)
try session.sendInteraction(request, retryTimeout: retryTimeout)
}
/// Search for an element using a given locator, starting from this element.
/// - Parameter locator: The locator strategy to use.
/// - Parameter waitTimeout: The amount of time to wait for element existence. Overrides the implicit wait timeout.
/// - Returns: The element that was found, if any.
@discardableResult // for use as an assertion
public func findElement(locator: ElementLocator, waitTimeout: TimeInterval? = nil) throws -> Element {
try session.findElement(startingAt: self, locator: locator, waitTimeout: waitTimeout)
}
/// Search for elements using a given locator, starting from this element.
/// - Parameter using: The locator strategy to use.
/// - Parameter waitTimeout: The amount of time to wait for element existence. Overrides the implicit wait timeout.
/// - Returns: The elements that were found, or an empty array.
public func findElements(locator: ElementLocator, waitTimeout: TimeInterval? = nil) throws -> [Element] {
try session.findElements(startingAt: self, locator: locator, waitTimeout: waitTimeout)
}
/// Gets an attribute of this element.
/// - Parameter name: the attribute name.
/// - Returns: the attribute value string.
public func getAttribute(name: String) throws -> String {
try webDriver.send(Requests.ElementAttribute(
session: session.id, element: id, attribute: name)).value
}
/// Sends key presses to this element.
/// - Parameter keys: A key sequence according to the WebDriver spec.
public func sendKeys(_ keys: Keys) throws {
try webDriver.send(Requests.ElementValue(
session: session.id, element: id, value: [keys.rawValue]))
}
/// Clears the text of an editable element.
public func clear() throws {
try webDriver.send(Requests.ElementClear(
session: session.id, element: id))
}
}