2

我正在使用 php-webdriver-bindings 在 PHP 下设置 Selenium 测试。有人在项目主页中遇到以下示例吗?

执行示例测试时,服务器会抛出 ExecutionException。从 Java 执行相同的步骤没有问题。(无论如何,这些步骤可能并不那么重要,因为已经在确定会话能力时发生了异常)

PHP代码:

<?php
require_once "phpwebdriver/WebDriver.php";
/*require("phpwebdriver/LocatorStrategy.php");*/ /*already included*/

$webdriver = new WebDriver("localhost", "4444");
$webdriver->connect("firefox");                            
$webdriver->get("http://google.com");
$element = $webdriver->findElementBy(LocatorStrategy::name, "q");
$element->sendKeys(array("selenium google code" ) );
$element->submit();

$webdriver->close();
?>

Java 代码:

package asdfjkl.selenium.test;

import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class Sel2Test2  {
    public static void main(String[] args) {
        URL url = null;

        try {
            url = new URL("http://localhost:4444/wd/hub");
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

        WebDriver driver = new RemoteWebDriver(
                url, DesiredCapabilities.firefox());

        driver.get("http://www.google.com");
        WebElement element = driver.findElement(By.name("q"));

        element.sendKeys("selenium google code");
        element.submit();

        driver.quit();
    }
}

执行 PHP 测试时的服务器输出(抛出异常):

May 29, 2012 11:57:55 AM org.openqa.grid.selenium.GridLauncher main
INFO: Launching a standalone server
11:57:56.537 INFO - Java: Sun Microsystems Inc. 20.0-b11
11:57:56.539 INFO - OS: Linux 2.6.18-308.1.1.el5 i386
11:57:56.554 INFO - v2.21.0, with Core v2.21.0. Built from revision 16552
11:57:57.774 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub
11:57:57.775 INFO - Version Jetty/5.1.x
11:57:57.776 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
11:57:57.777 INFO - Started HttpContext[/selenium-server,/selenium-server]
11:57:57.777 INFO - Started HttpContext[/,/]
11:58:01.141 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@90cb03
11:58:01.141 INFO - Started HttpContext[/wd,/wd]
11:58:01.145 INFO - Started SocketListener on 0.0.0.0:4444
11:58:01.145 INFO - Started org.openqa.jetty.jetty.Server@af8358
11:59:57.941 INFO - Executing: [new session: {javascriptEnabled=true, browserName=firefox, nativeEvents=false, version=}] at URL: /session)
12:00:11.527 INFO - Done: /session
12:00:11.531 INFO - Executing: org.openqa.selenium.remote.server.handler.GetSessionCapabilities@134263a at URL: /session/1338285476985)
12:00:11.531 INFO - Done: /session/1338285476985
12:00:11.532 INFO - WebDriver remote server: Fatal, unhandled exception: /session/1338285476985: java.util.concurrent.ExecutionException: java.lang.NullPointerException
12:00:11.532 WARN - EXCEPTION
javax.servlet.ServletException: java.util.concurrent.ExecutionException: java.lang.NullPointerException
        at org.openqa.selenium.remote.server.DriverServlet.handleRequest(DriverServlet.java:603)
        at org.openqa.selenium.remote.server.DriverServlet.doGet(DriverServlet.java:539)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
        at org.openqa.selenium.remote.server.DriverServlet.service(DriverServlet.java:520)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
        at org.openqa.jetty.jetty.servlet.ServletHolder.handle(ServletHolder.java:428)
        at org.openqa.jetty.jetty.servlet.ServletHandler.dispatch(ServletHandler.java:677)
        at org.openqa.jetty.jetty.servlet.ServletHandler.handle(ServletHandler.java:568)
        at org.openqa.jetty.http.HttpContext.handle(HttpContext.java:1530)
        at org.openqa.jetty.http.HttpContext.handle(HttpContext.java:1482)
        at org.openqa.jetty.http.HttpServer.service(HttpServer.java:909)
        at org.openqa.jetty.http.HttpConnection.service(HttpConnection.java:820)
        at org.openqa.jetty.http.HttpConnection.handleNext(HttpConnection.java:986)
        at org.openqa.jetty.http.HttpConnection.handle(HttpConnection.java:837)
        at org.openqa.jetty.http.SocketListener.handleConnection(SocketListener.java:243)
        at org.openqa.jetty.util.ThreadedServer.handle(ThreadedServer.java:357)
        at org.openqa.jetty.util.ThreadPool$PoolThread.run(ThreadPool.java:534)
