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

Npm package #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 80 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
# WP Playwright
An e2e testing tool with playwright within WordPress to swap your environment easily.
Introduction
------------
WP Playwright is an End-To-End (E2E) testing tool that leverages the power of [Playwright](https://github.com/microsoft/playwright) to make it easier to test WordPress applications. With WP Playwright, you can quickly swap between different testing environments, allowing you to test your application in multiple configurations with ease.

## Prerequisites:

Must have `WP CLI` [installed](https://wp-cli.org/).

## Installation process:
Features
--------
- Easy setup: WP Playwright is designed to be easy to set up and start using.
- Multiple environment support: With WP Playwright, you can quickly switch between development and testing environments, making it easier to test your application in different configurations.
- Robust testing: Playwright is a robust testing tool that provides a variety of testing capabilities, including support for headless and non-headless browsers, automatic waiting for elements to load, and more.

## ⚠️️ Attention ⚠️
Installation
------------
To install WP Playwright, simply run the following command in your terminal:

Currently don't install, download this from [github](https://github.com/RatulHasan/play-wordpress-wright), then copy and paste the `swapEnvironment.js` and `swapBackEnvironment.js` in your plugin root directory.
`npm install wp-playwright --save-dev`

Then, Copy your `wp-config.php` file and place it in your plugin root directory like `akismet/` and rename it with `playwright-wp-config.php`.

Add these line in `playwright-wp-config.php`
Usage
-----
To get started with WP Playwright, simply import the library into your testing script and configure it to use the environment you want to test against.

Here's a basic example of how you might use WP Playwright in your projects:

Copy your `wp-config.php` file and place it in your plugin root directory like `akismet/` and rename it with `playwright-wp-config.php`.

Add these line in `playwright-wp-config.php` if already not exists.
```
define( 'DB_NAME', 'change_it_to_test_db_name' );
define( 'DB_USER', 'root' );
Expand All @@ -26,29 +41,73 @@ define( 'ADMIN_USERNAME', 'UserName' ); // This is not database username. This i
define( 'ADMIN_PASSWORD', 'Password' ); // This is not database password. This is login password.
define( 'ADMIN_EMAIL', '[email protected]' );
```
move `swapEnvironment.js` and `swapBackEnvironment.js` files to your plugins root folder.

Add these lines in `playwright.config.js` file within `config` section.
Add these lines in `playwright.config.js` file within the `config` section.
```
globalSetup: require.resolve( './global-setup' ),
globalTeardown: require.resolve( './global-teardown' ),
```

Now create `global-setup.js` and `global-teardown.js` in your plugin root directory.

Then, write like this example in `global-setup.js`,
```
import { join } from 'path'
const { swapEnvironment } = require('wp-playwright/swapEnvironment');

const wpConfigPath = join(__dirname, '..', '..', '..', 'wp-config.php'); // your wp-congig.php path
const wpTestsConfigPath = join(__dirname, 'playwright-wp-config.php'); // your wp-congig.php path

async function globalSetup() {
await swapEnvironment(wpConfigPath, wpTestsConfigPath);

// ..... add rest of your globalSetup code here

}

export default globalSetup;
```
globalSetup: require.resolve( './swapEnvironment' ),
globalTeardown: require.resolve( './swapBackEnvironment' ),
Then, write like this example in `global-teardown.js`,
```
### If you don't want to swap your environment and jus want to refresh your DB only-
import { join } from 'path'
const { swapBackEnvironment } = require('wp-playwright/swapBackEnvironment');

const wpConfigPath = join(__dirname, '..', '..', '..', 'wp-config.php');
const wpTestsConfigPath = join(__dirname, 'playwright-wp-config.php');

async function globalTeardown() {
await swapBackEnvironment(wpConfigPath, wpTestsConfigPath);

// ..... add rest of your globalTeardown code here

}

export default globalTeardown;
```

This `swapEnvironment.js` file offers you some methods like-

- `swapEnvironment(wpConfigPath, wpTestsConfigPath)` - This will swap your environment.
- `createDatabaseIfNotExists()` - This will create your test database.
- `dropDatabaseTables()` - This will reset your test database.
- `installWP()` - This will refresh your WordPress installation.
- `installWP(site_url, title, username, email, password)` - This will refresh your WordPress installation.
- `renameFile(src, dest)` - This will rename file.
- `readWpConfigFile()` - This will read wp-config file and gives you defined values.
- `readWpConfigFile(wpConfigPath)` - This will read wp-config file and gives you defined values.

#### Project structure
```
plugin_folder
|-----playwright.config.js
|-----playwright-wp-config.php
|-----swapEnvironment.js
|-----swapBackEnvironment.js
```
Documentation
-------------

For more information on how to use WP Playwright, including detailed instructions on how to configure different testing environments, check out the [official documentation](https://github.com/ratulhasan/wp-playwright).

Contributing
------------

If you're interested in contributing to the development of WP Playwright, we welcome your contributions! Simply fork the repository on [Github](https://github.com/RatulHasan/play-wordpress-wright), make your changes, and submit a pull request.

License
-------

WP Playwright is open-source software licensed under the [MIT License](https://opensource.org/licenses/MIT).

_**If this package helps you anyway**_, please give me a **Star** ⭐ on [Github](https://github.com/RatulHasan/play-wordpress-wright).

Expand Down
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "wp-playwright",
"version": "1.0.0",
"description": "An e2e testing tool with playwright within WordPress to swap your environment easily.",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/RatulHasan/wp-playwright.git"
},
"keywords": [
"playwright",
"wordpress",
"e2e",
"wp-playwright"
],
"author": "Ratul Hasan",
"license": "MIT",
"bugs": {
"url": "https://github.com/RatulHasan/wp-playwright/issues"
},
"homepage": "https://github.com/RatulHasan/wp-playwright#readme"
}
5 changes: 3 additions & 2 deletions swapBackEnvironment.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import fs from 'fs';

import { dropDatabaseTables, renameFile, wpConfigBackupPath, wpConfigPath, wpTestsConfigPath } from './swapEnvironment'
import { dropDatabaseTables, renameFile } from './swapEnvironment'

async function swapBackEnvironment() {
export async function swapBackEnvironment(wpConfigPath, wpTestsConfigPath) {
try {
if (fs.existsSync(wpConfigPath)) {
await dropDatabaseTables();
const wpConfigBackupPath = `${wpConfigPath}.backup`;
renameFile(wpConfigPath, wpTestsConfigPath)
renameFile(wpConfigBackupPath, wpConfigPath)
}
Expand Down
15 changes: 6 additions & 9 deletions swapEnvironment.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { exec, execSync } from 'child_process'
import { join } from 'path'
import fs from 'fs'

export const wpConfigPath = join(__dirname, '..', '..', '..', 'wp-config.php')
export const wpConfigBackupPath = join(__dirname, '..', '..', '..', 'wp-config-backup.php')
export const wpTestsConfigPath = join(__dirname, 'playwright-wp-config.php')
import swapBackEnvironment from './swapBackEnvironment'


// Read test config file and store defined data in config variable.
export const readWpConfigFile = () => {
export const readWpConfigFile = (wpConfigPath) => {
if (!fs.existsSync(wpConfigPath)) {
return null;
}
Expand Down Expand Up @@ -87,19 +83,20 @@ export const installWP = async (site_url, title, username, email, password) => {
});
}

async function swapEnvironment() {
export const swapEnvironment = async ( wpConfigPath, wpTestsConfigPath ) =>{
try {
if (fs.existsSync(wpTestsConfigPath)) {
const wpConfigBackupPath = `${wpConfigPath}.backup`;
renameFile(wpConfigPath, wpConfigBackupPath)
renameFile(wpTestsConfigPath, wpConfigPath)

// get config data.
const config = readWpConfigFile();
const config = readWpConfigFile(wpConfigPath);

// If there is an error to get data, throw an error.
if (!config) {
console.error(`Error reading: ${wpConfigPath}`);
await swapEnvironment();
await swapBackEnvironment();
return;
}

Expand Down