[Selenium] 操作新弹出窗口之验证标题和内容
1)驗(yàn)證標(biāo)題
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{
//打開(kāi)父窗口,并記錄下父窗口的控點(diǎn)
String parentWindowId = driver.getWindowHandle();
//驗(yàn)證父窗口的完整標(biāo)題是Window open() Method
assertEquals("Window open() Method", driver.getTitle());
?
WebElement tryItButton = driver.findElement(By.xpath("//*[@id = \"main\"]/div[2]/a"));
tryItButton.click();
//獲取所有打開(kāi)窗口的控點(diǎn)列表
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());
//通過(guò)父窗口的控點(diǎn)再次切換回原父窗口,并再次驗(yàn)證其完整標(biāo)題為Window open() Method,確認(rèn)此處窗口切換成功
driver.switchTo().window(parentWindowId?);
assertEquals("Window open() Method", driver.getTitle());
}
?
@After
public void tearDown() throws Exception{
driver.quit();
}
}
2)驗(yàn)證內(nèi)容
...
for(String windowId : allWindowsId){
if(driver.switchTo().window(windowId).getPageSource().contains("open a new brower window")){
driver.switchTo().window(windowId);
break;
}
}
...
轉(zhuǎn)載于:https://www.cnblogs.com/feifeidxl/p/4551422.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的[Selenium] 操作新弹出窗口之验证标题和内容的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 开源点评:Protocol Buffer
- 下一篇: 文件操作和格式化