Search code examples
phpxpathbehatmink

xpath issue using behat for angularjs application


I've been trying to make a web crawler for the company i work for using behat combined with the mink extension. I did something similar in the past as well but the difference is that now the page i am trying to crawl through is build in angularjs.

This seems to cause an issue in my xpath selector which can not locate the elements i am requesting in the DOM.

To ensure i have the right xpath and double check it i also use the Xpath Helper from chrome (extension).

My function that i try to use is :

/**
 * @Then I login
 */
public function login()
{
    $page = $this->getSession()->getPage();

    $username = $page->find('xpath', "//*[@id='inputEmail']/self::INPUT");
    $password = $page->find('xpath', "//*[@id='inputPassword']/self::INPUT");

    if ($username == null && $password == null) {
       echo "nothing found";
    }
    else{
        $username->setValue('username');
        $password->setValue('password');
    }
}

In my .feature file i am able to access the login page and check that i am there by "seeing" some text in the dom to verify it is working, but when i try to fill the values in the fields above i am getting an error.

I guess it is an angular issue by the way it creates the DOM elements which causes my xpath unable to locate the fields i need but i can't think of a work around for this one.

Of course to use a JS framework like Protractor can be a solution but i would like to stay in php as it is easier maintainable by the team i am working with.

Any ideas would be more than welcome.


Solution

  • Few ideas:

    1. Use page objects to clean your code
    2. Make sure the element is present before using it by waiting for it (find() can return null or element)
    3. If you don't need UI use guzzle or use some headless driver
    4. Try to create a wait condition specific for the angular if you are using the UI