0.2.0 • Published 2 years ago

@woocommerce/e2e-utils v0.2.0

Weekly downloads
1
License
GPL-3.0+
Repository
github
Last release
2 years ago

WooCommerce End to End Test Utilities

This package contains utilities to simplify writing e2e tests specific to WooCommmerce.

Installation

npm install @woocommerce/e2e-utils --save

Usage

Example:

import {
	shopper,
	merchant,
	createSimpleProduct
} from '@woocommerce/e2e-utils';

describe( 'Cart page', () => {
	beforeAll( async () => {
		await createSimpleProduct();
	} );

	it( 'should display no item in the cart', async () => {
		await shopper.goToCart();
		await expect( page ).toMatchElement( '.cart-empty', { text: 'Your cart is currently empty.' } );
	} );
} );

Retries

This package provides support for enabling retries in tests:

  • In the test environment set E2E_RETEST=1.
  • To add conditional logic to your tests use the boolean constant IS_RETEST_MODE.

Available constants

Dashboard

  • WP_ADMIN_LOGIN - WordPress login
  • WP_ADMIN_DASHBOARD - WordPress dashboard
  • WP_ADMIN_WP_UPDATES - WordPress updates
  • WP_ADMIN_PLUGINS - Plugin list
  • WP_ADMIN_PERMALINK_SETTINGS - Permalink settings
  • WP_ADMIN_ALL_USERS_VIEW - WordPress user list
  • WP_ADMIN_POST_TYPE - Post listing
  • WP_ADMIN_NEW_POST_TYPE - New post
  • WP_ADMIN_ALL_COUPONS_VIEW - Coupons list
  • WP_ADMIN_NEW_COUPON - New coupon
  • WP_ADMIN_ALL_ORDERS_VIEW - Orders list
  • WP_ADMIN_NEW_ORDER - New Order
  • WP_ADMIN_ALL_PRODUCTS_VIEW - Products list
  • WP_ADMIN_NEW_PRODUCT - New product
  • WP_ADMIN_IMPORT_PRODUCTS - Import products
  • WP_ADMIN_PLUGIN_PAGE - Plugin settings page root
  • WP_ADMIN_WC_HOME - WooCommerce home screen
  • WP_ADMIN_SETUP_WIZARD - WooCommerce setup/onboarding wizard
  • WP_ADMIN_ANALYTICS_PAGES - WooCommerce analytics page root
  • WP_ADMIN_WC_SETTINGS - WooCommerce settings page root
  • WP_ADMIN_NEW_SHIPPING_ZONE - WooCommerce new shipping zone
  • WP_ADMIN_WC_EXTENSIONS - WooCommerce extensions page
  • WP_ADMIN_PLUGIN_INSTALL - WordPress plugin install page

Front end

  • SHOP_PAGE - Shop page
  • SHOP_PRODUCT_PAGE - Single product page
  • SHOP_CART_PAGE - Cart page
  • SHOP_CHECKOUT_PAGE - Checkout page
  • SHOP_MY_ACCOUNT_PAGE - Customer account page
  • MY_ACCOUNT_ORDERS - Customer orders
  • MY_ACCOUNT_DOWNLOADS - Customer downloads
  • MY_ACCOUNT_ADDRESSES - Customer addresses
  • MY_ACCOUNT_ACCOUNT_DETAILS - Customer account details

Test Functions

Merchant merchant

