duanlei1957 2017-03-06 11:27
浏览 74
已采纳

Codeception - 关闭弹出窗口打破selenium测试

I'm testing a simple flow using Codeception with Selenium/FacebookWebdriver where a popup window gets closed at the end - causing entire test to break.

The code is complete (the test will run) and will reproduce the error. I'm really desperate here, any suggestions will be very much appreciated.

These are the errors I get:

Codeception error message: [Facebook\WebDriver\Exception\NoSuchWindowException] Window not found. The browser window may have been closed.

Selenium server output message: WARN - Exception: Window not found. The browser window may have been closed.

This code reproduces the problem. It consits of 3 .html files (main.html, intermediate.html, popup.html), test file (PopupCest.php) and configuration file (selenium.suite.yml).

PopupCest.php

<?php

class PopupCest
{

    public function popup(SeleniumTester $I)
    {
        $I->expectTo('Start on main page and click a link that opens a popup');
        $I->amOnPage('/main.html');
        $I->click('#main-link');

        $I->expectTo('Interact with the popup window and click the final confirmation button');
        $I->switchToNextTab();
        $I->click('#final-confirmation-button');

        $I->expectTo('Go back to intermediate window and let it do its magic');
        $I->switchToPreviousTab();
        $I->see('contents of this intermediate page');
        $I->seeInCurrentUrl('intermediate.html');
    }

}

selenium.suite.yml

class_name: SeleniumTester
modules:
  enabled:
    - WebDriver:
        url: http://localhost
        browser: firefox
    - \Helper\Selenium

main.html

<html>
<head>
    <meta charset="utf-8">
    <title>Main page - under my control</title>
    <meta name="description" content="Main page - under my control">
</head>

<body>
    <h1>Main</h1>
    <p>
        After clicking this link, "intermediate page" url is received from a backend operation in reality.
    </p>
    <br>
    <a href="intermediate.html" id="main-link">Click here to open the popup</a> <br>
</body>
</html>

intermediate.html

<html>
<head>
    <meta charset="utf-8">
    <title>External - intermediate page</title>
    <meta name="description" content="Intermediate page outside of my control">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>

<body>
    <h1>Intermediate</h1>
    <p>
        In reality, contents of this intermediate page is fully controlled by other party - a payment processor. <br>
        It only load a javascript that opens up a popup window where user enters his payment details.
    </p>


    <script type="text/javascript">
        $(function() {
            var settings = "height=400,width=500,status=yes,dependent=no,resizable=yes";

            popup = window.open('popup.html', 'dont_know_the_name_here', settings);
            if (popup != null) {
                popup.focus();
            }
        });
    </script>
</body>
</html>

popup.html

<html>
<head>
    <meta charset="utf-8">
    <title>External - popup</title>
    <meta name="description" content="Popup page outside of my control">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>

<body>
    <h1>Popup</h1>
    <p>
        Contents of this page is fully controlled by other party - a payment processor. <br>
        This is where user enters his payment details.
    </p>
    <p>
        e.g. <i>"After you have finished your data input, click here to:"</i> <br>
        <a href="" id="final-confirmation-button">Confirm your payment</a>
    </p>


    <script type="text/javascript">
        $(function() {
            $('#final-confirmation-button').on('click', function(e) {
                e.preventDefault();
                window.close(); // <-- this breaks it all 
            });
        });
    </script>
</body>
</html>

Thank you for your help, Tomas

  • 写回答

1条回答 默认 最新

  • douyi6290 2017-03-19 20:00
    关注

    Test code seems to be correct, I suspect that problem is related to outdated versions or possibly Firefox driver.

    I tested your code with chrome+chromedriver and it seems to work without problem. Here's a test repo with instructions how to use chromedriver + required config changes: https://github.com/henrikauppinen/codeception-popup-chromedriver

    Generally I'd advise to use Chrome for this type of testing, unless you are testing Firefox specifically or if it's a requirement.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog