How to use PageTests class of com.github.epadronu.balin.core package

Best Balin code snippet using com.github.epadronu.balin.core.PageTests

Run Balin automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Page.kt

Source: Page.kt Github

copy
1/******************************************************************************
2 * Copyright 2016 Edinson E. Padrón Urdaneta
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17/* ***************************************************************************/
18package com.github.epadronu.balin.core
19/* ***************************************************************************/
20
21/* ***************************************************************************/
22import org.openqa.selenium.SearchContext
23import org.openqa.selenium.WebElement
24/* ***************************************************************************/
25
26/* ***************************************************************************/
27/**
28 * This class is the corner stone for Balin's implementation of the
29 * _Page Object Design Pattern_. All classes that model a Web page/view most
30 * extend this one.
31 *
32 * @sample com.github.epadronu.balin.core.PageTests.model_a_page_into_a_page_object_navigate_and_interact_with
33 *
34 * @param browser the browser used by the page in order to interact with the underlying web content.
35 * @constructor Create a new instance with the given browser as its bridge with the web content the page care about.
36 */
37abstract class Page(val browser: Browser) : ClickAndNavigateSupport,
38    ComponentMappingSupport,
39    JavaScriptSupport by browser,
40    SearchContext by browser,
41    WaitingSupport by browser {
42
43    companion object {
44        /**
45         * This method eases the definition of a page's _implicit at verification_.
46         *
47         * @sample com.github.epadronu.balin.core.PageTests.model_a_page_into_a_page_object_navigate_and_interact_with
48         *
49         * @param block context within which you can interact with the browser.
50         * @return The [block] unchanged.
51         */
52        @JvmStatic
53        fun at(block: Browser.() -> Any): Browser.() -> Any = block
54    }
55
56    /**
57     * Defines an optional _implicit verification_ to be checked as soon as the
58     * browser navigates to the page.
59     *
60     * Useful for performing early failure.
61     *
62     * @sample com.github.epadronu.balin.core.PageTests.model_a_page_into_a_page_object_navigate_and_interact_with
63     */
64    open val at: Browser.() -> Any = { true }
65
66    /**
67     * Defines an optional URL, which will be used when invoking
68     * [Browser.to] with a page factory.
69     *
70     * @sample com.github.epadronu.balin.core.PageTests.model_a_page_into_a_page_object_navigate_and_interact_with
71     */
72    open val url: String? = null
73
74    /**
75     * Click on an element and tells the browser it will navigate to the given
76     * page as consequence of such action.
77     *
78     * @sample com.github.epadronu.balin.core.PageTests.use_WebElement_click_in_a_page_to_place_the_browser_at_a_different_page
79     *
80     * @receiver the [WebElement][org.openqa.selenium.WebElement] to be clicked on.
81     * @param factory provides an instance of the page given the driver being used by the browser.
82     * @Returns An instance of the page the browser will navigate to.
83     * @throws PageImplicitAtVerificationException if the page has an _implicit at verification_ which have failed.
84     */
85    override fun <T : Page> WebElement.click(factory: (Browser) -> T): T {
86        this.click()
87
88        return browser.at(factory)
89    }
90
91    override fun <T : Component> WebElement.component(factory: (Page, WebElement) -> T): T = factory(this@Page, this)
92
93    override fun <T : Component> List<WebElement>.component(factory: (Page, WebElement) -> T): List<T> = this.map {
94        factory(this@Page, it)
95    }
96
97    /**
98     * Evaluate the page's _implicit at verification_.
99     *
100     * @return true if the verification passed, false otherwise.
101     */
102    internal fun verifyAt(): Boolean = when (val result = at(browser)) {
103        is Boolean -> result
104        is Unit -> true
105        else -> throw Error("Expressions of type `${result.javaClass.canonicalName}` are not allowed.")
106    }
107}
108/* ***************************************************************************/
109
Full Screen

SearchContextExtensions.kt

Source: SearchContextExtensions.kt Github

