Skip to content

Commit

Permalink
Tests: Add swap tests (#3845)
Browse files Browse the repository at this point in the history
  • Loading branch information
mike10ca committed Jun 18, 2024
1 parent d94b068 commit 29d3570
Show file tree
Hide file tree
Showing 15 changed files with 404 additions and 3 deletions.
14 changes: 14 additions & 0 deletions cypress/e2e/pages/create_tx.pages.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as constants from '../../support/constants'
import * as main from '../pages/main.page'
import * as wallet from '../pages/create_wallet.pages'

export const delegateCallWarning = '[data-testid="delegate-call-warning"]'
export const policyChangeWarning = '[data-testid="threshold-warning"]'
Expand Down Expand Up @@ -41,6 +42,8 @@ const filterApplyBtn = '[data-testid="apply-btn"]'
const filterClearBtn = '[data-testid="clear-btn"]'
const addressItem = '[data-testid="address-item"]'
const radioSelector = 'div[role="radiogroup"]'
const rejectTxBtn = '[data-testid="reject-btn"]'
const deleteTxModalBtn = '[data-testid="delete-tx-btn"]'

const viewTransactionBtn = 'View transaction'
const transactionDetailsTitle = 'Transaction details'
Expand All @@ -62,13 +65,24 @@ const expandAllBtnStr = 'Expand all'
const collapseAllBtnStr = 'Collapse all'
export const messageNestedStr = `"nestedString": "Test message 3 off-chain"`
const noTxFoundStr = (type) => `0 ${type} transactions found`
const deleteFromQueueStr = 'Delete from the queue'

export const filterTypes = {
incoming: 'Incoming',
outgoing: 'Outgoing',
module: 'Module-based',
}

function clickOnRejectBtn() {
cy.get(rejectTxBtn).click()
}

export function deleteTx() {
clickOnRejectBtn()
cy.get(wallet.choiceBtn).contains(deleteFromQueueStr).click()
cy.get(deleteTxModalBtn).click()
}

export function setTxType(type) {
cy.get(radioSelector).find('label').contains(type).click()
}
Expand Down
2 changes: 1 addition & 1 deletion cypress/e2e/pages/create_wallet.pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const safeActivationSection = '[data-testid="activation-section"]'
const addressAutocompleteOptions = '[data-testid="address-item"]'
export const qrCode = '[data-testid="qr-code"]'
export const addressInfo = '[data-testid="address-info"]'
const choiceBtn = '[data-testid="choice-btn"]'
export const choiceBtn = '[data-testid="choice-btn"]'
const addFundsBtn = '[data-testid="add-funds-btn"]'
const createTxBtn = '[data-testid="create-tx-btn"]'
const qrCodeSwitch = '[data-testid="qr-code-switch"]'
Expand Down
5 changes: 5 additions & 0 deletions cypress/e2e/pages/main.page.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,8 @@ export function verifyTextVisibility(stringsArray) {
export function getIframeBody(iframe) {
return cy.get(iframe).its('0.contentDocument.body').should('not.be.empty').then(cy.wrap)
}

export function connectSigner(signer) {
cy.get('button').contains('Connect').click()
cy.contains('Private key').click()
}
163 changes: 163 additions & 0 deletions cypress/e2e/pages/swaps.pages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import * as constants from '../../support/constants.js'
import * as main from '../pages/main.page.js'

// Incoming from CowSwap
export const inputCurrencyInput = '[id="input-currency-input"]'
export const outputurrencyInput = '[id="output-currency-input"]'
const tokenList = '[id="tokens-list"]'
export const swapBtn = '[id="swap-button"]'
const exceedFeesChkbox = 'input[id="fees-exceed-checkbox"]'
const settingsBtn = 'button[id="open-settings-dialog-button"]'
export const assetsSwapBtn = '[data-testid="swap-btn"]'
export const dashboardSwapBtn = '[data-testid="overview-swap-btn"]'
const confirmSwapStr = 'Confirm Swap'
const swapBtnStr = /Confirm Swap|Swap|Confirm (Approve COW and Swap)|Confirm/
const orderSubmittedStr = 'Order Submitted'

export const swapTokens = {
cow: 'COW',
dai: 'DAI',
eth: 'ETH',
}

const swapOrders = '**/api/v1/orders/*'
const surplus = '**/users/*/total_surplus'
const nativePrice = '**/native_price'
const quote = '**/quote/*'

export function clickOnAssetSwapBtn(index) {
cy.get(assetsSwapBtn).eq(index).as('btn')
cy.get('@btn').click()
}

export function verifyOrderSubmittedConfirmation() {
cy.get('div').contains(orderSubmittedStr).should('exist')
}

export function clickOnSettingsBtn() {
cy.get(settingsBtn).click()
}

export function setExpiry(value) {
cy.get('div').contains('Swap deadline').parent().next().find('input').clear().type(value)
}

export function setSlippage(value) {
cy.contains('button', 'Auto').next('button').find('input').clear().type(value)
}
export function waitForOrdersCallToComplete() {
cy.intercept('GET', swapOrders).as('Orders')
cy.wait('@Orders')
}

export function waitForSurplusCallToComplete() {
cy.intercept('GET', surplus).as('Surplus')
cy.wait('@Surplus')
}

export function waitFornativePriceCallToComplete() {
cy.intercept('GET', nativePrice).as('Price')
cy.wait('@Price')
}

export function waitForQuoteCallToComplete() {
cy.intercept('GET', quote).as('Quote')
cy.wait('@Quote')
}

export function clickOnConfirmSwapBtn() {
cy.get('button').contains(confirmSwapStr).click()
}

export function clickOnExceeFeeChkbox() {
cy.wait(1000)
cy.get(exceedFeesChkbox)
.should(() => {})
.then(($button) => {
if (!$button.length) {
return
}
cy.wrap($button).click()
})
}

export function clickOnSwapBtn() {
cy.get('button').contains(swapBtnStr).as('swapBtn')

cy.get('@swapBtn').should('exist').click()
}

export function checkSwapBtnIsVisible() {
cy.get('button').contains(swapBtnStr).should('be.visible')
}

export const currencyDirectionOptions = {
input: 'input',
output: 'output',
}

export function acceptLegalDisclaimer() {
cy.get('button').contains('Continue').click()
}

export function checkTokenBalance(safe, tokenSymbol) {
cy.get(inputCurrencyInput)
.invoke('text')
.then((text) => {
main.getSafeBalance(safe, constants.networkKeys.sepolia).then((response) => {
const targetToken = response.body.items.find((token) => token.tokenInfo.symbol === tokenSymbol)
const tokenBalance = targetToken.balance.toString()
let formattedBalance

if (tokenBalance.length > 4) {
formattedBalance = `${tokenBalance[0]},${tokenBalance.slice(1, 4)}`
} else {
formattedBalance = tokenBalance
}

expect(text).to.include(`${formattedBalance} ${tokenSymbol}`)
})
})
}

export function verifySelectedInputCurrancy(option) {
cy.get(inputCurrencyInput).within(() => {
cy.get('span').contains(option).should('be.visible')
})
}
export function selectInputCurrency(option) {
cy.get(inputCurrencyInput).within(() => {
cy.get('button').trigger('mouseover').trigger('click')
})
cy.get(tokenList).find('span').contains(option).click()
}

export function selectOutputCurrency(option) {
cy.get(outputurrencyInput).within(() => {
cy.get('button').trigger('mouseover').trigger('click')
})
cy.get(tokenList).find('span').contains(option).click()
}

export function setInputValue(value) {
cy.get(inputCurrencyInput).within(() => {
cy.get('input').type(value)
})
}

export function setOutputValue(value) {
cy.get(outputurrencyInput).within(() => {
cy.get('input').type(value)
})
}

export function isInputGreaterZero(inputSelector) {
return cy
.get(inputSelector)
.find('input')
.invoke('val')
.then((val) => {
const n = parseFloat(val)
return n > 0
})
}
4 changes: 4 additions & 0 deletions cypress/e2e/pages/transactions.page.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export function selectExecuteNow() {
cy.get(executeNowOption).click()
}

export function selectExecuteLater() {
cy.get(executeLaterOption).click()
}

export function selectConnectedWalletOption() {
cy.get(connectedWalletExecutionMethod).click()
}
Expand Down
68 changes: 68 additions & 0 deletions cypress/e2e/regression/swaps.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as constants from '../../support/constants.js'
import * as main from '../pages/main.page.js'
import * as swaps from '../pages/swaps.pages.js'
import * as tx from '../pages/transactions.page.js'
import * as create_tx from '../pages/create_tx.pages.js'
import { getSafes, CATEGORIES } from '../../support/safes/safesHandler.js'
import * as owner from '../pages/owners.pages'

const walletCredentials = JSON.parse(Cypress.env('CYPRESS_WALLET_CREDENTIALS'))
const signer = walletCredentials.OWNER_4_PRIVATE_KEY
let staticSafes = []

let iframeSelector

describe('Swaps tests', () => {
before(async () => {
staticSafes = await getSafes(CATEGORIES.static)
})

beforeEach(() => {
cy.clearLocalStorage()
cy.visit(constants.swapUrl + staticSafes.SEP_STATIC_SAFE_1)
main.acceptCookies()
iframeSelector = `iframe[src*="${constants.swapWidget}"]`
})

// TODO: Waiting for signer connection issue be resolved
it.skip('Verify an order can be created, signed and appear in tx queue', { defaultCommandTimeout: 30000 }, () => {
swaps.acceptLegalDisclaimer()
swaps.waitForOrdersCallToComplete()
cy.wait(2000)
main.getIframeBody(iframeSelector).within(() => {
swaps.clickOnSettingsBtn()
swaps.setSlippage('0.30')
swaps.setExpiry('2')
swaps.clickOnSettingsBtn()
swaps.selectInputCurrency(swaps.swapTokens.cow)
swaps.checkTokenBalance(staticSafes.SEP_STATIC_SAFE_1.substring(4), swaps.swapTokens.cow)
swaps.setInputValue(4)
swaps.selectOutputCurrency(swaps.swapTokens.dai)
swaps.checkSwapBtnIsVisible()
swaps.isInputGreaterZero(swaps.outputurrencyInput).then((isGreaterThanZero) => {
cy.wrap(isGreaterThanZero).should('be.true')
})
swaps.clickOnExceeFeeChkbox()
swaps.clickOnSwapBtn()
swaps.clickOnSwapBtn()
})
create_tx.changeNonce(12)
tx.selectExecuteLater()
cy.wait(1000)

create_tx.clickOnSignTransactionBtn()
main.getIframeBody(iframeSelector).within(() => {
swaps.verifyOrderSubmittedConfirmation()
})
cy.visit(constants.transactionQueueUrl + staticSafes.SEP_STATIC_SAFE_1)
main.verifyElementsCount(create_tx.transactionItem, 1)
create_tx.verifySummaryByName(swapsQueue.contractName, [swapsQueue.action, swapsQueue.oneOfOne])
cy.visit(constants.transactionQueueUrl + staticSafes.SEP_STATIC_SAFE_1)
owner.waitForConnectionStatus()
main.acceptCookies()
// main.connectSigner(signer)
create_tx.clickOnTransactionItem(0)
create_tx.deleteTx()
main.verifyElementsCount(create_tx.transactionItem, 0)
})
})
56 changes: 56 additions & 0 deletions cypress/e2e/regression/swaps_history.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as constants from '../../support/constants.js'
import * as main from '../pages/main.page.js'
import * as create_tx from '../pages/create_tx.pages.js'
import * as swaps_data from '../../fixtures/swaps_data.json'
import { getSafes, CATEGORIES } from '../../support/safes/safesHandler.js'

let staticSafes = []

const swapsHistory = swaps_data.type.history
const limitOrder =
'&id=multisig_0x8f4A19C85b39032A37f7a6dCc65234f966F72551_0x3faf510142c9ade7ac2a701fb697b95f321fd51f5eb9b17e7e534a8abe472b07'
const limitOrderSafe = 'sep:0x8f4A19C85b39032A37f7a6dCc65234f966F72551'

describe('[SMOKE] Swaps history tests', () => {
before(async () => {
staticSafes = await getSafes(CATEGORIES.static)
})

beforeEach(() => {
cy.clearLocalStorage()
cy.visit(constants.transactionsHistoryUrl + staticSafes.SEP_STATIC_SAFE_1)
main.acceptCookies()
})

it('Verify operation names are correct for buying and selling of tokens', { defaultCommandTimeout: 30000 }, () => {
create_tx.clickOnTransactionItemByName('8:05 AM')
create_tx.verifyExpandedDetails([
swapsHistory.buyOrder,
swapsHistory.buy,
swapsHistory.oneGNO,
swapsHistory.forAtMost,
swapsHistory.cow,
swapsHistory.expired,
swapsHistory.actionApprove,
swapsHistory.actionPreSignature,
])
cy.reload()

create_tx.clickOnTransactionItemByName('11:14 AM')
create_tx.verifyExpandedDetails([
swapsHistory.sellOrder,
swapsHistory.sell,
swapsHistory.oneCOW,
swapsHistory.forAtLeast,
swapsHistory.dai,
swapsHistory.filled,
swapsHistory.actionApprove,
swapsHistory.actionPreSignature,
])
})

it('Verify "Partially filled" field is displayed in limit order', () => {
cy.visit(constants.transactionUrl + limitOrderSafe + limitOrder)
create_tx.verifyExpandedDetails([swapsHistory.partiallyFilled])
})
})
Loading

0 comments on commit 29d3570

Please sign in to comment.