Proof of concept k6 test for checkout with config.

This commit is contained in:
Tam Mullen 2021-03-19 16:40:04 +00:00
parent 4b99a24af2
commit cd8faf2716
3 changed files with 394 additions and 0 deletions

View File

@ -0,0 +1,41 @@
#!/bin/bash
echo "Initializing WooCommerce E2E"
wp plugin install woocommerce --activate
wp user create customer customer@woocommercecoree2etestsuite.com --user_pass=password --role=subscriber --path=/var/www/html
# we cannot create API keys for the API, so we using basic auth, this plugin allows that.
wp plugin install https://github.com/WP-API/Basic-Auth/archive/master.zip --activate
# update permalinks to `pretty` to make it easier for testing APIs with k6
wp option update permalink_structure '/%postname%'
# install the WP Mail Logging plugin to test emails
wp plugin install wp-mail-logging --activate
# Installing and activating the WordPress Importer plugin to import sample products"
wp plugin install wordpress-importer --activate
# Adding basic WooCommerce settings"
wp option set woocommerce_store_address "Example Address Line 1"
wp option set woocommerce_store_address_2 "Example Address Line 2"
wp option set woocommerce_store_city "Example City"
wp option set woocommerce_default_country "US:CA"
wp option set woocommerce_store_postcode "94110"
wp option set woocommerce_currency "USD"
wp option set woocommerce_product_type "both"
wp option set woocommerce_allow_tracking "no"
wp option set --format=json woocommerce_cod_settings '{"enabled":"yes"}'
# WooCommerce shop pages
wp wc --user=admin tool run install_pages
# Importing WooCommerce sample products"
wp import wp-content/plugins/woocommerce/sample-data/sample_products.xml --authors=skip
# install Storefront
wp theme install storefront --activate
echo "Success! Your E2E Test Environment is now ready."

View File

@ -0,0 +1,20 @@
export const base_url = __ENV.URL || 'http://localhost:8084';
export const base_host = __ENV.HOST || 'localhost:8084';
export const addresses_customer_billing_first_name = 'John';
export const addresses_customer_billing_last_name = 'Doe';
export const addresses_customer_billing_company = 'Automattic';
export const addresses_customer_billing_country = 'US';
export const addresses_customer_billing_address_1 = 'addr 1';
export const addresses_customer_billing_address_2 = 'addr 2';
export const addresses_customer_billing_city = 'San Francisco';
export const addresses_customer_billing_state = 'CA';
export const addresses_customer_billing_postcode = '94107';
export const addresses_customer_billing_phone = '123456789';
export const addresses_customer_billing_email = 'john.doe@example.com';
export const payment_method = 'cod';
export const product_sku = 'woo-beanie';
export const product_url = 'beanie';
export const product_id = '13';

View File