copy
1/******************************************************************************
2 * Copyright 2016 Edinson E. Padrón Urdaneta
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17/* ***************************************************************************/
18package com.github.epadronu.balin.extensions
19/* ***************************************************************************/
20
21/* ***************************************************************************/
22import org.openqa.selenium.By
23import org.openqa.selenium.SearchContext
24import org.openqa.selenium.WebElement
25/* ***************************************************************************/
26
27/* ***************************************************************************/
28/**
29 * Find the nth element that can be located by the given CSS selector.
30 *
31 * This is an alternative to the `find` method.
32 *
33 * @param selector the CSS selector to be used for locating the element.
34 * @param index the index of the element to be returned.
35 * @return The nth matching element.
36 * @throws java.lang.IndexOutOfBoundsException for an illegal index value.
37 * @sample com.github.epadronu.balin.core.PageTests.model_a_page_into_a_page_object_navigate_and_interact_with
38 */
39fun SearchContext.`$`(selector: String, index: Int): WebElement = find(selector, index)
40
41/**
42 * Find all the elements that can be located by the given CSS selector,
43 * restricted by the specified range.
44 *
45 * This is an alternative to the `find` method.
46 *
47 * @param selector the CSS selector to be used for locating the elements.
48 * @param range specify the indices of the elements to be returned.
49 * @return The matching elements restricted by the specified range.
50 * @throws java.lang.IndexOutOfBoundsException for illegal index values within the range.
51 * @sample com.github.epadronu.balin.core.PageTests.model_a_page_into_a_page_object_navigate_and_interact_with
52 */
53fun SearchContext.`$`(selector: String, range: IntRange): List<WebElement> = find(selector, range)
54
55/**
56 * Find all the elements that can be located by the given CSS selector,
57 * restricted by the specified indices. (If no index is provided, then all
58 * matching elements will be returned.)
59 *
60 * This is an alternative to the `find` method.
61 *
62 * @param selector the CSS selector to be used for locating the elements.
63 * @param indices the indices of the elements to be returned.
64 * @return The matching elements restricted by the specified indices. (Or all matching elements if no index is provided.)
65 * @throws java.lang.IndexOutOfBoundsException for illegal index values.
66 * @sample com.github.epadronu.balin.core.PageTests.model_a_page_into_a_page_object_navigate_and_interact_with
67 */
68fun SearchContext.`$`(selector: String, vararg indices: Int): List<WebElement> = find(selector, *indices)
69
70/**
71 * Find the nth element that can be located by the given CSS selector.
72 *
73 * @param selector the CSS selector to be used for locating the element.
74 * @param index the index of the element to be returned.
75 * @return The nth matching element.
76 * @throws java.lang.IndexOutOfBoundsException for an illegal index value.
77 * @sample com.github.epadronu.balin.core.PageTests.model_a_page_into_a_page_object_navigate_and_interact_with
78 */
79fun SearchContext.find(selector: String, index: Int): WebElement = findElements(By.cssSelector(selector))[index]
80
81/**
82 * Find all the elements that can be located by the given CSS selector,
83 * restricted by the specified range.
84 *
85 * @param selector the CSS selector to be used for locating the elements.
86 * @param range specify the indices of the elements to be returned.
87 * @return The matching elements restricted by the specified range.
88 * @throws java.lang.IndexOutOfBoundsException for illegal index values within the range.
89 * @sample com.github.epadronu.balin.core.PageTests.model_a_page_into_a_page_object_navigate_and_interact_with
90 *
91 */
92fun SearchContext.find(selector: String, range: IntRange): List<WebElement> = findElements(By.cssSelector(selector))
93    .slice(range)
94
95/**
96 * Find all the elements that can be located by the given CSS selector,
97 * restricted by the specified indices. (If no index is provided, then all
98 * matching elements will be returned.)
99 *
100 * @param selector the CSS selector to be used for locating the elements.
101 * @param indices the indices of the elements to be returned.
102 * @return The matching elements restricted by the specified indices. (Or all matching elements if no index is provided.)
103 * @throws java.lang.IndexOutOfBoundsException for illegal index values.
104 * @sample com.github.epadronu.balin.core.PageTests.model_a_page_into_a_page_object_navigate_and_interact_with
105 */
106fun SearchContext.find(selector: String, vararg indices: Int): List<WebElement> {
107    val elements = findElements(By.cssSelector(selector))
108
109    if (indices.isEmpty()) {
110        return elements
111    }
112
113    return elements.slice(indices.asList())
114}
115/* ***************************************************************************/
116
Full Screen

MissingPageUrlException.kt

Source: MissingPageUrlException.kt Github

copy
1/******************************************************************************
2 * Copyright 2016 Edinson E. Padrón Urdaneta
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17/* ***************************************************************************/
18package com.github.epadronu.balin.exceptions
19/* ***************************************************************************/
20
21/* ***************************************************************************/
22/**
23 * This exception is thrown when either
24 * [com.github.epadronu.balin.core.Browser.to] or [com.github.epadronu.balin.core.Page.click]
25 * are used with a page that has not defined a url.
26 *
27 * @sample com.github.epadronu.balin.core.PageTests.model_a_page_into_a_page_object_with_no_url_and_try_to_navigate_to_it
28 */
29class MissingPageUrlException : BalinException("The page doesn't have a URL for the browser to navigate to")
30/* ***************************************************************************/
31
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie