2

My problem: I am running phpunit with Selenium to test a website on a server that is on the other side of the world. So, there is a delay of a few seconds for things like clicking on a tab or a new page. I start Selenium Server with Chromedriver. eg.

    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
    }

There is an error reported when the above is run and I capture it in an xml file: ********::testSearch PHPUnit_Extensions_Selenium2TestCase_WebDriverException: unknown error: Element ... is not clickable at point (430, 139). Other element would receive the click: (Session info: chrome=57.0.2987.133) (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233)

What I am trying to do is to wait for the DOM to be completely loaded before clicking on a button. But I get the above error intermittently. Does anyone know a way around this?? Its driving me nuts!!

1

2 Answers 2

3

Try the Explicit Waits. "An explicit wait is the code you define to wait for a certain condition to occur before proceeding further in the code. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished."

For example,

// 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'));

There are many prepared conditions you can pass to the until() method. All of them subclass WebDriverExpectedCondition, including elementToBeClickable() (https://github.com/facebook/php-webdriver/wiki/HowTo-Wait).

1
0

I don't know if this will help you but in java there is a method to wait for a certain item to be visible

Here is how it is written

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

Sorry I don't know how to write it in PHP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.