2016-04-05 3 views
0

お客様からのフィードバックを収集する機能があります。このため、ユーザーがログアウトすると、ウィンドウがランダムにポップアップします。 私はこれを私のオートメーションコードで処理したいと思います。Selenium:ランダムにポップアップするウィンドウを処理します

現在、ログアウト時に、ウィンドウが表示され、そのウィンドウに切り替える予定があり、ポップアップウィンドウが表示されないときにそのコードが失敗しています。

これを処理する最も良い方法は何ですか。これは私がこれまで持っているものである

...

public static void waitForNumberOfWindowsToEqual(final int numberOfWindows) { 


    ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>() { 
     public Boolean apply(WebDriver driver) { 
      return (driver.getWindowHandles().size() == numberOfWindows); 
     } 
    }; 

    WebDriverWait wait = new WebDriverWait(driver, BrowserFactory.explicitWait); 


    wait.until(expectation); 
} 

答えて

1

私はtry/catchでポップアップウィンドウの不在を処理します。ここでは一例です:あなたが必要なウィンドウを取得していない場合は

try { 
    WebDriverWait winwait = new WebDriverWait(driver, 3); 
    String mainWindow = driver.getWindowHandle(); 

    // wait for 2 windows and get the handles 
    Set<String> handles = winwait.until((WebDriver drv) -> { 
     Set<String> items = drv.getWindowHandles(); 
     return items.size() == 2 ? items : null; 
    }); 

    // set the context on the last opened window 
    handles.remove(mainWindow); 
    driver.switchTo().window(handles.iterator().next()); 

    // close the window 
    driver.close(); 

    // set the context back to the main window 
    driver.switchTo().window(mainWindow); 

} catch (TimeoutException ex) { 
    System.out.println("No window present within 3 seconds"); 
} 
+0

コードからクリック(ログアウトの場合)アクションが欠落していますか? – Girish

+0

この例では、logoutアクションはtryの直前に置く必要があります。 –

1

可能な場合は、行うには理想的なことは、しかし場合、ポップアップウィンドウが表示されますか動作するようにソースを介して見ていることであろうこれは達成できません:

// Get the number of windows open before clicking the log out button.  
int numberOfWindowsBeforeLogOut = driver.getWindowHandles().size(); 

// Click the log out button. 
logOutButton.click(); 

// Check how many windows are open after clicking the log out button. 
int numberOfWindowsAfterLogOut = driver.getWindowHandles().size(); 

// Now compare the number of windows before and after clicking the log out 
// button in a condition statement. 
if (numberOfWindowsBeforeLogOut < numberOfWindowsAfterLogOut) { 
    // If there is a new window available, switch to it. 
    driver.switchTo().window(titleOrWindowHandle); 
} 
0

、コードがTimeoutExceptionをスローします。したがって、wait.until(expectation)をtryブロックの中に置き、例外をキャッチします。コード内で

try { 
    wait.until(expectation); 
} catch (TimeoutException ex) { 
    System.out.println("Nowindow This Time"); 
} 
関連する問題