FunctionParametersDescription
collapseAdminMenucollapseCollapse or expand the WP admin menu
dismissOnboardingWizardDismiss the onboarding wizard if present
goToOrderorderIdGo to view a single order
goToProductproductIdGo to view a single product
loginLog in as merchant
logoutLog out of merchant account
openAllOrdersViewGo to the orders listing
openAllProductsViewGo to the products listing
openDashboardGo to the WordPress dashboard
openNewCouponGo to the new coupon editor
openNewOrderGo to the new order editor
openNewProductGo to the new product editor
openPermalinkSettingsGo to Settings -> Permalinks
openPluginsGo to the Plugins screen
openSettingsGo to WooCommerce -> Settings
runSetupWizardOpen the onboarding profiler
updateOrderStatusorderId, statusUpdate the status of an order
openEmailLogOpen the WP Mail Log page
openAnalyticsPageOpen any Analytics page
openAllUsersViewOpen the All Users page
openImportProductsOpen the Import Products page
openExtensionsGo to WooCommerce -> Extensions
openWordPressUpdatesPageGo to Dashboard -> Updates
installAllUpdatesInstall all pending updates on Dashboard -> Updates
updateWordPressInstall pending WordPress updates on Dashboard -> Updates
updatePluginsInstall all pending plugin updates on Dashboard -> Updates
updateThemesInstall all pending theme updates on Dashboard -> Updates
runDatabaseUpdateRuns the database update if needed

Shopper shopper

FunctionParametersDescription
addToCartAdd an item to the cart from a single product page
addToCartFromShopPageproductIdOrTitleAdd an item to the cart from the shop page
fillBillingDetailscustomerBillingDetailsFill billing fields in checkout form using configured address
fillShippingDetailscustomerShippingDetailsFill shipping fields in checkout form using configured address
goToAddressesGo to My Account -> Address Details
goToAccountDetailsGo to My Account -> Details
goToCartGo to the cart page
goToCheckoutGo to the checkout page
goToDownloadsGo to My Account -> Downloads
goToMyAccountGo to the My Account page
goToOrdersGo to My Account -> Orders
goToProductproductIdGo to a single product in the shop
goToShopGo to the shop page
loginLog in as the shopper
logoutLog out of the shopper account
placeOrderPlace an order from the checkout page
productIsInCheckoutproductTitle, quantity, total, cartSubtotalVerify product is in cart on checkout page
removeFromCartproductIdOrTitleRemove a product from the cart on the cart page
setCartQuantityproductTitle, quantityValueChange the quantity of a product on the cart page
searchForProductSearching for a product name and landing on its detail page
emptyCartRemoves any products and coupons that are in the cart

REST API withRestApi

Please note: if you're using a non-SSL environment (such as a Docker container from wc-e2e) you will need to use Basic Auth in order to authenticate with the API and use the withRestApi methods listed below. To do so, you will need to install the the Basic Auth plugin. One way this can be accomplished is by adding wp plugin install https://github.com/WP-API/Basic-Auth/archive/master.zip --activate to your initialize.sh script.

FunctionParametersDescription
addShippingZoneAndMethodzoneName, zoneLocation, zipCode, zoneMethod, cost, additionalZoneMethods, testResponseAdds a shipping zone along with a shipping method
batchCreateOrdersorders, testResponseCreate a batch of orders using the "Batch Create Order" API endpoint
addTaxClassestaxClassesAdd an array of tax classes if they do not exist
addTaxRatestaxRatesAdd an array of tax rates if they do not exist
createProductCategorycategoryNameCreate a product category with the provided name
deleteAllCouponsPermanently delete all coupons
deleteAllOrdersPermanently delete all orders
deleteAllProductAttributestestResponsePermanently delete all product attributes
deleteAllProductCategoriestestResponsePermanently delete all product categories
deleteAllProductsPermanently delete all products
deleteAllProductTagstestResponsePermanently delete all product tags
deleteAllShippingClassestestResponsePermanently delete all shipping classes
deleteAllShippingZonestestResponsePermanently delete all shipping zones except the default
deleteCouponcouponIdPermanently delete a coupon
deleteCustomerByEmailemailAddressDelete customer user account. Posts are reassigned to user ID 1
getSystemEnvironmentGet the current environment from the WooCommerce system status API.
resetOnboardingReset onboarding settings
resetSettingsGroupToDefaultsettingsGroup, testResponseReset settings in settings group to default except select fields
updateSettingOptionsettingsGroup, settingID, payloadUpdate a settings group
updatePaymentGatewaypaymentGatewayId, payload, testResponseUpdate a payment gateway

Classes

The package includes the following page specific utility class:

AdminEdit

The AdminEdit class is the base classic custom post type post editor class. It contains the following functions:

