解决CAPTCHA时,是否可以暂停/恢复PHPUnit + Selenium测试? [英] Is there a way I can pause/resume a PHPUnit+Selenium test while I solve the CAPTCHA?

查看:94
本文介绍了解决CAPTCHA时,是否可以暂停/恢复PHPUnit + Selenium测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在为某些表单编写测试脚本,该脚本使用CAPTCHA尝试阻止机器人.显然,花费数小时不是没有几年试图开发一种使用PHPUnit + Selenium击败CAPTCHA的方法,但是我仍然希望构建测试以在页面提交后继续进行.

So i am writing a test script for some forms which uses a CAPTCHA to try and stop bots. obviously no point spending hours is not years trying to develop a way to defeat CAPTCHA using PHPUnit+Selenium but i still want to build the test to continue after the page has been submitted.

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

I figured that since i have multiple screens the best way i could do this is to run the test in one screen and use something like executeScript('alert("CAPTCHA time!")'); or some jQuery to let me know when to solve the CAPTCHA myself while the test runs

但是我看不到如何做到这一点,我想到的一个想法是让测试停止,但是我当前使用的代码

however i can't see how i can do this, one idea i thought was to have the test stop but the code i current use

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

will just start up a new firefox browser rather than continue with the one that was already opened.

我的另一个想法是进行测试暂停",直到我解决了验证码问题,然后在完成后重新启动,但是我也不知道该怎么做.除了阅读所有试图找到一个功能的代码外,我真的没有针对Webdriver的语法参考指南,

the other idea i had was to have the test "pause" until i solve the CAPTCHA and then restart when i am done, but i've no idea how to do this either. and i don't really have a good syntax reference guide for the Webdriver i am using aside from reading though all the code trying to find one function

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

So is there a way I can pause/resume a PHPUnit+Selenium test while I solve the CAPTCHA?

注意:我正在使用的Webdriver是 Facebook上的一个,并且我包括了这个__init__.php文件

NOTE: the webdriver i am using is the Facebook one and i include this __init__.php file

<?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上的文件相比,我正在使用的文件已过时(确实需要在某个时候进行升级)

though i suspect the files i am using a out of date in comparison to what is on github (really need to upgrade at some point)

注2:我建议我们关闭CAPTCHA进行调试,但是已经被拒绝的力量和该代码已经存在,因此它们必须有我们不能仅仅将其关闭的原因

NOTE2: i have suggested we turn off the CAPTCHA for debugging but the powers that be have said no and this code existed beforehand so they must have their reasons why we can't just turn it off

推荐答案

如果通过命令行运行PHPUnit和Selenium,则可以使用PHP.net的

If you are running PHPUnit and Selenium though the command line you can use the STDIN Example found on PHP.net's Input/output streams page

$line = trim(fgets(STDIN));

当代码达到极限时,所有内容都会暂停并等待,直到您按 Enter ,因此,如果您使用这样的代码

when the code reaches that lime everything will pause and wait until you press Enter, so if you use code like this

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

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

you can then get text entered into the command line that you can then use selenium to input that into the CAPTCHA form.

唯一的问题是,如果让Selenium在会话中闲置太长时间直到超时,从而在代码恢复时会导致错误(当前正在尝试解决此问题)

the only problem is that if you let Selenium idle too long the session till time out and thus cause errors when the code resumes (and currently looking to try and get around this)

这篇关于解决CAPTCHA时,是否可以暂停/恢复PHPUnit + Selenium测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