【问题标题】:PHP - How to change machine's DateTime only during selenium testingPHP - 如何仅在硒测试期间更改机器的日期时间
【发布时间】:2015-08-28 04:51:32
【问题描述】:

所以,我要实现的是阻止用户登录站点如果当前机器日期与远程机器不同。

我创建了一个验证,用远程服务器的日期时间检查和验证机器。我已经尝试通过手动更改我的机器日期来进行手动测试,一切正常。

问题是,如果我想使用 selenium 测试来测试它怎么办?我想说每次我想运行测试时手动更改我的机器时间很烦人。

这是我的测试函数,其中包含更改默认时区的方法(但是,它不起作用

public function testLogin1()
{
  date_default_timezone_set('America/Anchorage');

  $testDate = getdate();

  var_dump(date_default_timezone_get());
  var_dump($testDate);

  $this->login('user', 'pass');

  sleep(3);
}

public function testLogin2()
{
  date_default_timezone_set('Asia/Bangkok');

  $testDate = getdate();

  var_dump(date_default_timezone_get());
  var_dump($testDate);

  $this->login('user', 'pass');

  sleep(3);
}

有没有可以设置全局 php DateTime / getdate() 的特定函数?

下面是我的 TestTime 类函数。

public static function isValid($currentDateTime) {

self::initRemoteServerDateTime();
self::initLatestUserActivityDateTime();

if (self::$remoteServerDateTime || self::$latestUserActivityDateTime) {
  $refDate = (self::$remoteServerDateTime) ? self::$remoteServerDateTime : self::$latestUserActivityDateTime;

  if ($currentDateTime->format('Y-m-d') == $refDate->format('Y-m-d')) {
      return true;
    }
  }
  return false;
}

这是我调用 TestTime 函数的操作类。

$currentDateTime = new DateTime();
if (!TestTime::isValid($currentDateTime)) {
  $errorMessage = 'Server\'s date and time is not set properly ['. $currentDateTime->format('j M Y H:i') .']. </br>  Please contact your system administrator.';
  throw new sfValidatorErrorSchema($this, array($this->getOption('username_field') => new sfValidatorError($this, $errorMessage)));
}

当用户单击登录按钮并提交表单时,我的操作类正在验证期间发生。

或者,有什么不同的方法可以做到这一点?

【问题讨论】:

  • 你能c/p你想测试的功能吗?
  • @PierreMarichez 我已经为你添加了代码。对于您的额外信息,我正在使用 symfony 框架。

标签: php selenium phpunit symfony1 functional-testing


【解决方案1】:

因为我不认为有办法做到这一点。我终于想出了一个 tmp 文件。这不是我想要的方法,但到目前为止效果很好。

这是我清理前的代码

protected function setUp()
{
  $my_file = '/tmp/lgnTimeValidation.tmp';
  unlink($my_file);
  $this->loadUpSession();
}

public function testLoginFromFarPast()
{
  $my_file = '/tmp/lgnTimeValidation.tmp';
  $handle = fopen($my_file, 'w+') or die('Cannot open file:  '.$my_file);
  $dateTxt = "2012-12-12 12:12";
  fwrite($handle, $dateTxt);
  fseek($handle, 0);
  $fileData = fread($handle, filesize($my_file));

  $this->login('user', 'pass', 'User');

  $theText = $this->session->execute(array(
    'script' => 'return jQuery(".form_error").html();',
    'args' => array(),
    ));
  $this->assertTrue((strpos($theText, "Server's date and time is not set properly") == true));

  fclose($handle);
}

public function testLoginFromFarFuture()
{
  $my_file = '/tmp/lgnTimeValidation.tmp';
  $handle = fopen($my_file, 'w+') or die('Cannot open file:  '.$my_file);
  $dateTxt = "2030-03-28 06:07";
  fwrite($handle, $dateTxt);
  fseek($handle, 0);
  $fileData = fread($handle, filesize($my_file));

  $this->login('user', 'pass', 'User');

  $theText = $this->session->execute(array(
    'script' => 'return jQuery(".form_error").html();',
    'args' => array(),
    ));
  $this->assertTrue((strpos($theText, "Server's date and time is not set properly") == true));

  fclose($handle);
}

public function testNormalLogin()
{
  $my_file = '/tmp/lgnTimeValidation.tmp';
  $handle = fopen($my_file, 'w+') or die('Cannot open file:  '.$my_file);
  $dt = new DateTime();
  $dateTxt = $dt->format("Y-m-d H:i:s");
  fwrite($handle, $dateTxt);
  fseek($handle, 0);
  $fileData = fread($handle, filesize($my_file));

  $this->login('user', 'pass', 'User');

  fclose($handle);
}

protected function tearDown()
{
  $my_file = '/tmp/lgnTimeValidation.tmp';
  unlink($my_file);
  $this->session->close();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 2019-12-26
    相关资源
    最近更新 更多