单击关闭弹出窗口后 JUnit 停止执行 window

JUnit stops executing after click closes the popup window

我正在帮助我的一个朋友完成他在 jUnit 中的第一个测试。 我们最近遇到了一个问题,我不知道是什么原因造成的。

我们正在执行在页面上弹出 Facebook 登录 window 的测试,然后填写登录输入,它将关闭 window 并刷新浏览器。登录后,我们想继续测试登录用户的功能。

问题是...在提交并关闭 window 的元素上执行 click() 函数后,测试没有进行,就像它重新启动工作一样,但过了一会儿停止了.

为了更好地说明问题...这是代码示例:

@Before
public void setUp() throws Exception 
{
    driver = new FirefoxDriver();
    baseUrl = "https://www.example.com/";
}

@Test
public void OrderWithLoginFbCash() throws Exception
{
    driver.get(baseUrl);

    // remembers the parent Windowhandle
    String parentHandle  = driver.getWindowHandle(); 

    // Opens Facebook login window
    WebElement FbLogin = (new WebDriverWait(driver , 20)).until(
        ExpectedConditions.presenceOfElementLocated(By.id("fblogin"))
    );
    FbLogin.click();

    for (String winHandle : driver.getWindowHandles()){
        driver.switchTo().window(winHandle);
    }

    // ... filling up the inputs ...

    WebElement FbLoginFacebook = (new WebDriverWait(driver, 20)).until(
        ExpectedConditions.presenceOfElementLocated(By.id("u_0_0"))
    );
    // everything's still fine here
    FbLoginFacebook.click(); // logs in, closes the window and reloads the page

    // !!! Test won't continue here for some reason. !!!

    System.out.println("Did the logging in stuff"); // Won't be printed

}

我自己不是测试人员,所以我很难说为什么它会停在那里。我认为这可能是因为它设置的 window 在元素的 click() 上关闭了,但后来我想不出任何解决该问题的方法。任何帮助,将不胜感激。谢谢。

因此,在尝试更多之后,我们成功地 运行 进行了测试。导致问题的原因显然是 FbLoginFacebook.click(),它实际上关闭了当前活动的 window,因此测试无法继续。

万一有人对我们如何解决问题感兴趣...FbLoginFacebook 元素触发了表单上的提交事件,该事件成功关闭了打开的 window(使用 Facebook 登录)。我不知道它为什么有效,但在我们手动提交表单后(通过选择表单元素并在其上执行 submit() 方法)它完美地工作。然后我们可以 return 通过 driver.switchTo().window(previouslySavedParentWindowHandle) 回到父 window 并且一切都像魅力一样。

// before changing of window we saved the 
// String parentHandle = driver.getWindowHandle();

WebElement FbLoginFacebook = (new WebDriverWait(driver, 20)).until(
    ExpectedConditions.presenceOfElementLocated(By.id("login_form"))
);
FbLoginFacebook.submit();

driver.switchTo().window(parentHandle);

无论如何,如果有人能解释为什么会发生这种情况以及我们将来如何避免这种情况(或者我们如何处理上述问题中的点击),我将不胜感激。然后我可以将其添加到此答案中。谢谢。