[Selenium] 操作新弹出窗口之验证标题和内容
1)驗證標題
package com.learningselenium.normalwebdriver;
?
import static org.junit.Assert.*;
import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
?
public class test MultipleWindowsTile{
WebDriver driver = new FirefoxDriver();
?
@Before
public void setUp() throws Exception{
driver.get("http://www.w3schools.com/jsref/met_win_open.asp");
}
?
@Test
public void testMultipleWindowsTitle() throws Exception{
//打開父窗口,并記錄下父窗口的控點
String parentWindowId = driver.getWindowHandle();
//驗證父窗口的完整標題是Window open() Method
assertEquals("Window open() Method", driver.getTitle());
?
WebElement tryItButton = driver.findElement(By.xpath("//*[@id = \"main\"]/div[2]/a"));
tryItButton.click();
//獲取所有打開窗口的控點列表
Set<String> allWindowsId = driver.getWindowHandles();
?
for(String windowId : allWindowsId){
if(driver.switchTo().window(windowId).getTitle().contains("Tryit")){
driver.switchTo().window(windowId);
break;
}
}
?
assertEquals("Tryit Editor v1.8", driver.getTitle());
//通過父窗口的控點再次切換回原父窗口,并再次驗證其完整標題為Window open() Method,確認此處窗口切換成功
driver.switchTo().window(parentWindowId?);
assertEquals("Window open() Method", driver.getTitle());
}
?
@After
public void tearDown() throws Exception{
driver.quit();
}
}
2)驗證內容
...
for(String windowId : allWindowsId){
if(driver.switchTo().window(windowId).getPageSource().contains("open a new brower window")){
driver.switchTo().window(windowId);
break;
}
}
...
轉載于:https://www.cnblogs.com/feifeidxl/p/4551422.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的[Selenium] 操作新弹出窗口之验证标题和内容的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 开源点评:Protocol Buffer
- 下一篇: 文件操作和格式化