【问题标题】:Is there a way I can pause/resume a PHPUnit+Selenium test while I solve the CAPTCHA?有没有办法在我解决验证码时暂停/恢复 PHPUnit+Selenium 测试?
【发布时间】:2016-02-10 19:14:16
【问题描述】:

所以我正在为某些使用 CAPTCHA 来尝试阻止机器人的表单编写测试脚本。显然,花费数小时而不是数年试图开发一种使用 PHPUnit+Selenium 击败 CAPTCHA 的方法是没有意义的,但我仍然想构建测试以在页面提交后继续。

我认为,由于我有多个屏幕,因此我可以做到这一点的最佳方法是在一个屏幕中运行测试并使用 executeScript('alert("CAPTCHA time!")'); 或一些 jQuery 之类的东西来让我知道何时在测试运行时自己解决验证码

但是我看不出我怎么能做到这一点,我认为的一个想法是让测试停止但我当前使用的代码

$host = 'http://localhost:4444/wd/hub'; // this is the default
$capabilities = array(WebDriverCapabilityType::BROWSER_NAME => 'firefox');
$this->driver = RemoteWebDriver::create($host, $capabilities, 5000);

// adding cookie
$this->driver->manage()->deleteAllCookies();
$this->driver->manage()->addCookie(array(
  'name' => 'cookie_name',
  'value' => 'cookie_value',
));
$this->cookies = $this->driver->manage()->getCookies();

只会启动一个新的 Firefox 浏览器,而不是继续使用已经打开的浏览器。

我的另一个想法是让测试“暂停”,直到我解决了验证码,然后在我完成后重新启动,但我也不知道该怎么做。除了阅读所有试图找到一个函数的代码之外,我真的没有一个很好的 Webdriver 语法参考指南

那么有没有办法在我解决验证码时暂停/恢复 PHPUnit+Selenium 测试?

注意:我使用的网络驱动程序是 the Facebook one,我包含了这个 __init__.php 文件

<?php
// Copyright 2004-present Facebook. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// interface
require_once('WebDriverSearchContext.php');
require_once('WebDriver.php');
require_once('WebDriverElement.php');
require_once('WebDriverCommandExecutor.php');
require_once('WebDriverAction.php');
require_once('WebDriverEventListener.php');

// abstract class
require_once('interactions/internal/WebDriverKeysRelatedAction.php');
require_once('interactions/internal/WebDriverSingleKeyAction.php');

// class
require_once('WebDriverAlert.php');
require_once('WebDriverBy.php');
require_once('WebDriverDimension.php');
require_once('WebDriverExceptions.php');
require_once('WebDriverExpectedCondition.php');
require_once('WebDriverHasInputDevices.php');
require_once('WebDriverKeys.php');
require_once('WebDriverNavigation.php');
require_once('WebDriverMouse.php');
require_once('WebDriverKeyboard.php');
require_once('WebDriverOptions.php');
require_once('WebDriverPoint.php');
require_once('WebDriverSelect.php');
require_once('WebDriverTargetLocator.php');
require_once('WebDriverTimeouts.php');
require_once('WebDriverWait.php');
require_once('WebDriverWindow.php');
require_once('interactions/WebDriverActions.php');
require_once('interactions/internal/WebDriverMouseAction.php');
require_once('interactions/WebDriverCompositeAction.php');
require_once('interactions/internal/WebDriverButtonReleaseAction.php');
require_once('interactions/internal/WebDriverClickAction.php');
require_once('interactions/internal/WebDriverClickAndHoldAction.php');
require_once('interactions/internal/WebDriverContextClickAction.php');
require_once('interactions/internal/WebDriverCoordinates.php');
require_once('interactions/internal/WebDriverDoubleClickAction.php');
require_once('interactions/internal/WebDriverMouseMoveAction.php');
require_once('interactions/internal/WebDriverMoveToOffsetAction.php');
require_once('internal/WebDriverLocatable.php');
require_once('remote/RemoteMouse.php');
require_once('remote/RemoteKeyboard.php');
require_once('remote/RemoteWebDriver.php');
require_once('remote/RemoteWebElement.php');
require_once('remote/WebDriverBrowserType.php');
require_once('remote/WebDriverCapabilityType.php');
require_once('remote/HttpCommandExecutor.php');
require_once('interactions/internal/WebDriverSendKeysAction.php');
require_once('interactions/internal/WebDriverKeyDownAction.php');
require_once('interactions/internal/WebDriverKeyUpAction.php');

