2017-04-21 69 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

相关问题