FunctionParametersDescription
verifyPublishbutton, publishNotice, publishVerificationPublish the post object currently being edited and verify publish status
getIdGet the ID of the post object being edited

General Utilities

There is a general utilities object utils with the following functions:

FunctionParametersDescription
getSlugtextTake a string name and generate the slug for it
describeIfconditionReturn the describe or describe.skip function when the condition is true / false
itconditionReturn the it or it.skip function when the condition is true / false
waitForTimeouttimeoutWait for a timeout in milliseconds

Page Utilities

FunctionParametersDescription
addProductToOrderorderId, productNameadds a product to an order using the product search
applyCouponcouponNamehelper method which applies a coupon in cart or checkout
clearAndFillInputselector, valueReplace the contents of an input with the passed value
clickFilterselectorhelper method that clicks on a list page filter
clickTabtabNameClick on a WooCommerce -> Settings tab
clickUpdateOrdernoticeText, waitForSaveHelper method to click the Update button on the order details page
completeOnboardingWizardcompletes the onboarding wizard with some default settings
createCouponcouponAmount, couponTypecreates a basic coupon. Default amount is 5. Default coupon type is fixed discount. Returns the generated coupon code.
createGroupedProductcreates a grouped product for the grouped product tests. Returns the product id.
createSimpleOrderstatuscreates a basic order with the provided status string
createSimpleProductcreates the simple product configured in default.json. Returns the product id.
createSimpleProductWithCategoryname, price,categoryNamecreates a simple product used passed values. Returns the product id.
createVariableProductcreates a variable product for the variable product tests. Returns the product id.
deleteAllEmailLogsdeletes the emails generated by WP Mail Logging plugin
evalAndClickselectorhelper method that clicks an element inserted in the DOM by a script
moveAllItemsToTrashhelper method that checks every item in a list page and moves them to the trash
permalinkSettingsPageSaveChangesSave the current Permalink settings
removeCouponhelper method that removes a single coupon within cart or checkout
selectOptionInSelect2selector, valuehelper method that searchs for select2 type fields and select plus insert value inside
selectOrderActionactionHelper method to select an order action in the Order Actions postbox
setCheckboxselectorCheck a checkbox
settingsPageSaveChangesSave the current WooCommerce settings page
uiUnblockedWait until the page is unblocked
unsetCheckboxselectorUncheck a checkbox
verifyAndPublishnoticeTextVerify that an item can be published
verifyCheckboxIsSetselectorVerify that a checkbox is checked
verifyCheckboxIsUnsetselectorVerify that a checkbox is unchecked
verifyPublishAndTrashbutton, publishNotice, publishVerification, trashVerificationVerify that an item can be published and trashed
verifyValueOfInputFieldselector, valueVerify an input contains the passed value
clickFilterselectorClick on a list page filter
moveAllItemsToTrashMoves all items in a list view to the Trash
verifyAndPublishnoticeTextVerify that an item can be published
selectOptionInSelect2selector, valuehelper method that searchs for select2 type fields and select plus insert value inside
searchForOrdervalue, orderId, customerNamehelper method that searchs for an order via many different terms
addShippingZoneAndMethodzoneName, zoneLocation, zipCode, zoneMethodutil helper method for adding shipping zones with shipping methods
applyCouponcouponNamehelper method which applies a coupon in cart or checkout
removeCouponhelper method that removes a single coupon within cart or checkout
selectOrderActionactionHelper method to select an order action in the Order Actions postbox
clickUpdateOrdernoticeText, waitForSaveHelper method to click the Update button on the order details page
deleteAllShippingZonesDelete all the existing shipping zones
waitForSelectorWithoutThrowselector, timeoutInSecondsconditionally wait for a selector without throwing an error. Default timeout is 5 seconds
createOrderorderOptionsCreates an order using the API with the passed in details
clickAndWaitForSelectorbuttonSelector, resultSelector, timeoutClick a button and wait for response

Test Utilities

As of version 0.1.3, all test utilities from @wordpress/e2e-test-utils are available through this package.