如何使用PHPUnit,Selenium和php-webdriver

时间:2015-11-17 00:20:32

标签: php selenium selenium-webdriver phpunit

我正在尝试使用它们之间的共享会话运行多个测试,从登录开始。

   <?php

   class ExampleTest extends PHPUnit_Framework_TestCase {

       /**
        * @var \RemoteWebDriver
        */

       protected $webDriver;
       protected $host = 'http://localhost:4444/wd/hub';
       protected $browser = array (
         'browserName' => 'chrome',
         'sessionStrategy' => 'shared'
       );

       public function setUp()
       {
         $this->webDriver = RemoteWebDriver::create($this->host, $this->browser);
       }

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

我的第一个测试是登录,工作正常:

   public function testLogin()
   {
     $this->webDriver->get('http://localhost:8888/public');
     $this->webDriver->findElement(WebDriverBy::name("login"))->sendKeys("logintest");
     $this->webDriver->findElement(WebDriverBy::name("password"))->sendKeys("passwordtest");
     $this->webDriver->findElement(WebDriverBy::className("btn"))->click();
   }

然后我想测试表行的点击(基于行内的值):

   public function testFranchiseClick()
   {
     $this->webDriver->get('http://localhost:8888/public/franchises');
     $this->webDriver->findElement(WebDriverBy::xpath("//td[contains(text(), 'TestPortal')]"))->click();
   }

不幸的是我收到以下错误:

   NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//td[contains(text(), 'TestPortal')]"}

我确信这是因为测试没有使用相同的会话(用户没有登录,因此没有访问/特许经营页面),因为如果我在testLogin()函数中包含这两个指令,它可以正常工作。

知道我做错了什么?我仍然希望能够使用这些“findElement(WebDriverBy ::”东西。

提前多多感谢!

2 个答案:

答案 0 :(得分:1)

您的测试需要独立,以便您可以毫无问题地运行其中一个测试。这意味着对于每个测试,您必须执行登录,执行您的操作,然后注销。如果我可以给你一个建议,请创建一个方法登录,您将在测试中使用它。在此登录方法中,您将获取元素并与它们进行交互。

答案 1 :(得分:0)

我使用过Selenium,但我不熟悉PHP,是在每次测试后执行了tearDown()还是在所有测试用例之后执行了一次?