@ -0,0 +1,333 @@
import { sleep, check, group } from "k6";
import http from "k6/http";
import { Trend } from 'k6/metrics';
import {
base_url,
base_host,
addresses_customer_billing_first_name,
addresses_customer_billing_last_name,
addresses_customer_billing_company,
addresses_customer_billing_country,
addresses_customer_billing_address_1,
addresses_customer_billing_address_2,
addresses_customer_billing_city,
addresses_customer_billing_state,
addresses_customer_billing_postcode,
addresses_customer_billing_phone,
addresses_customer_billing_email,
payment_method,
product_sku,
product_url,
product_id
} from '../../config.js';
/* add custom metrics for each step to the standard output */
let homePageTrend = new Trend('_step_01_home_page_duration');
let shopPageTrend = new Trend('_step_02_shop_page_duration');
let productPageTrend = new Trend('_step_03_product_page_duration');
let addToCartTrend = new Trend('_step_04_add_to_cart_duration');
let viewCartTrend = new Trend('_step_05_view_cart_duration');
let proceedCheckoutTrend = new Trend('_step_06_proceed_checkout_duration');
let placeOrderTrend1 = new Trend('_step_07_place_order_update_order_review_duration');
let placeOrderTrend2 = new Trend('_step_08_place_order_checkout_duration');
let orderReceivedTrend1 = new Trend('_step_09_order_received_duration');
let orderReceivedTrend2 = new Trend('_step_10_order_received_duration');
export default function main() {
let response;
const vars = {};
group("Home Page", function () {
response = http.get(`${base_url}/`,
{
headers: {
accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*//*;q=0.8,application/signed-exchange;v=b3;q=0.9",
//"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9",
connection: "keep-alive",
host: `${base_host}`,
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "same-origin",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"sec-ch-ua":
'"Chromium";v="88", "Google Chrome";v="88", ";Not A Brand";v="99"',
"sec-ch-ua-mobile": "?0",
},
});
homePageTrend.add(response.timings.duration);
});
group("Shop Page", function () {
response = http.get(`${base_url}/shop`, {
headers: {
accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*//*;q=0.8,application/signed-exchange;v=b3;q=0.9",
//"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9",
connection: "keep-alive",
host: `${base_host}`,
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "same-origin",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"sec-ch-ua":
'"Chromium";v="88", "Google Chrome";v="88", ";Not A Brand";v="99"',
"sec-ch-ua-mobile": "?0",
},
});
shopPageTrend.add(response.timings.duration);
});
group("Product Page", function () {
response = http.get(`${base_url}/product/${product_url}`, {
headers: {
accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*//*;q=0.8,application/signed-exchange;v=b3;q=0.9",
//"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9",
connection: "keep-alive",
host: `${base_host}`,
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "same-origin",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"sec-ch-ua":
'"Chromium";v="88", "Google Chrome";v="88", ";Not A Brand";v="99"',
"sec-ch-ua-mobile": "?0",
},
});
productPageTrend.add(response.timings.duration);
});
group("Product Page Add to cart", function () {
response = http.post(
`${base_url}/?wc-ajax=add_to_cart`,
{
product_sku: `${product_sku}`,
product_id: `${product_id}`,
quantity: "1",
},
{
headers: {
accept: "application/json, text/javascript, */*; q=0.01",
//"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9",
connection: "keep-alive",
"content-type":
"application/x-www-form-urlencoded;type=content-type;mimeType=application/x-www-form-urlencoded",
host: `${base_host}`,
origin: `${base_url}`,
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-requested-with": "XMLHttpRequest",
"sec-ch-ua":
'"Chromium";v="88", "Google Chrome";v="88", ";Not A Brand";v="99"',
"sec-ch-ua-mobile": "?0",
},
}
);
addToCartTrend.add(response.timings.duration);
});
group("View Cart", function () {
response = http.get(`${base_url}/cart`, {
headers: {
accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*//*;q=0.8,application/signed-exchange;v=b3;q=0.9",
//"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9",
connection: "keep-alive",
host: `${base_host}`,
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "same-origin",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"sec-ch-ua":
'"Chromium";v="88", "Google Chrome";v="88", ";Not A Brand";v="99"',
"sec-ch-ua-mobile": "?0",
},
});
viewCartTrend.add(response.timings.duration);
check(response, {
"body does not contain 'your cart is currently empty'": response =>
!response.body.includes("Your cart is currently empty."),
});
});
group("Proceed to checkout", function () {
response = http.get(`${base_url}/checkout`, {
headers: {
accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*//*;q=0.8,application/signed-exchange;v=b3;q=0.9",
//"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9",
connection: "keep-alive",
host: `${base_host}`,
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "same-origin",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"sec-ch-ua":
'"Chromium";v="88", "Google Chrome";v="88", ";Not A Brand";v="99"',
"sec-ch-ua-mobile": "?0",
},
});
proceedCheckoutTrend.add(response.timings.duration);
});
vars["woocommerce-process-checkout-nonce"] = response
.html()
.find("input[name=woocommerce-process-checkout-nonce]")
.first()
.attr("value");
group("Place Order", function () {
response = http.post(
`${base_url}/?wc-ajax=update_order_review`,
{
security: "ae0d006f28", //TODO: Correlate this value from update_order_review_nonce on ${base_url}/checkout. Test works with it hard coded for now.
payment_method: `${payment_method}`,
country: `${addresses_customer_billing_country}`,
state: `${addresses_customer_billing_state}`,
postcode: `${addresses_customer_billing_postcode}`,
city: `${addresses_customer_billing_city}`,
address: `${addresses_customer_billing_address_1}`,
address_2: `${addresses_customer_billing_address_2}`,
s_country: `${addresses_customer_billing_country}`,
s_state: `${addresses_customer_billing_state}`,
s_postcode: `${addresses_customer_billing_postcode}`,
s_city: `${addresses_customer_billing_city}`,
s_address: `${addresses_customer_billing_address_1}`,
s_address_2: `${addresses_customer_billing_address_2}`,
has_full_address: "true",
//post_data: //TODO: Test works without post_data, need to confirm if should be included for more realistic results.
},
{
headers: {
accept: "*//*",
//"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9",
connection: "keep-alive",
"content-type":
"application/x-www-form-urlencoded;type=content-type;mimeType=application/x-www-form-urlencoded",
host: `${base_host}`,
origin: `${base_url}`,
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-requested-with": "XMLHttpRequest",
"sec-ch-ua":
'"Chromium";v="88", "Google Chrome";v="88", ";Not A Brand";v="99"',
"sec-ch-ua-mobile": "?0",
},
}
);
placeOrderTrend1.add(response.timings.duration);
response = http.post(
`${base_url}/?wc-ajax=checkout`,
{
billing_first_name: `${addresses_customer_billing_first_name}`,
billing_last_name: `${addresses_customer_billing_last_name}`,
billing_company: `${addresses_customer_billing_company}`,
billing_country: `${addresses_customer_billing_country}`,
billing_address_1: `${addresses_customer_billing_address_1}`,
billing_address_2: `${addresses_customer_billing_address_2}`,
billing_city: `${addresses_customer_billing_city}`,
billing_state: `${addresses_customer_billing_state}`,
billing_postcode: `${addresses_customer_billing_postcode}`,
billing_phone: `${addresses_customer_billing_phone}`,
billing_email: `${addresses_customer_billing_email}`,
order_comments: "",
payment_method: `${payment_method}`,
"woocommerce-process-checkout-nonce": `${vars["woocommerce-process-checkout-nonce"]}`,
_wp_http_referer: "%2F%3Fwc-ajax%3Dupdate_order_review",
},
{
headers: {
accept: "application/json, text/javascript, *//*; q=0.01",
//"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9",
connection: "keep-alive",
"content-type":
"application/x-www-form-urlencoded;type=content-type;mimeType=application/x-www-form-urlencoded",
host: `${base_host}`,
origin: `${base_url}`,
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-requested-with": "XMLHttpRequest",
"sec-ch-ua":
'"Chromium";v="88", "Google Chrome";v="88", ";Not A Brand";v="99"',
"sec-ch-ua-mobile": "?0",
},
}
);
placeOrderTrend2.add(response.timings.duration);
});
group("Order received", function () {
response = http.get(
`${base_url}/checkout/order-received/`,
{
headers: {
accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*//*;q=0.8,application/signed-exchange;v=b3;q=0.9",
//"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9",
connection: "keep-alive",
host: `${base_host}`,
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "same-origin",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"sec-ch-ua":
'"Chromium";v="88", "Google Chrome";v="88", ";Not A Brand";v="99"',
"sec-ch-ua-mobile": "?0",
},
}
);
check(response, {
"body contains 'Thank you. Your order has been received.'": response =>
response.body.includes("Thank you. Your order has been received."),
});
orderReceivedTrend1.add(response.timings.duration);
response = http.post(
`${base_url}/?wc-ajax=get_refreshed_fragments`,
{
headers: {
accept: "*//*",
// "accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9",
connection: "keep-alive",
"content-type":
"application/x-www-form-urlencoded;type=content-type;mimeType=application/x-www-form-urlencoded",
host: `${base_host}`,
origin: `${base_url}`,
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-requested-with": "XMLHttpRequest",
"sec-ch-ua":
'"Chromium";v="88", "Google Chrome";v="88", ";Not A Brand";v="99"',
"sec-ch-ua-mobile": "?0",
},
}
);
orderReceivedTrend2.add(response.timings.duration);
}
);
sleep(1);
}