2013-12-23 1 views
-1

У меня есть следующие домашние задания:Невозможно окно, чтобы перейти в веб-драйвера Selenium

  • Написать программу с использованием Selenium WebDriver
  • Откройте файл «parent.htm» в Firefox.
  • Затем нажмите кнопку «Открыть модальный всплывающий».
  • Во всплывающем окне снова нажмите «Открыть всплывающее окно».
  • Закройте все окна, нажав на кнопки Закрыть в Pop-Ups

кодекса сделано до сих пор:

import java.util.List; 

import java.util.Set; 



import org.openqa.selenium.By; 

import org.openqa.selenium.JavascriptExecutor; 

import org.openqa.selenium.WebDriver; 

import org.openqa.selenium.WebElement; 

import org.openqa.selenium.chrome.ChromeDriver; 

import org.openqa.selenium.firefox.FirefoxDriver; 

import org.openqa.selenium.internal.seleniumemulation.JavascriptLibrary; 



public class popup_test { 

public static void main(String args[]) throws InterruptedException{ 

parent(); 

} 

public static void parent() throws InterruptedException{ 

System.setProperty("webdriver.chrome.driver","C:\\jars\\chromedriver.exe"); 

WebDriver driver = new ChromeDriver(); 

//WebDriver driver = new FirefoxDriver(); 

driver.get("C:\\Users\\jain\\Desktop\\popup\\parent.htm"); 

Thread.sleep(1000); 

String winHandleBefore1 = driver.getWindowHandle(); 

driver.findElement(By.id("OpenPopupButton")).click(); 

Set<String> winHandleColl_After = driver.getWindowHandles(); 

Object[] ObjHwndvals=winHandleColl_After.toArray(); 

Object Objhwnd=ObjHwndvals[1]; 

String strHwnd=(String) Objhwnd; 

driver=driver.switchTo().window(strHwnd); 

driver.findElement(By.id("OpenPopupButton")).click(); 



winHandleColl_After = driver.getWindowHandles(); 

ObjHwndvals=winHandleColl_After.toArray(); 

Object Objhwnd3=ObjHwndvals[2]; 

String strHwnd3=(String) Objhwnd; 

driver.switchTo().window(strHwnd3).findElement(By.id("MyModalPopup")).click(); 

driver.switchTo().window(strHwnd).findElement(By.id("MyModalPopup")).click(); 

} 

Файлы: Пожалуйста, сохраните это как .htm расширение родителя:

<html> 
<title>Parent Window</title> 

<script type="text/javascript" language="javascript"> 

    function openPopUpWithSize(theURL, title, heightInPX, widthInPX) 
    { 
    window.showModalDialog(theURL, title, 'dialogHeight=' + heightInPX + 'px;dialogWidth='+ widthInPX + 'px'); 
    } 
</script> 

<body> 

<div> 
<input type="button" id="OpenPopupButton" value="Open Modal Popup" onclick="openPopUpWithSize('child.htm', 'Simple Modal Popup Example', 400, 500);" /> 
</div> 
</body> 
</html> 

ребенок:

<html> 
<title>Child Window</title> 

<script type="text/javascript" language="javascript"> 

    function openPopUpWithSize(theURL, title, heightInPX, widthInPX) 
    { 
    window.showModalDialog(theURL, title, 'dialogHeight=' + heightInPX + 'px;dialogWidth='+ widthInPX + 'px'); 
    } 
    function closePopup() { 
    window.close(); 
    return false; 
    } 

</script> 

<body> 

<div> 
<input type="button" id="OpenPopupButton" value="Open Modal Popup" onclick="openPopUpWithSize('child.htm', 'Simple Modal Popup Example', 400, 500);" /> 

<input type="button" id="MyModalPopup" value="Close" onclick="closePopup();" /> 
</div> 

</body> 
</html> 
+0

привет, что вы хотите сделать, после закрытия всплывающего окна вы хотите переключить родительское окно. Я прав? –

+0

yes Naveen, это именно то, что я хочу – user3128718

+0

Than use [driver.switchTo(). Window (winHandleBefore1);] здесь строка «winHandleBefore1» содержит состояние родительского окна. –

ответ

0

Метод, который вы использовали showModalDialog(), специфичен для браузера IE, поэтому лучше попробовать браузер IE.

Смежные вопросы