首页 > 解决方案 > 硒和 php

问题描述

我一直在尝试测试此代码,取自此页面:https ://help.crossbrowsertesting.com/selenium-testing/getting-started/php/ 我想要做的就是创建一个浏览器堆栈帐户并使用一些简单的测试硒。

#Insert opening PHP tag
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverExpectedConditions;
use PHPUnit\Framework\TestCase;

class ToDoTest extends TestCase {
    protected $driver;
    static private $username = "YOUR_USERNAME";
    static private $authkey = "YOUR_AUTHKEY";
    static private $sessionId = NULL;

    public function setScore($score) {
        print "Setting the score to ". $score. "\n";
        $url = "https://crossbrowsertesting.com/api/v3/selenium/" . self::$sessionId;
        $options = array('action'=>'set_score', 'score'=>$score);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD, self::$username . ":" . self::$authkey);

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($options));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $info = curl_exec($ch);
        curl_close ($ch);
    }

    public function takeSnapshot(){
        $url = "https://crossbrowsertesting.com/api/v3/selenium/" . self::$sessionId . "/snapshots";
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
        curl_setopt($ch, CURLOPT_USERPWD, self::$username . ":" . self::$authkey);      
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        $info = curl_exec($ch);  
        curl_close($ch);  

    }

    public function setUp() {

        $capabilities = array("name"=> "Selenium Test Example PHP",
                  "build"=> "1.0",
                  "browser_api_name"=> "Chrome76",
                  "os_api_name"=> "Win10",
                  "record_video"=> "true",
                  "record_network"=> "false",);

        $host = "http://" . self::$username . ":" . self::$authkey . "@hub.crossbrowsertesting.com:80/wd/hub";
        $this->driver = RemoteWebDriver::create($host, $capabilities );
    }
    public function testToDos() {
        try {
            self::$sessionId = $this->driver->getSessionId();
            print "this is the session id: " . self::$sessionId;

            print "\nNavigating to URL\n";
            $this->driver->get("http://crossbrowsertesting.github.io/todo-app.html");
            $this->takeSnapshot();
            sleep(3);

            $this->assertEquals("Todo App - CrossBrowserTesting.com",$this->driver->getTitle());


            // if we've made it this far, assertions have passed and we'll set the score to pass.
            $this->setScore('pass');


        } catch (Exception $ex) {
            // if we caught an exception along the way, an assertion failed and we'll set the score to fail.
            $this->setScore('fail');

            print "Caught Exception: " . $ex->getMessage();

        }
    }
    public function tearDown() {   
        $this->driver->quit();
    }
}
?>

它显示的是这个错误:

Argument 1 passed to Facebook\WebDriver\Remote\DesiredCapabilities::__construct() must be of the type array, null given, called in 

我一直在尝试解决它,但没有找到我应该怎么做的方法。有什么建议吗?谢谢

标签: phpseleniumtesting

解决方案


推荐阅读