require_once('support/events/EventFiringWebDriver.php');
require_once('support/events/EventFiringWebDriverNavigation.php');
require_once('WebDriverDispatcher.php');
require_once('support/events/EventFiringWebElement.php');

// touch
require_once('interactions/WebDriverTouchScreen.php');
require_once('remote/RemoteTouchScreen.php');
require_once('interactions/WebDriverTouchActions.php');
require_once('interactions/touch/WebDriverTouchAction.php');
require_once('interactions/touch/WebDriverDoubleTapAction.php');
require_once('interactions/touch/WebDriverDownAction.php');
require_once('interactions/touch/WebDriverFlickAction.php');
require_once('interactions/touch/WebDriverFlickFromElementAction.php');
require_once('interactions/touch/WebDriverLongPressAction.php');
require_once('interactions/touch/WebDriverMoveAction.php');
require_once('interactions/touch/WebDriverScrollAction.php');
require_once('interactions/touch/WebDriverScrollFromElementAction.php');
require_once('interactions/touch/WebDriverTapAction.php');
require_once('interactions/touch/WebDriverUpAction.php');

虽然我怀疑我使用的文件与 github 上的文件相比已经过时(确实需要在某个时候升级)

注意 2:我建议我们关闭验证码进行调试,但权力已拒绝,并且此代码事先存在,因此他们必须有他们的理由,我们不能将其关闭

【问题讨论】:

  • 我会尝试通过在“开发”模式下运行应用程序/框架来避免验证码(不需要身份验证或验证码),或将本地 IP 列入白名单,或配置为在测试中使用 test API keys环境
  • @NoChecksum 除非这种“开发”模式是 CAPTCHA 内置的,那么它或多或少是我在 NOTE2 中已经解释过的,但我会询问您建议的其他选项。如果我被 CAPTCHA 卡住了,我仍然会寻求答案,但也适用于任何其他罕见的情况,即我无法轻松关闭或绕过反机器人措施
  • 将解决方案的发现留给阻止你做显而易见的事情的权力总是一个好主意:在测试运行期间禁用验证码,或者预定义它并输入已知文本(即在开发模式下)它始终是“开发模式”作为文本,测试可以输入它作为答案)。
  • 你不想自动解决验证码?
  • @L.Bar 这是不可能的(据我所知)。它是其中之一,您可以单击一个框来证明您的人类,并且在随机时间(至少我认为它是随机的,因为我并不总是得到它)会要求您选择匹配的图像(即选择所有蛋糕,选择所有棕榈树)。如果可以的话,我会的,但如果我可以自动解决生产环境 CAPTCHA,那么我有点打败它存在的原因

标签: php selenium phpunit


【解决方案1】:

如果你通过命令行运行 PHPUnit 和 Selenium,你可以使用 PHP.net 的 Input/output streams page 上的 STDIN 示例

$line = trim(fgets(STDIN));

当代码达到这个时间值时,一切都会暂停并等到你按下 Enter,所以如果你使用这样的代码

echo "\nCommand: ";
$line = trim(fgets(STDIN));
echo "\nInput = '".$line ."'";

然后您可以将文本输入到命令行中,然后您可以使用 selenium 将其输入到 CAPTCHA 表单中。

唯一的问题是,如果您让 Selenium 闲置太久,会话就会超时,从而在代码恢复时导致错误(目前正在尝试解决这个问题)

【讨论】:

    猜你喜欢
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多