php 驱动程序报告事件(PDO 问题)

php driver report event (PDO issue)

我正在使用 selenium php 端口,我正在尝试将测试结果记录到数据库中以供进一步挖掘。

问题是,当我尝试记录例如登录按钮时:

<?php
namespace Facebook\WebDriver;

include("C:/MAMP/htdocs/vendor/autoload.php");

use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
$host = 'http://127.0.0.1:9515/';
$driver = RemoteWebDriver::create($host, DesiredCapabilities::chrome(), 50000);

#Novibet.gr login test
$driver->get('http://www.example.com/');
$Login_element = $driver->findElement(WebDriverBy::className('login'))->click();
$driver->findElement(WebDriverBy::name('Username'))->click();
$username_element= $driver->getKeyboard()->sendKeys('username');
$driver->findElement(WebDriverBy::name('Password'))->click();
$username_element= $driver->getKeyboard()->sendKeys('password');

if ($element = $driver->findElement(WebDriverBy::cssSelector('#user .login #login input[type=submit]'))->click()){
correct_function();}
function correct_function(){
$timestamp = date("Y-m-d H:i:s");
$conn = new PDO("mysql:host=localhost;dbname=cms", "user_test", "test123"); //LINE 28
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO tests (id, Test_name, Test_description, Severity, Status , Timestamp_of_test)
VALUES ('Login_test', 'Click to login button', 'critical','$timestamp')";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
}

我收到以下错误:

PHP Fatal error:  Uncaught Error: Class 'Facebook\WebDriver\PDO' not found 
in C:\Users\akal\Desktop\QA\hello.php:28
Stack trace:
#0 C:\Users\akal\Desktop\QA\hello.php(22): F 
Facebook\WebDriver\correct_function()
#1 {main}
thrown in C:\Users\akal\Desktop\QA\hello.php
Fatal error: Uncaught Error: Class 'Facebook\WebDriver\PDO' not found in 
C:\Users\akal\Desktop\QA\hello.php:28
Stack trace:
#0 C:\Users\akal\Desktop\QA\hello.php(22): 
Facebook\WebDriver\correct_function()
#1 {main}
thrown in C:\Users\akal\Desktop\QA\hello.php on line 28
on line 28

据我了解,当我使用目录 C:/mamp/htdocs 时,composer 似乎正在查看 C:/users/akal/desktop/qa/hello。是这个问题吗?

您的脚本试图在 Facebook\WebDriver 命名空间中找到 PDO class。您需要通过在每个 PDO 用法前添加斜线 \ 来告诉 php,这是全局可用的 class。

$conn = new \PDO("mysql:host=localhost;dbname=cms", "user_test", "test123");
$conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);