This library allows you to work with selenium webdriver on server-side swift
Adding the dependency
Add the following entry in your Package.swift to start using SwiftWebDriver:
.package(url: "https://github.com/ashi-psn/SwiftWebDriver.git", from: "0.1.1")
and add dependency to your target:
.target(name: "MyApp", dependencies: [.product(name: "SwiftWebDriver", package: "SwiftWebDriver")]),
docker-compose build // build image
docker-compose up -d // Run httpd and chromedriver
docker-compose run test // Run Tests in Docker
let chromeOption = ChromeOptions(
args: [
Args(.headless),
]
)
// create webdriver object
// default webdriver url is http://localhost:4444
// if you need to change webdriver url, use `driverURL` parameter.
let driver = WebDriver(
driver: ChromeDriver(
browserObject: chromeOption
)
)
//create session
let sessionId = try await driver.start()
//stop session
try await driver.stop()
// find element by class name
let classText = try await driver.findElement(.css(.class("classElement")))
// find element by id name
let idElement = try await driver.findElement(.css(.id("idElement")))
// find element by name parameter
let nameElement = try await driver.findElement(.css(.name("nameElement")))
let element = try await driver.findElement(.xpath("//*[@id=\"inParentSingleElement\"]"))
let element = try await driver.findElement(.linkText("go to next page"))
let element = try await driver.findElement(.particalLinkText("go"))
Both Selectors support the findElements Method. findElements method return Elements, such as Array.
Element.text method return value.
let text = try await driver
.findElement(.css(.id("myValue")))
.text()
Elements and Elements can use chain methods to narrow down the elements
let element = try await driver
.findElement(.css(.id("parent"))
.findElement(.css(.id("child")))
.text()