This repository has been archived by the owner on Feb 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove promise from shopping-cart-widget-component.js and add async/a…
…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
Showing
1 changed file
with
20 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |