我想模拟现有应用程序的行为和测试。网络流量就像
login.jsp> [响应] index.jsp> [调用javascript打开新窗口welcome.jsp
Selenium无法检测到最后一个新窗口,即欢迎窗口。您能否让我知道需要做什么,以便它与新窗口一起使用。但这可以在IE9,Chrome和Firefox中使用。该应用程序是旧的,无法修改。
下面是用于打开欢迎页面的js代码段
var i = window.open('/en/account/Welcome.jsp', '20180814084959','menubar=no,toolbar=no,directories=no,status=yes,resizable=yes,scrollbars=yes,width=1015,height=690,left=0,top=0,screenX=0,screenY=0');
if(i)
{
var ie8 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;
if(!ie8)
{
window.opener = self;
window.close();
}
else
{
window.open('', '_parent', '');
window.close();
}
}
}
下面是Selenium的代码段
@Test
public void testNavisysLoginPolicySearch() throws Exception {
driver.get("http://yyy.xxx.com");
driver.findElement(By.id("userid")).clear();
driver.findElement(By.id("userid")).sendKeys("asdadsasd");
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys("sadasd");
driver.findElement(By.id("password")).sendKeys(Keys.ENTER);
waitForNewWindow(driver,20);
//new WebDriverWait(driver,5).until(ExpectedConditions.numberOfWindowsToBe(2));
for(String winHandle : driver.getWindowHandles()){
System.out.println(winHandle);
try {
driver.switchTo().window(winHandle);
}catch(NoSuchWindowException nwe) {
;
}
}
System.out.println(driver.getPageSource());
java.util.List<WebElement> links = driver.findElements(By.tagName("a"));
System.out.println(links.size());
for (int i = 1; i<=links.size(); i=i+1) {
System.out.println(links.get(i).getText());
}
driver.findElement(By.cssSelector("a[href*='/en/search/Search.jsp']" )).click();
driver.findElement(By.name("statusField")).click();
new Select(driver.findElement(By.name("statusField"))).selectByVisibleText("Active");
driver.findElement(By.xpath("//option[@value='1-Active']")).click();
driver.findElement(By.name("search")).click();
driver.findElement(By.linkText("A48A2500050")).click();
}
public boolean waitForNewWindow(WebDriver driver, int timeout){
boolean flag = false;
int counter = 0;
while(!flag){
try {
Set<String> winId = driver.getWindowHandles();
if(winId.size() > 1){
flag = true;
return flag;
}
Thread.sleep(1000);
counter++;
if(counter > timeout){
return flag;
}
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
return flag;
}
使用以下代码段初始化的IE驱动程序类
driver(){
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
InternetExplorerOptions ieOpts = new InternetExplorerOptions();
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
caps.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
caps.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
// caps.setCapability("ignoreProtectedModeSettings",1);
caps.setCapability("IntroduceInstabilityByIgnoringProtectedModeSettings",true);
// caps.setCapability("nativeEvents",true);
caps.setCapability("browserFocus",true);
caps.setCapability("enablePersistentHover", true);
ieOpts.requireWindowFocus();
ieOpts.merge(caps);
caps.setCapability("requireWindowFocus", true);
if ( environment.equalsIgnoreCase("local") ) {
System.setProperty("webdriver.ie.driver", Configuration.getProperty("ie.driver.windows.path"));
webDriver.set(new InternetExplorerDriver(ieOpts.merge(caps)));
}
}