2014-02-25 27 views
3

我有一個場景來驗證打印屬性對話框(Windows組件)在點擊打印鏈接後正確打開。瞭解Java中的Robot實用程序類,它可以模擬像Escape/Enter等鍵盤事件在該窗口上操作。Selenium WebDriver:驗證頁面上顯示的打印窗口對話框

有什麼辦法,我們可以驗證開闢了新的對話框是一個打印對話 - 這是驗證對話框的標題即打印或從Windows對話框或別的東西,這將確認對話框是一個檢索文本打印對話框。

Print Window dialog

+0

Propably重複:http://stackoverflow.com/questions/11537103/how-to-handle-print-dialog-in-selenium – Spindizzy

+1

@Spindizzy這個問題解決處理打印窗口對話框的其他方面。在這裏我想確認新窗口打開一個打印窗口(可能是通過驗證窗口標題或打印對話框中的某些元素等)。 http://stackoverflow.com/questions/11537103/how-to-handle-print-dialog-in-selenium解決如何處理該對話框,並不符合我的目的! –

回答

1

打印對話框來自操作系統,它的硒不能處理(還)。因此你將無法檢查是否存在。我能想到的唯一方法是使用java.awt.Robot,發送VK_ESCAPE並聲明測試繼續。

擔任首發,你可以試試這個:

 Runnable r = new Runnable() { 

     @Override 
     public void run() { 

      try { 
       Robot r = new Robot(); 
       r.delay(1000); 
       r.keyPress(KeyEvent.VK_ESCAPE); 
       r.keyRelease(KeyEvent.VK_ESCAPE); 
      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } 

     } 
    }; 

    Actions actions = new Actions(getDriver()); 
    actions.sendKeys(Keys.CONTROL).sendKeys("p"); 

    Thread t = new Thread(r); 
    t.start(); 

    actions.perform(); 

    //some stupid asserts that we reached here 
1

如果您在Windows(其中我會假設你),你可以使用與Visual Studio走來的inspect.exe工具操作。它將允許您與對話框進行交互,甚至可以準確地發送您想要的任何信息,包括從下拉列表中選擇要素或進行任何其他需要的交互。如果您希望使用硒保存文件,這甚至可以工作,但要回答您的問題,您甚至可以使用它來檢測該窗口是否確實存在。你想如何從那裏繼續是你的呼叫。

//using System.Windows.Automation; 
//using System.Windows.Forms; 

AutomationElement desktop = AutomationElement.RootElement; 
AutomationElement Firefox = desktop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "MozillaWindowClass")); 
AutomationElement PrinterComboBox = PrintForm1.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "1139")); 
SelectionPattern selectPrinterComboBox = (SelectionPattern)PrinterComboBox.GetCurrentPattern(SelectionPattern.Pattern); 
AutomationElement ItemInDropdown = PrinterComboBox.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "SelectPrintMethod")); 
SelectionItemPattern ItemInDropdownSelectItem = (SelectionItemPattern)ItemInDropdown.GetCurrentPattern(SelectionItemPattern.Pattern); 
ItemInDropdownSelectItem.Select(); 
AutomationElement OKButton = PrintForm1.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "1")); 
InvokePattern ClickOK = (InvokePattern)OKButton.GetCurrentPattern(InvokePattern.Pattern); 
ClickOK.Invoke(); 
相關問題