doue9730
doue9730
采纳率100%
2018-05-07 12:12

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

已采纳

I'm using selenium php port and I'mm trying to log the results of the tests into a database for further mining.

Problem is, when I try to record for example the login button:

<?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";
}

I'm getting the below error:

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

To my understanding, while I'm using directory C:/mamp/htdocs it seems that composer is looking at C:/users/akal/desktop/qa/hello. Is this the issue?

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

1条回答

  • dqcwl02022 dqcwl02022 3年前

    Your script tries to find the PDO class in the Facebook\WebDriver namespace. You need to tell php, that this is a global available class, by adding a slash \ in front of every PDO usage.

    $conn = new \PDO("mysql:host=localhost;dbname=cms", "user_test", "test123");
    $conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
    
    点赞 评论 复制链接分享

为你推荐