有没有办法在解决CAPTCHA时暂停/恢复PHPUnit + Selenium测试?

时间:2015-11-10 00:47:38

标签: php selenium phpunit

所以我正在为一些使用CAPTCHA尝试停止机器人的表单编写测试脚本。显然没有必要花费数小时的时间来尝试开发一种使用PHPUnit + Selenium来击败CAPTCHA的方法,但我仍然希望在页面提交后继续构建测试。

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

然而,我无法看到我如何做到这一点,我认为一个想法是让测试停止,但我目前使用的代码

$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浏览器,而不是继续使用已经打开的浏览器。

我的另一个想法是进行测试"暂停"直到我解决CAPTCHA,然后在我完成后重启,但我也不知道如何做到这一点。并且我没有真正有一个很好的语法参考指南,我正在使用的Webdriver,除了阅读所有代码试图找到一个函数

有没有办法在解析CAPTCHA时暂停/恢复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:我建议我们关闭CAPTCHA进行调试,但是权力说不,而且这些代码事先存在,所以他们必须知道为什么我们不能将其关闭

1 个答案:

答案 0 :(得分:0)

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

$line = trim(fgets(STDIN));

当代码达到那个时,所有内容都会暂停并等到你按 Enter ,所以如果你使用这样的代码

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

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

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