doudu8291
doudu8291
2015-11-10 00:47

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

已采纳

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.

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();

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

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

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

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');

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)

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • drtj40036 drtj40036 4年前

    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));
    

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

    echo "
    Command: ";
    $line = trim(fgets(STDIN));
    echo "
    Input = '".$line ."'";
    

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

    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)

    点赞 评论 复制链接分享