1+ import { until , WebElement } from "selenium-webdriver" ;
2+ import WebviewMixin from "../WebviewMixin" ;
13import { Editor } from "./Editor" ;
2- import { Locator , until , WebElement } from "selenium-webdriver" ;
34
45/**
56 * Page object representing an open editor containing a web view
67 */
7- export class WebView extends Editor {
8-
9- private static handle : string | undefined ;
10-
11- /**
12- * Search for an element inside the webview iframe.
13- * Requires webdriver being switched to the webview iframe first.
14- * (Will attempt to search from the main DOM root otherwise)
15- *
16- * @param locator webdriver locator to search by
17- * @returns promise resolving to WebElement when found
18- */
19- async findWebElement ( locator : Locator ) : Promise < WebElement > {
20- return await this . getDriver ( ) . findElement ( locator ) ;
21- }
22-
23- /**
24- * Search for all element inside the webview iframe by a given locator
25- * Requires webdriver being switched to the webview iframe first.
26- * (Will attempt to search from the main DOM root otherwise)
27- *
28- * @param locator webdriver locator to search by
29- * @returns promise resolving to a list of WebElement objects
30- */
31- async findWebElements ( locator : Locator ) : Promise < WebElement [ ] > {
32- return await this . getDriver ( ) . findElements ( locator ) ;
33- }
34-
35- /**
36- * Switch the underlying webdriver context to the webview iframe.
37- * This allows using the findWebElement methods.
38- * Note that only elements inside the webview iframe will be accessible.
39- * Use the switchBack method to switch to the original context.
40- */
41- async switchToFrame ( ) : Promise < void > {
42- if ( ! WebView . handle ) {
43- WebView . handle = await this . getDriver ( ) . getWindowHandle ( ) ;
44- }
8+ class WebViewBase extends Editor {
459
10+ async getViewToSwitchTo ( handle : string ) : Promise < WebElement | undefined > {
4611 const handles = await this . getDriver ( ) . getAllWindowHandles ( ) ;
4712 for ( const handle of handles ) {
4813 await this . getDriver ( ) . switchTo ( ) . window ( handle ) ;
@@ -52,35 +17,23 @@ export class WebView extends Editor {
5217 return ;
5318 }
5419 }
55- await this . getDriver ( ) . switchTo ( ) . window ( WebView . handle ) ;
20+ await this . getDriver ( ) . switchTo ( ) . window ( handle ) ;
5621
57- const reference = await this . findElement ( WebView . locators . EditorView . webView ) ;
58- const containers = await this . getDriver ( ) . wait ( until . elementsLocated ( WebView . locators . WebView . container ( await reference . getAttribute ( WebView . locators . WebView . attribute ) ) ) , 5000 ) ;
22+ const reference = await this . findElement ( WebViewBase . locators . EditorView . webView ) ;
23+ const containers = await this . getDriver ( ) . wait ( until . elementsLocated ( WebViewBase . locators . WebView . container ( await reference . getAttribute ( WebViewBase . locators . WebView . attribute ) ) ) , 5000 ) ;
5924
60- const view = await containers [ 0 ] . getDriver ( ) . wait ( async ( ) => {
25+ return await containers [ 0 ] . getDriver ( ) . wait ( async ( ) => {
6126 for ( let index = 0 ; index < containers . length ; index ++ ) {
62- const tries = await containers [ index ] . findElements ( WebView . locators . WebView . iframe ) ;
27+ const tries = await containers [ index ] . findElements ( WebViewBase . locators . WebView . iframe ) ;
6328 if ( tries . length > 0 ) {
6429 return tries [ 0 ] ;
6530 }
6631 }
6732 return undefined ;
6833 } , 5000 ) as WebElement ;
69-
70- await this . getDriver ( ) . switchTo ( ) . frame ( view ) ;
71-
72- await this . getDriver ( ) . wait ( until . elementLocated ( WebView . locators . WebView . activeFrame ) , 5000 ) ;
73- const frame = await this . getDriver ( ) . findElement ( WebView . locators . WebView . activeFrame ) ;
74- await this . getDriver ( ) . switchTo ( ) . frame ( frame ) ;
7534 }
7635
77- /**
78- * Switch the underlying webdriver back to the original window
79- */
80- async switchBack ( ) : Promise < void > {
81- if ( ! WebView . handle ) {
82- WebView . handle = await this . getDriver ( ) . getWindowHandle ( ) ;
83- }
84- return await this . getDriver ( ) . switchTo ( ) . window ( WebView . handle ) ;
85- }
86- }
36+ }
37+
38+ export const WebView = WebviewMixin ( WebViewBase ) ;
39+ export type WebView = InstanceType < typeof WebView > ;
0 commit comments