Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration workaround needed to allow for modular, per-page elements #76

Closed
walterdolce opened this issue Oct 12, 2016 · 6 comments
Closed

Comments

@walterdolce
Copy link
Contributor

Hi,

In order to achieve something like the following in terms of code/folders structure:

behat.yml
application/
  features/
    bootstrap/
      Page/
        Homepage/
          Element/
            LoginButton.php
            RegisterButton.php
        Registration/
          Element/
            Email.php
            Password.php
            SubmitButton.php

It seems like the configuration setup below is necessary for the extension to work - note the empty namespace declaration for the elements:

# behat.yml
default:
  autoload:
    '': %paths.base%/application/features/bootstrap
  extensions:
    SensioLabs\Behat\PageObjectExtension:
      namespaces:
        element:
          -

Otherwise, an error like the following would be thrown:

# ...

 When the user clicks on the register button

 Could not find a class for the "Page\Homepage\Element\RegisterButton" element. None of the configured namespaces worked: "\Page\Element\Page\Homepage\Element\RegisterButton" (InvalidArgumentException)

# ...

Is the need to configure an empty namespace the only way to make the extension happy?
Would it make sense to have the extension just get the element class based on what's passed to the Page::getElement method (after perhaps checking the existence in the "base namespace", so to avoid breaking other people's code out there....), like so:

# SomeContext
# RegisterButton::class -> Page\Homepage\Element\RegisterButton
$this->homepage->getElement(RegisterButton::class)->click();

Note this is not a declaration of a bug. Rather a genuine question as to what's the best way to achieve separation of components on a page basis (because not all pages have the same elements and namespacing them after the page they're contained within makes sense and gives a sense of cleanliness and structure - IMO).

Thanks

@walterdolce walterdolce changed the title Unnecessary configuration workaround needed to allow for modular, per-page elements Configuration workaround needed to allow for modular, per-page elements Oct 12, 2016
@DonCallisto
Copy link
Contributor

@walterdolce I've got this type of organization. I've simply defined my PageNameResolver

<?php

namespace NuvolaBehat\Page;

use SensioLabs\Behat\PageObjectExtension\PageObject\Factory\ClassNameResolver;

class PageNameResolver implements ClassNameResolver
{
    /**
     * @param string $pageFQCN
     *
     * @return string
     */
    public function resolvePage($pageFQCN)
    {
        return $pageFQCN;
    }

    /**
     * @param string $elementFQCN
     *
     * @return string
     */
    public function resolveElement($elementFQCN)
    {
        return $elementFQCN;
    }
}

that work as you expect

$this->homepage->getElement(RegisterButton::class)->click();

@jakzal
Copy link
Member

jakzal commented Jan 24, 2017

Just FYI, in future versions I'd like to reference page objects by FQCN rather than their name by default.

@DonCallisto
Copy link
Contributor

Cool, when new version comes out, I'll remove my PageNameResolver ;)

@DonCallisto
Copy link
Contributor

@jakzal if you want, I can try to make an implementation for FQCN. Something like: check if it's a valid (existing) one, check if it's an instance of that type (interface) and, then, return it. Maybe I can make a PR with RFC status. Let me know if you agree with this :)

@jakzal
Copy link
Member

jakzal commented Jan 29, 2017

Sure, PR would be nice :) Could be as simple as returning the class right away if it exists from the existing implementation, or a new implementation.

@jakzal
Copy link
Member

jakzal commented May 22, 2017

I'll close this one as since FQCN is implemented you can put your page objects anywhere.

@jakzal jakzal closed this as completed May 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants