admin管理员组文章数量:1417053
.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
2 Answers
Reset to default 4You 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
})
本文标签: javascriptIf else condition in CypressStack Overflow
版权声明:本文标题:javascript - If else condition in Cypress - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745261810a2650388.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论