Selenium ダイアログ
alert、confirm、prompt
html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>テストページ</title>
<script type="text/javascript">
function msg() {
alert("メッセージダイアログ");
}
function confirMsg() {
let res = confirm("確認ダイアログ");
document.getElementById("id21").value = res;
}
function promptMsg() {
let str = prompt("入力ダイアログ");
document.getElementById("id31").value = str;
}
</script>
</head>
<body>
<button id="id1" onclick="msg()">メッセージ</button>
<button id="id2" onclick="confirMsg()">確認</button>
<input id="id21" type="text" />
<button id="id3" onclick="promptMsg()">入力</button>
<input id="id31" type="text" />
</body>
</html>
java
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Test {
public static void main(String[] arg) throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.get("http://localhost/test/test.html");
driver.findElement(By.id("id1")).click();
Thread.sleep(1000);
Alert alert = driver.switchTo().alert();
System.out.println(alert.getText());
alert.accept();
driver.findElement(By.id("id2")).click();
Thread.sleep(1000);
Alert confirm = driver.switchTo().alert();
System.out.println(confirm.getText());
confirm.dismiss();
driver.findElement(By.id("id3")).click();
Thread.sleep(1000);
Alert prompt = driver.switchTo().alert();
System.out.println(prompt.getText());
prompt.sendKeys("Hello");
prompt.accept();
Thread.sleep(1000 * 10);
driver.quit();
}
}
alert、confirm、promptはdriver.switchTo().alert()でダイアログにアクセスし
alert.accept()でOKとし、confirm.dismiss()でキャンセルとします。
promptで文字を入力する場合は、sendKeysで入力します。
window.open
html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JavaScriptのテストページ</title>
</head>
<body>
<button id="btn1" onclick="show()">子ウィンドウを表示</button>
</body>
<script type="text/javascript">
function show() {
var win = window.open("./test2.html");
if (!win) {
alert("ウィンドウ表示に失敗しました")
}
}
</script>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>子ウィンドウ</title>
</head>
<body>
子ウィンドウ
<button id="btn2" onclick="closeDialog()">閉じる</button>
</body>
<script type="text/javascript">
function closeDialog() {
this.close();
}
</script>
</html>
java
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Test {
public static void main(String[] arg) throws InterruptedException {
WebDriver driver = new ChromeDriver();
Test test = new Test(driver);
test.add("default");
driver.get("http://localhost/test/test.html");
driver.findElement(By.id("btn1")).click();
test.add("child");
Thread.sleep(1000);
test.show("default");
Thread.sleep(1000);
test.show("child");
Thread.sleep(1000);
driver.findElement(By.id("btn2")).click();
Thread.sleep(1000);
driver.quit();
}
Map<String, String> mapHandle = new HashMap<>();
WebDriver driver;
public Test(WebDriver driver) {
this.driver = driver;
}
void add(String name) throws InterruptedException {
for (int i = 0; i < 10; i++) {
Set<String> handles = driver.getWindowHandles();
if (mapHandle.size() != handles.size()) {
for (String handle : handles) {
if (!mapHandle.containsValue(handle)) {
mapHandle.put(name, handle);
return;
}
}
}
Thread.sleep(1000);
}
}
void show(String name) {
driver.switchTo().window(mapHandle.get(name));
}
}
ダイアログへのアクセスはdriver.getWindowHandles()で全ウィンドウのハンドルを取得し、
driver.switchTo().window()で取得したハンドルを渡すを処理対象のウィンドウを切り替えることができます。
ページのトップへ戻る