2013-05-27 40 views
3

我試圖在我的硒測試套件中執行JavaScript,但它不工作,我沒有收到錯誤反饋。它優雅地接受我輸入的任何參數作爲執行功能並通過測試。以下是結合我曾嘗試:使用php-webdriver-bindings-0.9.0在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

+0

我希望在測試達到在瀏覽器中調用$ this-> webdriver-> execute()的點時獲得警報。 –

+0

@ManiMuridi讓'execute'或'execute_async'工作有什麼好運?有一個令人不安的缺乏使用文件! – tester

回答

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,有一個類似的問題:(原稿從https://github.com/sebastianbergmann/phpunit-selenium/issues/160

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() 
    } 
} 

相關問題