最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - If else condition in Cypress - Stack Overflow

programmeradmin0浏览0评论

.grid > languagefield is the element of UI dropdown list

Depends on the language selected on this languagefield, it'll do something

This is what I have but Cypress doesn't hit if condition, something not right with if (Lang == 'American English')

const Date = Cypress.moment().add(-3, 'days').format('YYYY-MM-DD');
const DateUS = Cypress.moment(Date).format('MM/DD');
const DateUK = Cypress.moment(Date).format('DD/MM');
const Lang = '.grid > languagefield'
    
        if (Lang == 'American English') {
          //should get DateUS format  
        } else {
          //should get DateUK format 
         }

Thanks a lot!

Result of cy.get('.grid > :nth-child(7)').invoke('text').then((txt) => { cy.log(txt) })

More info

.grid > languagefield is the element of UI dropdown list

Depends on the language selected on this languagefield, it'll do something

This is what I have but Cypress doesn't hit if condition, something not right with if (Lang == 'American English')

const Date = Cypress.moment().add(-3, 'days').format('YYYY-MM-DD');
const DateUS = Cypress.moment(Date).format('MM/DD');
const DateUK = Cypress.moment(Date).format('DD/MM');
const Lang = '.grid > languagefield'
    
        if (Lang == 'American English') {
          //should get DateUS format  
        } else {
          //should get DateUK format 
         }

Thanks a lot!

Result of cy.get('.grid > :nth-child(7)').invoke('text').then((txt) => { cy.log(txt) })

More info

Share Improve this question edited Dec 14, 2020 at 13:07 user3601310 asked Dec 14, 2020 at 3:35 user3601310user3601310 8932 gold badges12 silver badges19 bronze badges 1
  • is this what you're looking for? stackoverflow./questions/57533756/… – albert Commented Dec 14, 2020 at 3:39
Add a ment  | 

2 Answers 2

Reset to default 4

You can write something like:

cy.get('.grid > languagefield').invoke('text').then((txt) => {
  if (txt.trim() == 'American English') {
    //Do Something
  } else {
    //Do Something 
  }
})

OR, If you want to use each() to loop through the elements of the dropdown list and then find the text and perform some action, you can do something like:

cy.get('selctor').each(($el) => {
  if ($el.text().trim() == 'American English') {
    //Do Something
  } else {
    //Do Something 
  }
})

If the Profile section is visible, this will get you the text of the dropdown,

const language = Cypress.$('div[name="language"]')  // parent div for language selector
  .children().first()                               // first child is selected text 
  .text()

console.log(language);     // American English

The equivalent for Cypress aliasing

const languageDateFormats = {
  'American English': DateUS,
  'British English': DateUK,
  // other langauges
}

cy.visit('/profile');
cy.get('div[name="language"]')  // parent div for language selector
  .children().first()           // first child is selected text 
  .invoke('text')
  .as('language')

cy.visit('/timeline');
cy.get('@language').then(language => {
  const dateFormat = languageDateFormats[language];
  // test the timeline date format
})
发布评论

评论列表(0)

  1. 暂无评论