2013-06-13 95 views
0

我有一個名稱用於在表單中生成隨機名的數組,我想在多個函數中使用它。phpunit_Selenium2中的全局變量

class TestMyTest extends PHPUnit_Extensions_Selenium2TestCase { 
public function setUp() 
{ 
$this->setHost('localhost'); 
$this->setPort(4444); 
$this->setBrowser("firefox"); 
$this->setBrowserUrl("xxxxxxxxxxxxxxxxxx"); 
} 
    public $firstNameArray = array(
    "Howard", 
    "Sheldon", 
    "Leonard", 
    "Rajesh" 
    ); 

    public $lastNameArray = array(
    "Wolowitz", 
    "Cooper", 
    "Hofstadter", 
    "Koothrappali" 
    ); 


public function testCreateNewCustomer() 
{ 

    $name = rand(0,count($firstNameArray)); 
    $this->byId('customer-name')->value($firstNameArray[$name].' '.$lastNameArray[$name]); 
    $random_phone_nr = rand(1000000,9999999); 
    $this->byId('customer-phone')->value('070'.$random_phone_nr); 
    $this->byId('customer-email')->value($firstNameArray[$name].'.'.$lastNameArray[$name].'@testcomp.com'); 
} 

這給我錯誤「Undefined variable:firstNameArray」在我聲明$ name的變量的行上。我不希望必須在每個我想使用它的函數中聲明相同的數組。那麼我該如何解決這個問題?

+0

要使用爲什麼這個數據通過一個類變量?我認爲你應該考慮使用Dataprovider(http://phpunit.de/manual/3.7/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.data-providers)或簡單地把這個日期在你的測試方法中。 – nonsenz

回答

1

它不是一個全局變量,它是一個類/實例變量:

$this->byId('customer-name')->value($this->firstNameArray[$name].' '.$this->lastNameArray[$name]); 
$this->byId('customer-email')->value($this->firstNameArray[$name].'.'.$this->lastNameArray[$name].'@testcomp.com'); 

編輯

對不起,我錯過了一個參考:

$name = rand(0,count($this->firstNameArray)-1); 
+0

嗯,我仍然得到一個錯誤,但不同的一個「不能訪問空屬性」在同一行。 – user1593846

+0

是的,我知道,當然我編輯$ firstNameArray pressent所有行$ this - > $ firstNameArray。 – user1593846

+0

你的rand()應該在0和count-1之間,而不是在0和count之間 –