2017-04-21 125 views
1

我的問題: 我正在運行phpunit和Selenium來測試位於世界另一端的服務器上的網站。所以,像點擊一個標籤頁或新頁面之類的東西會延遲幾秒鐘。我用Chromedriver啓動Selenium Server。 例如。如何讓Selenium在執行click()命令之前等待頁面完全加載

public function setUp() 
    { 
     $this->setHost('localhost'); // Set the hostname for the connection to the Selenium server. 
     $this->setPort(4444); // set port # for connection to selenium server 
     $this->setBrowser('chrome'); // set the browser to be used 
     $this->setBrowserUrl('https://www.*.com'); // set base URL for tests 
     $this->prepareSession()->currentWindow()->maximize(); // Maximize the window when the test starts 
     $this->timeouts()->implicitWait(30000); // Wait up to 30 seconds for all elements to appear 
    } 

    public function testLoginToeSeaCare(){ 
     $this->timeouts()->implicitWait(10000); // Wait up to 10 seconds for all elements to appear 

     $url = 'https://www.*.com'; 
     $loginName = 'Ned'; 
     $loginPassword = 'Flanders'; 

     $this->url($url); // Load this url 

     $this->timeouts()->implicitWait(30000); // Wait up to 30 seconds for all elements to appear 
     $username = $this->byId('username'); // Search page for input that has an id = 'username' and assign it to $username 
     $password = $this->byId('password'); // Search page for input that has an id = 'password' and assign it to $password 

     $this->byId('username')->value($loginName); // Enter the $loginName text in username field 
     $this->byId('password')->value($loginPassword); // Enter the $loginPassword in password field 
     $this->byCssSelector('form')->submit(); // submit the form 


     $tab1Link = $this->byLinkText("Tab1"); // Search for the textlink Tab1 
     $this->assertEquals('Tab1', $tab1Link->text()); // assert tab text is present 

     $this->timeouts()->implicitWait(10000); // Wait up to 10 seconds for all elements to appear 
     $tab2Link = $this->byLinkText("Tab2"); 
     $tab2Link->click(); // Click 'Tab2' tab 
    } 

就有了上面運行,我抓住它在一個XML文件時報告了一個錯誤: ******** :: testSearch PHPUnit_Extensions_Selenium2TestCase_WebDriverException:未知的錯誤:元素...無法點擊在點(430,139)。其他元素將收到點擊:(會話信息:鉻= 57.0.2987.133)(驅動程序信息:chromedriver = 2.29.461591(62ebf098771772160f391d75e589dc567915b233)

我所試圖做的是等待DOM之前完全加載點擊一個按鈕但是我間歇地得到了上面的錯誤有沒有人知道解決這個問題的方法??它驅使我堅果!!

+0

請參閱如何檢查頁面是否已加載-https://sqa.stackexchange.com/questions/26776/how-to-verify-if-a-web-page - 已被正確加載或沒有/ 26786#26786 – demouser123

回答

2

嘗試顯式等待 「顯式等待是您定義的代碼等待在繼續進行代碼之前會出現一定的條件,有一些方便的方法可以幫助你編寫代碼,只需要等待就可以等待,WebDriverWait和ExpectedCondition結合是一種可以完成的方法。「

例如,

// Wait for the page title to be 'My Page'. 

// Default wait (= 30 sec) 
$driver->wait()->until(WebDriverExpectedCondition::titleIs('My Page')); 


// Wait for at most 10s and retry every 500ms if it the title is not  correct. 
$driver->wait(10, 500)->until(WebDriverExpectedCondition::titleIs('My Page')); 

有可以傳遞到直到()方法制備的許多條件。它們都是WebDriverExpectedCondition的子類,包括elementToBeClickable()(https://github.com/facebook/php-webdriver/wiki/HowTo-Wait)。

+0

@GhostCat固定。 – captainderteufel

0

我不知道這是否會幫助你 但在Java中有等待某一個項目是可見的

這裏是它是怎麼寫的

WebDriverWait Wait=new WebDriverWait(Driver, 10); 
Wait.until(ExpectedConditions.visibilityOf(Driver.findElement(By.//your element locator))); 

對不起我的方法不知道如何寫入PHP

相關問題