Caused by: java.util.concurrent.ExecutionException: java.lang.NullPointerException
        at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:222)
        at java.util.concurrent.FutureTask.get(FutureTask.java:83)
        at org.openqa.selenium.remote.server.DefaultSession.execute(DefaultSession.java:158)
        at org.openqa.selenium.remote.server.handler.WebDriverHandler.execute(WebDriverHandler.java:90)
        at org.openqa.selenium.remote.server.rest.ResultConfig.handle(ResultConfig.java:240)
        at org.openqa.selenium.remote.server.DriverServlet.handleRequest(DriverServlet.java:597)
        ... 16 more
Caused by: java.lang.NullPointerException
        at org.openqa.selenium.remote.server.renderer.ForwardResult.render(ForwardResult.java:39)
        at org.openqa.selenium.remote.server.rest.ResultConfig$1.call(ResultConfig.java:233)
        at org.openqa.selenium.remote.server.rest.ResultConfig$1.call(ResultConfig.java:1)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
        at java.util.concurrent.FutureTask.run(FutureTask.java:138)
        at org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:151)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:662)
12:00:11.533 WARN - /wd/hub/session/1338285476985:
java.util.concurrent.ExecutionException: java.lang.NullPointerException
        at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:222)
        at java.util.concurrent.FutureTask.get(FutureTask.java:83)
        at org.openqa.selenium.remote.server.DefaultSession.execute(DefaultSession.java:158)
        at org.openqa.selenium.remote.server.handler.WebDriverHandler.execute(WebDriverHandler.java:90)
        at org.openqa.selenium.remote.server.rest.ResultConfig.handle(ResultConfig.java:240)
        at org.openqa.selenium.remote.server.DriverServlet.handleRequest(DriverServlet.java:597)
        at org.openqa.selenium.remote.server.DriverServlet.doGet(DriverServlet.java:539)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
        at org.openqa.selenium.remote.server.DriverServlet.service(DriverServlet.java:520)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
        at org.openqa.jetty.jetty.servlet.ServletHolder.handle(ServletHolder.java:428)
        at org.openqa.jetty.jetty.servlet.ServletHandler.dispatch(ServletHandler.java:677)
        at org.openqa.jetty.jetty.servlet.ServletHandler.handle(ServletHandler.java:568)
        at org.openqa.jetty.http.HttpContext.handle(HttpContext.java:1530)
        at org.openqa.jetty.http.HttpContext.handle(HttpContext.java:1482)
        at org.openqa.jetty.http.HttpServer.service(HttpServer.java:909)
        at org.openqa.jetty.http.HttpConnection.service(HttpConnection.java:820)
        at org.openqa.jetty.http.HttpConnection.handleNext(HttpConnection.java:986)
        at org.openqa.jetty.http.HttpConnection.handle(HttpConnection.java:837)
        at org.openqa.jetty.http.SocketListener.handleConnection(SocketListener.java:243)
        at org.openqa.jetty.util.ThreadedServer.handle(ThreadedServer.java:357)
        at org.openqa.jetty.util.ThreadPool$PoolThread.run(ThreadPool.java:534)
Caused by: java.lang.NullPointerException
        at org.openqa.selenium.remote.server.renderer.ForwardResult.render(ForwardResult.java:39)
        at org.openqa.selenium.remote.server.rest.ResultConfig$1.call(ResultConfig.java:233)
        at org.openqa.selenium.remote.server.rest.ResultConfig$1.call(ResultConfig.java:1)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
        at java.util.concurrent.FutureTask.run(FutureTask.java:138)
        at org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:151)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:662)
12:00:11.540 INFO - Executing: [get: http://google.com] at URL: /session/1338285476985/url)
12:00:12.664 INFO - Done: /session/1338285476985/url
12:00:12.666 INFO - Executing: [find element: By.name: q] at URL: /session/1338285476985/element)
12:00:12.956 INFO - Done: /session/1338285476985/element
12:00:12.961 INFO - Executing: [send keys: 0 org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement@13472277, [selenium google code]] at URL: /session/1338285476985/element/0/value)
12:00:13.096 INFO - Done: /session/1338285476985/element/0/value
12:00:13.097 INFO - Executing: [submit: 0 org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement@13472277] at URL: /session/1338285476985/element/0/submit)
12:00:13.227 INFO - Done: /session/1338285476985/element/0/submit
12:00:13.228 INFO - Executing: [delete session: 1338285476985] at URL: /session/1338285476985)
12:00:13.304 INFO - Done: /session/1338285476985

