Giter Club home page Giter Club logo

wordpressbehatextension's Introduction

WordPress Extension for Behat 3

This is a Behat 3.0 Extension for WordPress plugin and theme development.

The Extension allows you to use WordPress functions in your context class if you include StephenHarris\WordPressBehatExtension\Context\WordPressContext (or create and include a child class of it, i.e. make your FeatureContext ).

It also provides other contexts.

Version: 0.4.0 . This project follows SemVer.

History

This repository started off as a fork of:

Installation

(For 'quick start' guides, please see the Recipes).

  1. Add a composer development requirement for your WordPress theme or plugin:

    {
        "repositories": [
            {
                "type": "vcs",
                "url": "https://github.com/stephenharris/WordPressBehatExtension.git"
            }
        ],
        "require-dev" : {
            "stephenharris/wordpress-behat-extension": "~0.3",
            "behat/mink-goutte-driver": "~1.1",
            "behat/mink-selenium2-driver": "~1.3.1",
            "johnpbloch/wordpress": "~4.6.1"
        }
    }

    You don't have to install WordPress via composer. But you shall need a path to a WordPress install below. Additionally you don't have to use the Goutte and Selenium2 drivers, but these are the most common.

  2. Add the following Behat configuration file below. You will need:

  • The path to your WordPress install (here assumed vendor/wordpress, relative to your project's root directory).

  • The database, and database username and password of your WordPress install (here assumed wordress_test, root, '')

  • The URL of your WordPress install (In this example we'll be using php's build in server)

  • A temporary directory to store e-mails that are 'sent'

    default:
      suites:
        default:
          contexts:
            - FeatureContext
            - \StephenHarris\WordPressBehatExtension\Context\WordPressContext
            - \StephenHarris\WordPressBehatExtension\Context\Plugins\WordPressPluginContext
            # and any other contexts you need, please see the documentation
      extensions:
        StephenHarris\WordPressBehatExtension:
          path: '%paths.base%/vendor/wordpress'
          connection:
            host: 'localhost'
            db: 'wordpress_test'
            username: 'root'
            password: ''
          mail:
            directory: '/tmp/mail'
        Behat\MinkExtension:
          base_url:    'http://localhost:8000'
          goutte: ~
          selenium2: ~

    Note the StephenHarris\WordPressBehatExtension\Context\WordPressContext context included. This will cause WordPress to be loaded, and all its functions available in your context classes.. You can also include other contexts.

  1. Install the vendors and initialize behat test suites

    composer update
    # You will need to ensure a WordPress install is available, with database credentials that
    # mach the configuration file above
    vendor/bin/behat --init
  2. Write some Behat features in your project's features directory and define any steps. The WordPressContext context will make all WordPress functions available in your context classes (but there is a better way).

    Feature: Manage plugins
        In order to manage plugins
        As an admin
        I need to enable and disable plugins
    
        Background:
            Given I have a vanilla wordpress installation
                | name          | email                   | username | password |
                | BDD WordPress | [email protected]          | admin    | test     |
            And I am logged in as "admin" with password "test"
    
        Scenario: Enable the dolly plugin
            Given there are plugins
                | plugin    | status  |
                | hello.php | enabled |
            When I go to "/wp-admin/"
            Then I should see a "#dolly" element
    
        Scenario: Disable the dolly plugin
            Given there are plugins
                | plugin    | status   |
                | hello.php | disabled |
            When I go to "/wp-admin/"
            Then I should not see a "#dolly" element
    
    
  3. Run the tests

In our example, since we using PHP's built-in web sever, this will need to be started so that Behat can access our site.

php -S localhost:8000 -t vendor/wordpress -d disable_functions=mail
```

```bash
vendor/bin/behat
```

Documentation

Please see the Docs.

Aim

The aim of this project is to provide a collection of context classes that allow for easy testing of WordPress' core functionality. Those contexts can then be built upon to test your site/plugin/theme-specific functionality.

License

WordPressBehatExtension is open source and released under MIT license. See LICENSE file for more info.

Health Warning

This is not to be used on a live site. Your database will be cleared of all data.

Currently this extension also over-rides your wp-config.php but this implementation may change in the future.

The extension installs three mu-plugins into your install (which it assumes is at {site-path}/wp-content/mu-plugins). These plug-ins do the following:

  • wp-mail.php - over-rides wp_mail() function to store the e-mails locally
  • wp-install.php - over-rides wp_install_defaults() to prevent any default content being created, with the exception of the 'Uncategorised' category.
  • move-admin-bar-to-back.php - a workaround for #1 which prevent elements from being hidden from Selenium behind the admin menu bar.

Changelog

A changelog can be found at CHANGELOG.md.

How to help

This project needs a lot of love :). You can help by doing any of the following

  • Opening an issue to request a context / step definitions
  • Submitting a PR to add a context / step definition
  • Submiting a PR to add to or improve the documentation
  • Opening an issue you have questions or find any bugs
  • Just using this extension in your development / testing workflow and providing your feedback

wordpressbehatextension's People

Stargazers

Nate Luzod avatar Alexander Barton avatar David avatar Otto Rask avatar Steve Persch avatar Matthias Pfefferle avatar Pascal Birchler avatar Stephen Edgar avatar

Watchers

Otto Rask avatar James Cloos avatar Stephen Harris avatar Christian Leucht avatar

wordpressbehatextension's Issues

Something's changed (dependency tree) issue

Hi, my composer.json is below.

I can't seem to install this library. Whenever I try, I use composer update -vvv --profile and when the dependency tree is evaluated, it never stops. I got up to pass#60 before stopping it. I'd previously let it run to about 120 passes.

This is a child theme to a wordpress install. The composer.json is in the child theme's directory.

My log is attached as a txt file.
log.txt

{
  "name": "qnl/qnl_the7child",
  "description": "QNL Child theme based on The7 Wordpress Theme",
  "minimum-stability": "dev",
  "prefer-stable" : true,
  "license": "proprietary",
  "authors": [
    {
      "name": "Christopher Fern",
      "email": "[email protected]"
    }
  ],
  "require-dev": {
    "phpunit/phpunit": "7.*",
    "behat/gherkin" : "^4.6.2",
    "symfony/event-dispatcher": "4.3.*",

    "stephenharris/wordpress-behat-extension": "dev-master",
    "behat/mink-goutte-driver": "1.2.*",
    "behat/mink-selenium2-driver": "1.4.*",
    "johnpbloch/wordpress": "~4.6.1",

    "cweagans/composer-patches": "^1.6.7",
    "behat/behat": "3.6.*",
    "behat/debug-extension": "1.*",
    "genesis/behat-fail-aid": "3.7.*",
    "roave/security-advisories": "dev-master"
  },
  "require": {
    "php": "7.2.*",
    "dereuromark/media-embed": "0.5.3"
  },
  "autoload": {
    "psr-4": {
      "QNL\\" : "./QNL/"
    }
  },
  "config": {
    "bin-dir": "bin/"
  }
}

"Element is not clickable at point"

Sometimes Selenium cannot click an element because the "Element is not clickable at point". This can be because the target element is underneath the admin bar.

A quick fix might be to set the z-index of the admin bar to 0.

A more robust solution might be to over-ride the Behat-Mink methods to ensure the browser screen is shifted so that the element is visible. Or (if possible) intercept the failure and attempt a few more times with the browser screen adjusted.

Call to undefined function

I would like to use WordPress functions in my own FeatureContext class and write steps which are not presented in WordPressBehatExtension. Behat returns some issue when I try example from documentation. If my behat.yml file looks like this:

default:
  suites:
    default:
      contexts:
        - FeatureContext
        - \StephenHarris\WordPressBehatExtension\Context\WordPressContext
        - \StephenHarris\WordPressBehatExtension\Context\Plugins\WordPressPluginContext
  extensions:
    StephenHarris\WordPressBehatExtension:
      path: '/var/www/wordpress'
      connection:
        db: "wordpress"
        username: "wordpress"
        password: "wordpress"
      flush_database: false
      mail:
        directory: '/tmp/mail'
    Behat\MinkExtension:
      base_url: "http://192.168.33.10"
      browser_name: chrome
      goutte: ~
      selenium2: ~

and my .feature file looks like this:

Feature: Admin activates plugin
  In order to use the plugin
  As a WordPress user
  I should be able to activate it

  Scenario: Enable the dolly plugin
    Given there are plugins
      | plugin    | status  |
      | hello.php | enabled |
    When I go to "/wp-admin/"
    Then I should see a "#dolly" element

I get error when try to run Behat:

PHP Fatal error: Call to undefined function StephenHarris\WordPressBehatExtension\Context\Plugins\activate_plugin() in /home/vagrant/my-plugin/vendor/stephenharris/wordpress-behat-extension/src/Context/Plugins/WordPressPluginContext.php on line 21

What did I do wrong? I use vccw vagrant and run tests after I ssh into it.

Tests sometimes fail at the log-in stage.

I've seen this on Travis and when running locally. It seems for some reason, the web driver (Selenium) sometimes enters the password into the username field.

I'm not sure what the best way forward is. We could:

  • Check for this and try logging-in again
  • Improve the step implementation for logging so that the field is double checked that it's the correct one.

Custom taxonomy not available when WordPressTermContext->insert()

My plugin has a custom taxonomy that works, but when I try to create some terms like:

Background:
  Given [...]
  And there are plugins
        | plugin              | status  |
        | library/library.php | enabled |
  And there are document_tag terms
        | name      | slug | description   |
        | English   | en   |               |
        | Spanish   | es   |               |

...I get an error: "exception 'InvalidArgumentException' with message 'Invalid taxonomy term information schema: Invalid taxonomy.' in vendor/stephenharris/wordpress-behat-extension/src/Context/Terms/WordPressTermTrait.php:21"

This seems to be because the taxonomy is not available/registered when the context runs wp_insert_term(). If I run get_taxonomies() in the exception, my taxonomy isn't listed, but if I run is_plugin_active('library/library.php'), it is active.

I'll keep trying to debug, but would welcome guidance. I'm new to behat, so could definitely be missing something simple.

Note: I can run (and pass) scenarios where the same terms are created, but when setting them up as background, I get an error.

Error message for incorrect password when logging in incorrect

When I am logged in as :username with password :password verifies the username and password. If the password is incorrect we should get the message:

Password for user :username incorrect

Instead we se:

Password for user :password incorrect

Perhaps the password should also be included in the error message for clarity.

WPTable HTML not parsed correctly in selenium

I'm not sure exactly what's happening yet, but it seems like there's something in WPTableRow or WPTableCell that's incompatible with the HTML/DOM used for post lists, at least with JS on. In Goutte I get passing tests, but in Selenium 2, I get a failure for the same test.

As an example, here is the (lightly edited) output for one of "Then the post list table looks like" feature steps from Goutte:

      | Title            | Author       | Tags   | Date                |
      | Another Doc      | Another User |        | Published2017/01/04 |
      | Multilingual Doc | Another User |        | Published2017/01/03 |
      | English Doc      | Admin        |        | Published2017/01/02 |

The same post list from Selenium 2 gives me an error, "Unable to parse post list table. Found: Array". That array's contents in table node format would be:

      |  |
      | Another User |        | Published2017/01/04 |
      | Another User |        | Published2017/01/03 |
      | Admin        |        | Published2017/01/02 |

Note the missing header row labels and missing title column. I've verified that the WP admin ui is working and shows the expected header row and title column by visiting with a full browser.

Documentation

A TODO list for documentation.

  • Installation / setting up
    • Stepping up on a vvv instance
    • Stepping up on a Jenkins server
  • Using the helper classes
    • WordPressPostTrait
    • WordPressTermTrait
    • WordPressUserTrait
    • Spin
    • WordPressInboxFactoryAwareContext (making assertions about e-mails)

assertPostTypeTerms comparing stale cached terms

I have a scenario that tests bulk editing of a custom post type and custom taxonomy. My first bulk edit works, and this step passes:

Then the document "Multilingual Doc" should have document_tag terms "connections"

But bulk editing the same document again, the expected result is compared against the same $actual_terms as the first time, even though an additional term has been added to the post (verified through screenshot and manually visiting the site), leading to a failed test.

Trouble using with MySQL sitting at remote location

Current configuration setup seems to force mysqli connections to connect always to localhost. This creates issues in setups where the database not running on localhost.

Example: Gitlab CI and running MySQL as a Docker service container (which results MySQL being available at hostname mysql, not localhost).

Errors that appear are similar to

PHP Warning:  mysqli::__construct(): (HY000/2002): No such file or directory in 
    /path/to/project/vendor/stephenharris/wordpress-behat-extension/src/Context/Initializer/WordPressContextInitializer.php
    on line 170

Could the behat.yml connection config be expanded to include an option to change the MySQL hostname/IP?

"Menu item could not be found"

This sometimes occurs when running the Given I go to menu item "Users" step. (See: https://travis-ci.org/stephenharris/WordPressBehatExtension/jobs/167079131)

Running locally I can see the menu is collapsed into its icon view so this is the probable cause (and so is a result of the browser's viewport):

user-13

That could explain why it works on Jenkins but not Travis or locally.

The step definition needs to be defined to cope with the collapsed view, and (potentially?) the mobile view.

Overriding Page Object extension's config

We are forcibly setting the namespace of the Page Object extension so that it maps the page name to the appropriate class.

See: https://github.com/stephenharris/WordPressBehatExtension/blob/develop/src/ServiceContainer/WordPressBehatExtension.php#L45-L48

I think this override user's settings. We should, ideally, supplement any user configuration with our own namespaces so that the user's namespaces take precedence, but that our classes are always findable if needed.

Default user(s) and password(s)

Howdy!

A short "improvement" related to the WordPressContext::installWordPress -> TableNode.

Current

Currently the action I have a vanilla wordpress installation will create by default an user admin with password test.

Suggestion

New default: Adding for each role an user

For easier usage and writing tests to different user role's, it would make sense to enhance the existing action, create as default for each role an user in WordPress.

New default: same password as username

Secondly it would make sense to set the password to the username by default. e.G.:

user: admin | pass: admin
...
user: subscriber | pass: subscriber

The current password "test" is an easy to remember password, but it would be much easier to write features when it is the same.

Password optional for default users

Last but not least: the WordPressLoginContext::login could have an improvement by adding the password as "optional" and set it to the given user. This would save some typing and we can write with a default configuration following:

And I am logged in as "admin"
...
And I am logged in as "subscriber"

Please let me know if i can create a pull for this. :-)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.