Completed
Push — master ( 6d8164...bf7249 )
by Roy
03:26
created

tests/e2e/lib/test-helper.js   A

Complexity

Total Complexity 13
Complexity/F 1.08

Size

Lines of Code 124
Function Count 12

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
nc 1
dl 0
loc 124
rs 10
c 1
b 0
f 0
wmc 13
mnd 1
bc 13
fnc 12
bpm 1.0833
cpm 1.0833
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A test-helper.js ➔ ??? 0 4 1
1
/**
2
 * External dependencies
3
 */
4
import config from 'config';
5
import { until, By } from 'selenium-webdriver';
6
import { WebDriverManager, WebDriverHelper as helper } from 'wp-e2e-webdriver';
7
import { Helper as wcHelper, SingleProductPage, CheckoutOrderReceivedPage } from 'wc-e2e-page-objects';
8
9
/**
10
 * Internal dependencies
11
 */
12
import CheckoutPage from './checkout-page';
13
import CustomerFlow from './customer-flow';
14
import GuestCustomerFlow from './guest-customer-flow';
15
import StoreOwnerFlow from './store-owner-flow';
16
17
let manager;
18
let driver;
19
let currentUser;
20
21
export const startBrowser = () => {
22
	manager = new WebDriverManager( 'chrome', { baseUrl: config.get( 'url' ) } );
23
	driver = manager.getDriver();
24
};
25
26
export const quitBrowser = () => {
27
	helper.clearCookiesAndDeleteLocalStorage( driver );
28
	manager.quitBrowser();
29
};
30
31
export const asStoreOwner = () => {
32
	helper.clearCookiesAndDeleteLocalStorage( driver );
33
34
	currentUser = new StoreOwnerFlow( driver, {
35
		baseUrl: config.get( 'url' ),
36
		username: config.get( 'users.shopmanager.username' ),
37
		password: config.get( 'users.shopmanager.password' )
38
	} );
39
	return currentUser;
40
};
41
42
export const asCustomer = () => {
43
	helper.clearCookiesAndDeleteLocalStorage( driver );
44
45
	currentUser = new CustomerFlow( driver, {
46
		baseUrl: config.get( 'url' ),
47
		username: config.get( 'users.customer.username' ),
48
		password: config.get( 'users.customer.password' )
49
	} );
50
	return currentUser;
51
};
52
53
export const asGuestCustomer = () => {
54
	helper.clearCookiesAndDeleteLocalStorage( driver );
55
56
	currentUser = new GuestCustomerFlow( driver, { baseUrl: config.get( 'url' ) } );
57
	return currentUser;
58
};
59
60
export const setStripeSettings = setting => {
61
	const storeOwner = asStoreOwner();
62
	storeOwner.setStripeSettings( setting );
63
	storeOwner.logout();
64
};
65
66
export const openOnePaymentProduct = () => {
67
	return new SingleProductPage( driver, {
68
		url: manager.getPageUrl( config.get( 'products.onePayment' ) )
69
	} );
70
};
71
72
export const payWithStripe = () => {
73
	const checkout = new CheckoutPage( driver, {
74
		url: manager.getPageUrl( '/checkout' )
75
	} );
76
77
	helper.setWhenSettable( driver, By.css( '#billing_first_name' ), 'John' );
78
	helper.setWhenSettable( driver, By.css( '#billing_last_name' ), 'Doe' );
79
	helper.selectOption( driver, By.css( '#billing_country' ), 'United States' );
80
	helper.setWhenSettable( driver, By.css( '#billing_address_1' ), '1234 Test St.' );
81
	helper.setWhenSettable( driver, By.css( '#billing_address_2' ), '#020' );
82
	helper.setWhenSettable( driver, By.css( '#billing_city' ), 'Los Angeles' );
83
	helper.selectOption( driver, By.css( '#billing_state' ), 'California' );
84
	helper.setWhenSettable( driver, By.css( '#billing_postcode' ), '90066' );
85
	helper.setWhenSettable( driver, By.css( '#billing_phone' ), '8008008000' );
86
	helper.setWhenSettable( driver, By.css( '#billing_email' ), '[email protected]' );
87
	checkout.selectPaymentMethod( 'Credit card (Stripe)' );
88
89
	var iframeElement = driver.findElement( By.name( '__privateStripeFrame4' ) );
90
	driver.switchTo().frame( iframeElement );
91
	driver.findElement( By.name( 'cardnumber' ) ).sendKeys( '4242424242424242' );
92
	driver.findElement( By.name( 'exp-date' ) ).sendKeys( '1220' );
93
	driver.findElement( By.name( 'cvc' ) ).sendKeys( '222' );
94
95
	driver.switchTo().defaultContent();
96
97
	checkout.placeOrder();
98
99
	wcHelper.waitTillUIBlockNotPresent( driver, 20000 );
100
};
101
102
export const redirectedTo = ( urlSubstr, timeout = 10000, msg = '' ) => {
103
	if ( ! msg ) {
104
		msg = `waiting to be redirected to URL that contains "${ urlSubstr }"`;
105
	}
106
	return driver.wait( until.urlContains( urlSubstr ), timeout, msg );
107
};
108
109
export const getAttribute = ( selector, attr ) => {
110
	return driver.findElement( selector ).getAttribute( attr );
111
};
112
113
export const checkoutHasText = text => {
114
	const checkout = new CheckoutPage( driver, {
115
		visit: false
116
	} );
117
118
	return checkout.hasText( text );
119
};
120
121
export const orderReceivedHasText = text => {
122
	const orderReceived = new CheckoutOrderReceivedPage( driver, {
123
		visit: false
124
	} );
125
126
	return orderReceived.hasText( text );
127
};
128