简体   繁体   English

赛普拉斯:如何处理在不同视口上运行的移动和桌面上具有不同名称的选择器?

[英]Cypress: How to handle selectors with different names on mobile and desktop running on different viewports?

I am new to Cypress 12.3 and trying to find out the best possible way to run my automation on different viewports.我是 Cypress 12.3 的新手,正在尝试找出在不同视口上运行自动化的最佳方法。 My website in some places uses different css selector on mobile eg on Search of a product, Later on checkout.我的网站在某些地方使用不同的 css 选择器,例如在搜索产品时,稍后在结帐时。

I was writing the code below where depending on the viewport size being mobile or desktop it will execute those steps.我正在编写下面的代码,根据视口大小是移动设备还是桌面,它将执行这些步骤。 However in this process I realised that some steps would be repetitive in both if and else when the css selectors are same on mobile and desktop.然而,在这个过程中,我意识到当 css 选择器在移动设备和桌面设备上相同时,一些步骤在 if 和 else 中都是重复的。

Please can some one assists in what is the best way to write the tests?请有人协助编写测试的最佳方法是什么?

import AllowCookies from "../pagesObjects/components/AllowCookies"
import Header from "../pagesObjects/components/Header"
import PLPPage from "../pagesObjects/pages/PLPPage"
import BasePage from "../pagesObjects/pages/BasePage"

const sizes = [BasePage.setiPhoneViewport(),BasePage.setAndriodViewport(), BasePage.setTabletViewport(), [1280, 768]]

describe('Add random product to bag', () => {
    let testData
    beforeEach(() => {
        cy.fixture('general').then(function (data) {
            testData = data
            cy.visit(data.baseURL + data.urlHomePath)
        })
        AllowCookies.clickOnAllowAll()
    })

     sizes.forEach((size) => {
        it(` Add random products ${size} `, () => {
            cy.currencyPopup()
            if (Cypress._.isArray(size)) {
                cy.viewport(size[0], size[1])
            } else {
                cy.viewport(size)
            }
            if (size === testData.sizeiPhone || size === testData.sizeAndroid || size === testData.sizeiPad) {
                Header.searchMobile(testData.validSearchTxt)
                PLPPage.clickARandomProduct()
            }
            else {
                Header.search(testData.validSearchTxt)
                PLPPage.clickARandomProduct()
            }
        })
    })
})

Try adding "metadata" to the BasePage return values尝试将“元数据”添加到 BasePage 返回值

// BasePage

setiPhoneViewport() {
  return {
    device: 'iphone-6',
    isMobile: 'mobile'
  }
}
// Test

if (Cypress._.isArray(size)) {
  cy.viewport(size[0], size[1])
} else {
  cy.viewport(size.device)
}

const search = size.isMobile ? Header.searchMobile : Header.search;
search(testData.validSearchTxt)
PLPPage.clickARandomProduct()

If you have a "custom" size that happens to be mobile,如果你有一个恰好是移动的“自定义”尺寸,

const sizes = [
  BasePage.setiPhoneViewport(),
  BasePage.setAndriodViewport(), 
  BasePage.setTabletViewport(), 
  [1280, 768],
  [320, 250],    // is mobile
]

then include it in the expression然后将其包含在表达式中

// Test

function getSearchMethod(size) {
  return 
    
}

if (Cypress._.isArray(size)) {
  cy.viewport(size[0], size[1])
} else {
  cy.viewport(size.device)
}

const search = size.isMobile || (Cypress._.isArray(size) && size[0] < 350) ? 
    Header.searchMobile : Header.search;
search(testData.validSearchTxt)
PLPPage.clickARandomProduct()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM