Skip to content

Commit 09eec10

Browse files
feat(ChromeDriver): implement get property method
added it to driver protocols, and wrote integration tests that are working as expected
1 parent 584d107 commit 09eec10

6 files changed

Lines changed: 89 additions & 45 deletions

File tree

Sources/SwiftWebDriver/WebDriver.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,13 @@ public class WebDriver<T: Driver> {
134134
try await driver.setAttribute(element: element, attributeName: attributeName, newValue: newValue)
135135
}
136136

137-
public func getProperty(element: Element, property: String) async throws -> PostExecuteResponse {
138-
try await driver.getProperty(element: element, property: property)
137+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
138+
public func getProperty(element: Element, propertyName: String) async throws -> PostExecuteResponse {
139+
try await driver.getProperty(element: element, propertyName: propertyName)
140+
}
141+
142+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
143+
public func setProperty(element: Element, propertyName: String, newValue: String) async throws {
144+
try await driver.setProperty(element: element, propertyName: propertyName, newValue: newValue)
139145
}
140146
}

Sources/SwiftWebDriver/WebDrivers/Chrome/ChromeDriver.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,16 +309,29 @@ public class ChromeDriver: Driver {
309309
try await execute(script, args: args, type: .sync)
310310
}
311311

312-
public func getProperty(element: Element, property: String) async throws -> PostExecuteResponse {
312+
public func getProperty(element: Element, propertyName: String) async throws -> PostExecuteResponse {
313313
let script = "return arguments[0][arguments[1]];"
314314

315315
let args: [AnyEncodable] = [
316316
AnyEncodable(["element-6066-11e4-a52e-4f735466cecf": element.elementId]),
317-
AnyEncodable(property)
317+
AnyEncodable(propertyName)
318318
]
319319
return try await execute(script, args: args, type: .sync)
320320
}
321321

322+
public func setProperty(element: Element, propertyName: String,
323+
newValue: String) async throws
324+
{
325+
let script = "arguments[0][arguments[1]] = arguments[2];"
326+
let args: [AnyEncodable] = [
327+
AnyEncodable(["element-6066-11e4-a52e-4f735466cecf": element.elementId]),
328+
AnyEncodable(propertyName),
329+
AnyEncodable(newValue)
330+
]
331+
332+
try await execute(script, args: args, type: .sync)
333+
}
334+
322335
deinit {
323336
let url = url
324337
let sessionId = sessionId

Sources/SwiftWebDriver/WebDrivers/Driver.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,7 @@ public protocol Driver: FindElementProtocol {
4949

5050
func setAttribute(element: Element, attributeName: String, newValue: String) async throws
5151

52-
func getProperty(element: Element, property: String) async throws -> PostExecuteResponse
52+
func getProperty(element: Element, propertyName: String) async throws -> PostExecuteResponse
53+
54+
func setProperty(element: Element, propertyName: String, newValue: String) async throws
5355
}
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<!DOCTYPE html>
2+
23
<head>
34
<title>expect title</title>
45
<script>
@@ -8,29 +9,32 @@
89
}
910

1011
function startTimer() {
11-
window.setTimeout(function(){
12+
window.setTimeout(function () {
1213
let element = document.getElementById("willAppendChild")
1314
let newElement = document.createElement('p')
1415
newElement.textContent = 'async add element'
1516
newElement.id = "asyncAddElement"
1617
element.appendChild(newElement)
1718
}, 2000)
1819
}
19-
20-
window.setTimeout(function(){
20+
21+
window.setTimeout(function () {
2122
let element = document.getElementById("willDelete")
2223
element.remove()
2324
}, 2000)
2425
</script>
2526
</head>
27+
2628
<body>
2729
<button id="button" onclick="onClick()"></button>
2830
<input type="text" id="attribute" value="expect attribute">
31+
<div id="setproperty"></div>
2932
<input type="text" id="clearInputValue" value="clearInputValue">
30-
<input id="sendValue" type="text" value="">
31-
32-
<div id="willAppendChild" />
33-
<button id="startButton" onclick="startTimer()">start</button>
34-
<button id="willDelete" >Deleting</button>
33+
<input id="sendValue" type="text" value="">
34+
35+
<div id="willAppendChild" />
36+
<button id="startButton" onclick="startTimer()">start</button>
37+
<button id="willDelete">Deleting</button>
3538
</body>
39+
3640
</html>

Tests/SwiftWebDriverIntegrationTests/ChromeDriver/Element/ChromeDriverGetPropertyIntegrationTests.swift

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// ChromeDriverPropertyIntegrationTests.swift
2+
// Copyright (c) 2025 GetAutomaApp
3+
// All source code and related assets are the property of GetAutomaApp.
4+
// All rights reserved.
5+
6+
@testable import SwiftWebDriver
7+
import Testing
8+
9+
@Suite("Chrome Driver Property Integration Tests", .serialized)
10+
internal class ChromeDriverPropertyIntegrationTests: ChromeDriverTest {
11+
@Test("Get Property")
12+
func getProperty() async throws {
13+
page = "elementHandleTestPage.html"
14+
try await driver.navigateTo(urlString: testPageURL.absoluteString)
15+
let updatedElementValue = "NewElementValue"
16+
try await driver.execute("""
17+
let element = document.getElementById('attribute')
18+
element.value = '\(updatedElementValue)'
19+
""")
20+
let element = try await driver.findElement(.css(.id("attribute")))
21+
guard
22+
let elementValue = try await driver.getProperty(element: element, propertyName: "value").value?.stringValue
23+
else {
24+
#expect(Bool(false), "Could not convert element value to string value")
25+
return
26+
}
27+
28+
#expect(elementValue == updatedElementValue)
29+
}
30+
31+
@Test("Set Property")
32+
func setProperty() async throws {
33+
page = "elementHandleTestPage.html"
34+
try await driver.navigateTo(urlString: testPageURL.absoluteString)
35+
let element = try await driver.findElement(.css(.id("setproperty")))
36+
let newPropertyValue = "element new inner text"
37+
38+
try await driver.setProperty(element: element, propertyName: "innerText", newValue: newPropertyValue)
39+
guard
40+
let elementInnerTextValue = try await driver.getProperty(element: element, propertyName: "innerText").value?
41+
.stringValue
42+
else {
43+
#expect(Bool(false), "Could not convert element innerText value to string value")
44+
return
45+
}
46+
47+
#expect(newPropertyValue == elementInnerTextValue)
48+
}
49+
50+
deinit {}
51+
}

0 commit comments

Comments
 (0)