selenium的窗口跳动

来源:互联网 发布:看图软件安卓版 编辑:程序博客网 时间:2024/05/06 22:23
package com.gloryroad.Demo;


import java.util.Set;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;


public class IdentifyPopUpWindowByTitle {
String url="http://127.0.0.1:8020/HTMLDemo/HTMLPDir/Temp02/new_window.html";
public WebDriver driver;
@BeforeMethod
public void setUp(){
driver=new FirefoxDriver();
driver.get(url);

}

@AfterMethod
public void tearDown(){
driver.close();
}

@Test
public void identyfyWindow()
{
String currentWindow=driver.getWindowHandle();
WebElement target=driver.findElement(By.xpath("//a"));
target.click();
Set<String> allWindowHandles=driver.getWindowHandles();

if (!allWindowHandles.isEmpty()) {
for (String windHandle : allWindowHandles) {
try {
if (currentWindow!=windHandle) {
driver.switchTo().window(windHandle);
Thread.sleep(3000);
driver.findElement(By.id("query")).sendKeys("sougou首页");
driver.quit();
}

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
driver.switchTo().window(currentWindow);
Assert.assertEquals(driver.getTitle(),"Fruit");
}


}
0 0
原创粉丝点击