使用php-webdriver-bindings-0.9.0在selenium中执行javascript

3

我正在尝试在我的selenium测试套件中执行JavaScript,但它没有工作,并且我没有得到任何错误反馈。它会优雅地接受我输入的任何参数并通过测试。以下是我尝试过的组合:

class TestingStuff extends PHPUnit_Framework_TestCase {

protected function setUp() {
    $this->webdriver = new WebDriver("localhost", 4444);
    $this->webdriver->connect("firefox");
}

protected function tearDown() {
    $this->webdriver->close();
}

public function testSomething() {
    $this->webdriver->get('http://localhost/testdir/');
    // Here is the execute function
    $this->webdriver->execute('alert', 'Hello');

$this->webdriver->get('http://127.0.0.1/testdir/');
// Here is the execute function
$this->webdriver->execute('alert("Hello")', '');

$this->webdriver->get('http://127.0.0.1/testdir/');
// Here is the execute function
$this->webdriver->execute('javascript:alert("Hello")', '');

$this->webdriver->get('http://localhost/testdir/');
// Here is the execute function
$this->webdriver->execute('alert()', 'Hello');
}

}

这是“WebDriver”类中的函数:

/**
Inject a snippet of JavaScript into the page for execution in the context of the currently   selected frame.
* The executed script is assumed to be synchronous and the result of evaluating the script
* is returned to the client.
* @return Object result of evaluating the script is returned to the client.
*/
public function execute($script, $script_args) {
    $request = $this->requestURL . "/execute";
    $session = $this->curlInit($request);
    $args = array('script' => $script, 'args' => $script_args);
    $jsonData = json_encode($args);
    $this->preparePOST($session, $jsonData);
    $response = curl_exec($session);
    return $this->extractValueFromJsonResponse($response);
}

2
你期望发生什么?实际上发生了什么?没有发生什么? - Arran
我期望在测试到达调用 $this->webdriver->execute() 的浏览器端点时收到警报。 - Mani Muridi
@ManiMuridi 你有没有成功地让execute或者execute_async工作起来?对于它们的使用,文档实在是太少了,令人困扰! - tester
1个回答

7

看一看

https://github.com/scopium/Work/blob/master/php-webdriver-bindings-0.9.0/test/PHPWebdriverTest.php

public function testExecute() {
    $this->webdriver->get(TEST_URL);
    $result = $this->webdriver->executeScript("return sayHello('unitTest')", array());
    $this->assertEquals("hello unitTest !!!", $result);
}

这是你想要实现的吗?
我正在使用phpunit-selenium,遇到了类似的问题:
class MyTestClass extends PHPUnit_Extensions_Selenium2TestCase
{
   protected function setUp()
   {
     $this->setBrowser('firefox');
     $this->setBrowserUrl('http://yourdomain.tld');
   }

   public function testScript()
   {
     $this->url('./');
     $this->execute(array('script' => "alert('Hello');", 'args' => array()));
     sleep(3); // Just to see the alert() 
   }
}

(原文来自 https://github.com/sebastianbergmann/phpunit-selenium/issues/160)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接