abstract class Component : ClickAndNavigateSupport, ComponentMappingSupport, JavaScriptSupport, SearchContext, WaitingSupport
A component is a reusable piece of functionality that can be shared among several pages, and which interaction can be performed independently of other pieces in the web page.
// Given a component for the Kotlin's features
class Feature(page: Page, element: WebElement) : Component(page, element) {
val title by lazy {
`$`("h3.feature-title", 0).text
}
val description by lazy {
`$`("p.feature-description", 0).text
}
override fun toString(): String {
return "Feature(title = $title, description = $description)"
}
}
// And a component of the section on features
class FeaturesSection(page: Page, element: WebElement) : Component(page, element) {
val title by lazy {
`$`("h2.section-header", 0).text
}
val features by lazy {
`$`("li.kotlin-feature").component(::Feature)
}
}
// And the Kotlin's website index page
class IndexPage(browser: Browser) : Page(browser) {
override val url = "https://kotlinlang.org/"
override val at = at {
title == "Kotlin Programming Language"
}
val featuresSection by lazy {
`$`("section.kotlin-overview-section._features", 0).component(::FeaturesSection)
}
}
Browser.drive(driverFactory) {
// When I visit the Kotlin's website index page
val indexPage = to(::IndexPage)
// Then the header for the features section must be correct
assertEquals(indexPage.featuresSection.title, "Why Kotlin?")
val actualFeatures = indexPage.featuresSection.features.associateBy(
Feature::title, Feature::description
)
// And the features should be correctly described inside said section
assertEquals(actualFeatures, expectedFeatures)
}
page
- the page the component is linked to.
rootElement
- the component's root element.
Component(page: Page, rootElement: WebElement)
Create a new component, given the page it's linked to and its root element. |
val browser: Browser
The browser used by the component in order to interact with the underlying web content. |
|
val page: Page
the page the component is linked to. |
|
val rootElement: WebElement
the component's root element. |
fun SearchContext.$(selector: String, index: Int): WebElement
Find the nth element that can be located by the given CSS selector. fun SearchContext.$(selector: String, range: IntRange): List<WebElement>
Find all the elements that can be located by the given CSS selector, restricted by the specified range. fun SearchContext.$(selector: String, vararg indices: Int): List<WebElement>
Find all the elements that can be located by the given CSS selector, restricted by the specified indices. (If no index is provided, then all matching elements will be returned.) |
|
fun SearchContext.find(selector: String, index: Int): WebElement
Find the nth element that can be located by the given CSS selector. fun SearchContext.find(selector: String, range: IntRange): List<WebElement>
Find all the elements that can be located by the given CSS selector, restricted by the specified range. fun SearchContext.find(selector: String, vararg indices: Int): List<WebElement>
Find all the elements that can be located by the given CSS selector, restricted by the specified indices. (If no index is provided, then all matching elements will be returned.) |