Skip to content
This repository has been archived by the owner on Feb 18, 2021. It is now read-only.

Commit

Permalink
Remove promise from shopping-cart-widget-component.js and add async/a…
Browse files Browse the repository at this point in the history
…wait (#1153)

* remove promise from shopping-cart-widget-component.js

* now add async/await to shopping-cart-widget-component.js

* add requested changes to shopping-cart-widget-component.js
  • Loading branch information
Stojdza authored Apr 23, 2018
1 parent ed718f8 commit 605e10d
Showing 1 changed file with 20 additions and 84 deletions.
104 changes: 20 additions & 84 deletions lib/components/shopping-cart-widget-component.js
Original file line number Diff line number Diff line change
@@ -1,105 +1,41 @@
/** @format */

import webdriver, { By as by } from 'selenium-webdriver';
import { By as by } from 'selenium-webdriver';
import * as driverHelper from '../driver-helper.js';

import BaseContainer from '../base-container.js';

export default class ShoppingCartWidgetComponent extends BaseContainer {
constructor( driver ) {
super( driver, by.className( 'cart-toggle-button' ) );
super( driver, by.css( '.cart-toggle-button' ) );
}

open() {
let d = webdriver.promise.defer();
const driver = this.driver;
const explicitWait = this.explicitWait;

this.displayedOpen().then(
function cartAlreadyOpen() {
d.fulfill( true );
},
function cartClosed() {
driverHelper
.clickWhenClickable( driver, by.css( '.cart-toggle-button' ), explicitWait )
.then(
function success() {
d.fulfill( true );
},
function failure() {
d.reject( 'Unable to find Shopping Cart Widget button' );
}
);
}
);

return d.promise;
}

displayedOpen() {
let d = webdriver.promise.defer();

driverHelper.isElementPresent( this.driver, by.css( 'cart-body' ) ).then(
function( present ) {
if ( present ) {
d.fulfill( true );
} else {
d.reject( 'Cart not open' );
}
},
function() {
d.reject( 'Cart not open' );
}
async open() {
return await driverHelper.clickWhenClickable(
this.driver,
by.css( '.cart-toggle-button' ),
this.explicitWaitMS
);

return d.promise;
}

removeItem( self ) {
let d = webdriver.promise.defer();

driverHelper
.isElementPresent( this.driver, by.css( '.cart-empty' ) )
.then( function( cartEmpty ) {
if ( ! cartEmpty ) {
driverHelper.clickWhenClickable( self.driver, by.css( '.cart__remove-item' ) );
}
} );

return d.promise;
async removeItem( self ) {
let cartEmpty = await driverHelper.isElementPresent( this.driver, by.css( '.cart-empty' ) );
if ( ! cartEmpty ) {
return await driverHelper.clickWhenClickable( self.driver, by.css( '.cart__remove-item' ) );
}
}

empty() {
let d = webdriver.promise.defer();
async empty() {
let self = this;
const cartBadgeSelector = by.css( '.cart__count-badge' );

driverHelper.isElementPresent( this.driver, cartBadgeSelector ).then( function( present ) {
if ( present ) {
self.open().then( function() {
self.driver
.findElement( cartBadgeSelector )
.getText()
.then( function( numItems ) {
let flow = self.driver.controlFlow();
let promiseArray = [];
for ( let i = 0; i < numItems; i++ ) {
promiseArray.push(
flow.execute( function() {
self.removeItem( self );
} )
);
}
webdriver.promise.all( promiseArray ).then( function() {
d.fulfill( true );
} );
} );
} );
} else {
d.fulfill( true );
let present = await driverHelper.isElementPresent( self.driver, cartBadgeSelector );
if ( present ) {
await self.open();
let numItems = await self.driver.findElement( cartBadgeSelector ).getText();
for ( let i = 0; i < numItems; i++ ) {
await self.removeItem( self );
}
} );

return d.promise;
}
}
}

0 comments on commit 605e10d

Please sign in to comment.