1+ import { Locator , WebElement , until } from "selenium-webdriver" ;
2+ import { AbstractElement } from "../AbstractElement" ;
3+
4+ /**
5+ * Page object representing a user-contributed panel implemented using a Webview.
6+ */
7+ export class WebviewView extends AbstractElement {
8+
9+ constructor ( ) {
10+ super ( WebviewView . locators . Workbench . constructor ) ;
11+ }
12+
13+ private static handle : string | undefined ;
14+
15+ /**
16+ * Search for an element inside the webview iframe.
17+ * Requires webdriver being switched to the webview iframe first.
18+ * (Will attempt to search from the main DOM root otherwise)
19+ *
20+ * @param locator webdriver locator to search by
21+ * @returns promise resolving to WebElement when found
22+ */
23+ async findWebElement ( locator : Locator ) : Promise < WebElement > {
24+ return this . getDriver ( ) . findElement ( locator ) ;
25+ }
26+
27+ /**
28+ * Search for all element inside the webview iframe by a given locator
29+ * Requires webdriver being switched to the webview iframe first.
30+ * (Will attempt to search from the main DOM root otherwise)
31+ *
32+ * @param locator webdriver locator to search by
33+ * @returns promise resolving to a list of WebElement objects
34+ */
35+ async findWebElements ( locator : Locator ) : Promise < WebElement [ ] > {
36+ return this . getDriver ( ) . findElements ( locator ) ;
37+ }
38+
39+ /**
40+ * Switch the underlying webdriver context to the webview iframe.
41+ * This allows using the findWebElement methods.
42+ * Note that only elements inside the webview iframe will be accessible.
43+ * Use the switchBack method to switch to the original context.
44+ */
45+ async switchToFrame ( ) : Promise < void > {
46+ if ( ! WebviewView . handle ) {
47+ WebviewView . handle = await this . getDriver ( ) . getWindowHandle ( ) ;
48+ }
49+
50+ const view = await this . getDriver ( ) . wait ( until . elementLocated ( WebviewView . locators . WebView . iframe ) ) as WebElement ;
51+
52+ await this . getDriver ( ) . switchTo ( ) . frame ( view ) ;
53+
54+ await this . getDriver ( ) . wait ( until . elementLocated ( WebviewView . locators . WebView . activeFrame ) , 5000 ) ;
55+ const frame = await this . getDriver ( ) . findElement ( WebviewView . locators . WebView . activeFrame ) ;
56+ await this . getDriver ( ) . switchTo ( ) . frame ( frame ) ;
57+ }
58+
59+ /**
60+ * Switch the underlying webdriver back to the original window
61+ */
62+ async switchBack ( ) : Promise < void > {
63+ if ( ! WebviewView . handle ) {
64+ WebviewView . handle = await this . getDriver ( ) . getWindowHandle ( ) ;
65+ }
66+ return this . getDriver ( ) . switchTo ( ) . window ( WebviewView . handle ) ;
67+ }
68+
69+ }
0 commit comments