ブラウジングコンテキスト
コマンド
このセクションでは、ブラウジングコンテキストコマンドに関連する API について説明します。
新しいウィンドウを開く
新しいウィンドウで新しいブラウジングコンテキストを作成します。
void testCreateAWindow() {
BrowsingContext browsingContext = new BrowsingContext(driver, WindowType.WINDOW);
Assertions.assertNotNull(browsingContext.getId());
}
const browsingContext = await BrowsingContext(driver, {
type: 'window',
})
新しいタブを開く
新しいタブで新しいブラウジングコンテキストを作成します。
void testCreateATab() {
BrowsingContext browsingContext = new BrowsingContext(driver, WindowType.TAB);
Assertions.assertNotNull(browsingContext.getId());
}
const browsingContext = await BrowsingContext(driver, {
type: 'tab',
})
既存のウィンドウハンドルを使用する
既存のタブ/ウィンドウのブラウジングコンテキストを作成してコマンドを実行します。
void testCreateABrowsingContextForGivenId() {
String id = driver.getWindowHandle();
BrowsingContext browsingContext = new BrowsingContext(driver, id);
Assertions.assertEquals(id, browsingContext.getId());
}
const id = await driver.getWindowHandle()
const browsingContext = await BrowsingContext(driver, {
browsingContextId: id,
})
参照ブラウジングコンテキストでウィンドウを開く
参照ブラウジングコンテキストは、トップレベルブラウジングコンテキストです。この API を使用すると、新しいウィンドウの作成に使用される参照ブラウジングコンテキストを渡すことができます。実装はオペレーティングシステムに依存します。
void testCreateAWindowWithAReferenceContext() {
BrowsingContext
browsingContext =
new BrowsingContext(driver, WindowType.WINDOW, driver.getWindowHandle());
Assertions.assertNotNull(browsingContext.getId());
}
const browsingContext = await BrowsingContext(driver, {
type: 'window',
referenceContext: await driver.getWindowHandle(),
})
参照ブラウジングコンテキストでタブを開く
参照ブラウジングコンテキストは、トップレベルブラウジングコンテキストです。この API を使用すると、新しいタブの作成に使用される参照ブラウジングコンテキストを渡すことができます。実装はオペレーティングシステムに依存します。
void testCreateATabWithAReferenceContext() {
BrowsingContext
browsingContext =
new BrowsingContext(driver, WindowType.TAB, driver.getWindowHandle());
Assertions.assertNotNull(browsingContext.getId());
}
const browsingContext = await BrowsingContext(driver, {
type: 'tab',
referenceContext: await driver.getWindowHandle(),
})
URL に移動
void testNavigateToAUrl() {
BrowsingContext browsingContext = new BrowsingContext(driver, WindowType.TAB);
NavigationResult info = browsingContext.navigate("https://selenium.dokyumento.jp/selenium/web/bidi/logEntryAdded.html");
Assertions.assertNotNull(browsingContext.getId());
Assertions.assertNotNull(info.getNavigationId());
Assertions.assertTrue(info.getUrl().contains("/bidi/logEntryAdded.html"));
}
let info = await browsingContext.navigate('https://selenium.dokyumento.jp/selenium/web/bidi/logEntryAdded.html')
準備状態を指定して URL に移動
void testNavigateToAUrlWithReadinessState() {
BrowsingContext browsingContext = new BrowsingContext(driver, WindowType.TAB);
NavigationResult info = browsingContext.navigate("https://selenium.dokyumento.jp/selenium/web/bidi/logEntryAdded.html",
ReadinessState.COMPLETE);
Assertions.assertNotNull(browsingContext.getId());
Assertions.assertNotNull(info.getNavigationId());
Assertions.assertTrue(info.getUrl().contains("/bidi/logEntryAdded.html"));
}
const info = await browsingContext.navigate(
'https://selenium.dokyumento.jp/selenium/web/bidi/logEntryAdded.html',
'complete'
)
ブラウジングコンテキストツリーを取得
親ブラウジングコンテキストから派生するすべてのブラウジングコンテキストのツリー(親ブラウジングコンテキストを含む)を提供します。
void testGetTreeWithAChild() {
String referenceContextId = driver.getWindowHandle();
BrowsingContext parentWindow = new BrowsingContext(driver, referenceContextId);
parentWindow.navigate("https://selenium.dokyumento.jp/selenium/web/iframes.html", ReadinessState.COMPLETE);
List<BrowsingContextInfo> contextInfoList = parentWindow.getTree();
Assertions.assertEquals(1, contextInfoList.size());
BrowsingContextInfo info = contextInfoList.get(0);
Assertions.assertEquals(1, info.getChildren().size());
Assertions.assertEquals(referenceContextId, info.getId());
Assertions.assertTrue(info.getChildren().get(0).getUrl().contains("formPage.html"));
}
const browsingContextId = await driver.getWindowHandle()
const parentWindow = await BrowsingContext(driver, {
browsingContextId: browsingContextId,
})
await parentWindow.navigate('https://selenium.dokyumento.jp/selenium/web/iframes.html', 'complete')
const contextInfo = await parentWindow.getTree()
深さを指定してブラウジングコンテキストツリーを取得
親ブラウジングコンテキストから派生するすべてのブラウジングコンテキストのツリー(親ブラウジングコンテキストを含む)を、渡された深さの値まで提供します。
void testGetTreeWithDepth() {
String referenceContextId = driver.getWindowHandle();
BrowsingContext parentWindow = new BrowsingContext(driver, referenceContextId);
parentWindow.navigate("https://selenium.dokyumento.jp/selenium/web/iframes.html", ReadinessState.COMPLETE);
List<BrowsingContextInfo> contextInfoList = parentWindow.getTree(0);
Assertions.assertEquals(1, contextInfoList.size());
BrowsingContextInfo info = contextInfoList.get(0);
Assertions.assertNull(info.getChildren()); // since depth is 0
Assertions.assertEquals(referenceContextId, info.getId());
}
const browsingContextId = await driver.getWindowHandle()
const parentWindow = await BrowsingContext(driver, {
browsingContextId: browsingContextId,
})
await parentWindow.navigate('https://selenium.dokyumento.jp/selenium/web/iframes.html', 'complete')
const contextInfo = await parentWindow.getTree(0)
すべてのトップレベルブラウジングコンテキストを取得
void testGetAllTopLevelContexts() {
BrowsingContext window1 = new BrowsingContext(driver, driver.getWindowHandle());
BrowsingContext window2 = new BrowsingContext(driver, WindowType.WINDOW);
List<BrowsingContextInfo> contextInfoList = window1.getTopLevelContexts();
Assertions.assertEquals(2, contextInfoList.size());
}
const id = await driver.getWindowHandle()
const window1 = await BrowsingContext(driver, {
browsingContextId: id,
})
await BrowsingContext(driver, { type: 'window' })
const res = await window1.getTopLevelContexts()
タブ/ウィンドウを閉じる
void testCloseAWindow() {
BrowsingContext window1 = new BrowsingContext(driver, WindowType.WINDOW);
BrowsingContext window2 = new BrowsingContext(driver, WindowType.WINDOW);
window2.close();
Assertions.assertThrows(BiDiException.class, window2::getTree);
}
@Test
void testCloseATab() {
BrowsingContext tab1 = new BrowsingContext(driver, WindowType.TAB);
BrowsingContext tab2 = new BrowsingContext(driver, WindowType.TAB);
tab2.close();
Assertions.assertThrows(BiDiException.class, tab2::getTree);
}
const window1 = await BrowsingContext(driver, {type: 'window'})
const window2 = await BrowsingContext(driver, {type: 'window'})
await window2.close()
ブラウジングコンテキストをアクティブにする
BrowsingContext window1 = new BrowsingContext(driver, driver.getWindowHandle());
BrowsingContext window2 = new BrowsingContext(driver, WindowType.WINDOW);
window1.activate();
const window1 = await BrowsingContext(driver, {
browsingContextId: id,
})
await window1.activate()
ブラウジングコンテキストをリロード
BrowsingContext browsingContext = new BrowsingContext(driver, WindowType.TAB);
browsingContext.navigate("https://selenium.dokyumento.jp/selenium/web/bidi/logEntryAdded.html", ReadinessState.COMPLETE);
NavigationResult reloadInfo = browsingContext.reload(ReadinessState.INTERACTIVE);
ユーザープロンプトを処理
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
driver.get("https://selenium.dokyumento.jp/selenium/web/alerts.html");
driver.findElement(By.id("prompt-with-default")).click();
String userText = "Selenium automates browsers";
browsingContext.handleUserPrompt(true, userText);
スクリーンショットをキャプチャ
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
driver.get("https://selenium.dokyumento.jp/selenium/web/alerts.html");
String screenshot = browsingContext.captureScreenshot();
ビューポートのスクリーンショットをキャプチャ
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
driver.get("https://selenium.dokyumento.jp/selenium/web/coordinates_tests/simple_page.html");
WebElement element = driver.findElement(By.id("box"));
Rectangle elementRectangle = element.getRect();
String screenshot =
browsingContext.captureBoxScreenshot(
elementRectangle.getX(), elementRectangle.getY(), 5, 5);
const browsingContext = await BrowsingContext(driver, {
browsingContextId: id,
})
const response = await browsingContext.captureBoxScreenshot(5, 5, 10, 10)
要素のスクリーンショットをキャプチャ
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
driver.get("https://selenium.dokyumento.jp/selenium/web/formPage.html");
WebElement element = driver.findElement(By.id("checky"));
String screenshot = browsingContext.captureElementScreenshot(((RemoteWebElement) element).getId());
const response = await browsingContext.captureElementScreenshot(elementId)
ビューポートを設定
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
driver.get("https://selenium.dokyumento.jp/selenium/web/formPage.html");
browsingContext.setViewport(250, 300, 5);
ページを印刷
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
driver.get("https://selenium.dokyumento.jp/selenium/web/formPage.html");
PrintOptions printOptions = new PrintOptions();
String printPage = browsingContext.print(printOptions);
const result = await browsingContext.printPage({
orientation: 'landscape',
scale: 1,
background: true,
width: 30,
height: 30,
top: 1,
bottom: 1,
left: 1,
right: 1,
shrinkToFit: true,
pageRanges: ['1-2'],
})
戻る
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
browsingContext.navigate("https://selenium.dokyumento.jp/selenium/web/formPage.html", ReadinessState.COMPLETE);
wait.until(visibilityOfElementLocated(By.id("imageButton"))).submit();
wait.until(titleIs("We Arrive Here"));
browsingContext.back();
進む
void canNavigateForwardInTheBrowserHistory() {
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
browsingContext.navigate("https://selenium.dokyumento.jp/selenium/web/formPage.html", ReadinessState.COMPLETE);
wait.until(visibilityOfElementLocated(By.id("imageButton"))).submit();
wait.until(titleIs("We Arrive Here"));
browsingContext.back();
Assertions.assertTrue(driver.getPageSource().contains("We Leave From Here"));
browsingContext.forward();
履歴をたどる
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
browsingContext.navigate("https://selenium.dokyumento.jp/selenium/web/formPage.html", ReadinessState.COMPLETE);
wait.until(visibilityOfElementLocated(By.id("imageButton"))).submit();
wait.until(titleIs("We Arrive Here"));
browsingContext.traverseHistory(-1);
イベント
このセクションでは、ブラウジングコンテキストイベントに関連する API について説明します。
ブラウジングコンテキスト作成イベント
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
CompletableFuture<BrowsingContextInfo> future = new CompletableFuture<>();
inspector.onBrowsingContextCreated(future::complete);
String windowHandle = driver.switchTo().newWindow(WindowType.WINDOW).getWindowHandle();
BrowsingContextInfo browsingContextInfo = future.get(5, TimeUnit.SECONDS);
const browsingContextInspector = await BrowsingContextInspector(driver)
await browsingContextInspector.onBrowsingContextCreated((entry) => {
contextInfo = entry
})
await driver.switchTo().newWindow('window')
DOM コンテンツロードイベント
String windowHandle = driver.switchTo().newWindow(WindowType.TAB).getWindowHandle();
BrowsingContextInfo browsingContextInfo = future.get(5, TimeUnit.SECONDS);
Assertions.assertEquals(windowHandle, browsingContextInfo.getId());
}
}
@Test
void canListenToDomContentLoadedEvent()
const browsingContextInspector = await BrowsingContextInspector(driver)
let navigationInfo = null
await browsingContextInspector.onDomContentLoaded((entry) => {
navigationInfo = entry
})
const browsingContext = await BrowsingContext(driver, {
browsingContextId: await driver.getWindowHandle(),
})
await browsingContext.navigate('https://selenium.dokyumento.jp/selenium/web/bidi/logEntryAdded.html', 'complete')
ブラウジングコンテキストロードイベント
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
CompletableFuture<NavigationInfo> future = new CompletableFuture<>();
inspector.onBrowsingContextLoaded(future::complete);
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
context.navigate("https://selenium.dokyumento.jp/selenium/web/bidi/logEntryAdded.html", ReadinessState.COMPLETE);
NavigationInfo navigationInfo = future.get(5, TimeUnit.SECONDS);
const browsingContextInspector = await BrowsingContextInspector(driver)
await browsingContextInspector.onBrowsingContextLoaded((entry) => {
navigationInfo = entry
})
const browsingContext = await BrowsingContext(driver, {
browsingContextId: await driver.getWindowHandle(),
})
await browsingContext.navigate('https://selenium.dokyumento.jp/selenium/web/bidi/logEntryAdded.html', 'complete')
ナビゲーション開始イベント
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
CompletableFuture<NavigationInfo> future = new CompletableFuture<>();
inspector.onNavigationStarted(future::complete);
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
context.navigate("https://selenium.dokyumento.jp/selenium/web/bidi/logEntryAdded.html", ReadinessState.COMPLETE);
NavigationInfo navigationInfo = future.get(5, TimeUnit.SECONDS);
フラグメントナビゲーションイベント
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
CompletableFuture<NavigationInfo> future = new CompletableFuture<>();
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
context.navigate("https://selenium.dokyumento.jp/selenium/web/linked_image.html", ReadinessState.COMPLETE);
inspector.onFragmentNavigated(future::complete);
context.navigate("https://selenium.dokyumento.jp/selenium/web/linked_image.html#linkToAnchorOnThisPage", ReadinessState.COMPLETE);
NavigationInfo navigationInfo = future.get(5, TimeUnit.SECONDS);
const browsingContextInspector = await BrowsingContextInspector(driver)
const browsingContext = await BrowsingContext(driver, {
browsingContextId: await driver.getWindowHandle(),
})
await browsingContext.navigate('https://selenium.dokyumento.jp/selenium/web/linked_image.html', 'complete')
await browsingContextInspector.onFragmentNavigated((entry) => {
navigationInfo = entry
})
await browsingContext.navigate('https://selenium.dokyumento.jp/selenium/web/linked_image.html#linkToAnchorOnThisPage', 'complete')
ユーザープロンプトオープンイベント
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
CompletableFuture<NavigationInfo> future = new CompletableFuture<>();
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
context.navigate("https://selenium.dokyumento.jp/selenium/web/linked_image.html", ReadinessState.COMPLETE);
inspector.onFragmentNavigated(future::complete);
context.navigate("https://selenium.dokyumento.jp/selenium/web/linked_image.html#linkToAnchorOnThisPage", ReadinessState.COMPLETE);
NavigationInfo navigationInfo = future.get(5, TimeUnit.SECONDS);
ユーザープロンプトクローズイベント
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
CompletableFuture<UserPromptClosed> future = new CompletableFuture<>();
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
inspector.onUserPromptClosed(future::complete);
driver.get("https://selenium.dokyumento.jp/selenium/web/alerts.html");
driver.findElement(By.id("prompt")).click();
context.handleUserPrompt(true, "selenium");
UserPromptClosed userPromptClosed = future.get(5, TimeUnit.SECONDS);
Assertions.assertEquals(context.getId(), userPromptClosed.getBrowsingContextId());
ブラウジングコンテキスト破棄イベント
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
CompletableFuture<BrowsingContextInfo> future = new CompletableFuture<>();
inspector.onBrowsingContextDestroyed(future::complete);
String windowHandle = driver.switchTo().newWindow(WindowType.WINDOW).getWindowHandle();
driver.close();
BrowsingContextInfo browsingContextInfo = future.get(5, TimeUnit.SECONDS);
Assertions.assertEquals(windowHandle, browsingContextInfo.getId());
const browsingContextInspector = await BrowsingContextInspector(driver)
await browsingContextInspector.onBrowsingContextDestroyed((entry) => {
contextInfo = entry
})
await driver.switchTo().newWindow('window')
const windowHandle = await driver.getWindowHandle()
await driver.close()