balin / com.github.epadronu.balin.core / ComponentMappingSupport / component

component

abstract fun <T : Component> WebElement.component(factory: (Page, WebElement) -> T): T

Create a new component with the given WebElement as its root element.

Depending on how the component is designed, the interactions with the underlying web content may be performed relatively to the component's root element.

// 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)
}

Parameters

factory - provides an instance of the component, given the page it's linked to and its root element.

Receiver
The component's root element.

Return
An instance of the desired component.

abstract fun <T : Component> List<WebElement>.component(factory: (Page, WebElement) -> T): List<T>

Map the given collection of WebElement into a collection of com.github.epadronu.balin.core.Component.

// 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)
}

Parameters

factory - provides an instance of the component, given the page it's linked to and its root element.

See Also

WebElement.component

Receiver
The collection to be mapped.

Return
A collection of com.github.epadronu.balin.core.Component.