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

时间:2018-05-07 12:12:52

标签: php selenium composer-php facebook-php-webdriver

我正在使用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时,似乎作曲家正在查看C:/users/akal/desktop/qa/hello。这是问题吗?

1 个答案:

答案 0 :(得分:3)

您的脚本尝试在PDO命名空间中查找Facebook\WebDriver类。你需要告诉php,这是一个全局可用的类,通过在每个\用法前添加斜杠PDO

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