执行 Java 测试时的服务器输出(未抛出异常):

May 29, 2012 12:02:26 PM org.openqa.grid.selenium.GridLauncher main
INFO: Launching a standalone server
12:02:26.682 INFO - Java: Sun Microsystems Inc. 20.0-b11
12:02:26.684 INFO - OS: Linux 2.6.18-308.1.1.el5 i386
12:02:26.695 INFO - v2.21.0, with Core v2.21.0. Built from revision 16552
12:02:26.816 INFO - RemoteWebDriver instances should connect to: http://127.0.0.   1:4444/wd/hub
12:02:26.818 INFO - Version Jetty/5.1.x
12:02:26.818 INFO - Started HttpContext[/selenium-server/driver,/selenium-server   /driver]
12:02:26.819 INFO - Started HttpContext[/selenium-server,/selenium-server]
12:02:26.819 INFO - Started HttpContext[/,/]
12:02:26.842 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@288051
12:02:26.842 INFO - Started HttpContext[/wd,/wd]
12:02:26.846 INFO - Started SocketListener on 0.0.0.0:4444
12:02:26.846 INFO - Started org.openqa.jetty.jetty.Server@af8358
12:04:02.749 INFO - Executing: [new session: {platform=ANY, browserName=firefox     version=}] at URL: /session)
12:04:04.910 INFO - Done: /session
12:04:04.949 INFO - Executing: org.openqa.selenium.remote.server.handler.GetSes    ionCapabilities@134263a at URL: /session/1338285746744)
12:04:04.950 INFO - Done: /session/1338285746744
12:04:04.991 INFO - Executing: [get: http://www.google.com] at URL: /session/13    8285746744/url)
12:04:05.624 INFO - Done: /session/1338285746744/url
12:04:05.658 INFO - Executing: [find element: By.name: q] at URL: /session/1338    85746744/element)
12:04:05.919 INFO - Done: /session/1338285746744/element
12:04:05.988 INFO - Executing: [send keys: 0 org.openqa.selenium.support.events    EventFiringWebDriver$EventFiringWebElement@4175a8c3, [selenium google code]] at    URL: /session/1338285746744/element/0/value)
12:04:06.095 INFO - Done: /session/1338285746744/element/0/value
12:04:06.124 INFO - Executing: [submit: 0 org.openqa.selenium.support.events.Ev    ntFiringWebDriver$EventFiringWebElement@4175a8c3] at URL: /session/133828574674    /element/0/submit)
12:04:06.247 INFO - Done: /session/1338285746744/element/0/submit
12:04:06.274 INFO - Executing: [delete session: 1338285746744] at URL: /session    1338285746744)
12:04:06.346 INFO - Done: /session/1338285746744

有人遇到过这个吗?我知道 Selenium 不正式支持 PHP。但我认为它不应该让任何连接像这样使服务器失败。我还想知道,尽管示例代码显着地显示在 php-webdriver-bindings 的主页上,但显然没有人注意到。我究竟做错了什么?

4

1 回答 1

1

这是由于您使用的 PHP WebDriver 绑定库发送的“Accept:”标头不正确。

在会话初始化时,您需要指定标头:“Accept: application/json”

为此,您需要稍微修补您的库或升级到已经可以进行此修复的最新版本。

要手动应用补丁 - 找到将初始请求发送到 WebDriver 服务器的行:

curl_setopt( $this->_curl, CURLOPT_HTTPHEADER, array("application/json;charset=UTF-8"));

并将其更改为:

curl_setopt( $this->_curl, CURLOPT_HTTPHEADER, array("application/json;charset=UTF-8", "Accept: application/json"));

错误将不再发生。在此处查看更多信息:

http://code.google.com/p/selenium/issues/detail?id=2091

https://github.com/facebook/php-webdriver/issues/9#issuecomment-3381300

于 2012-06-18T21:49:23.660 回答