Cypress - if then functions
I have question about Cypress.
I have an element on page which doesn't appear allways. There is no logic when it shows and when not.
Is in Cypress some IF/THEN function or something how do you check if the element is displayed (so fill it up) and when you don't see it than skip that step?
My code:
if (Cypress.$('[data-bind="validationElement: interestInsurable"]').length > 0) {
cy.get('[for="p4-conditional-csob-interest-insurable-1"]').click()
}
else {cy.get('#car-info-serie')}
This is how it looks like in playground:
Picture
And there is HTML of that checkbox:
<label class="z-radio z-radio-inline primary" for="p4-conditional-csob-interest-insurable-1" data-bind="popover: { content: VehicleInsuranceTooltips.conditionalDataCsobInterestInsurable1Tooltip }" data-original-title="" title="">
<input id="p4-conditional-csob-interest-insurable-1" name="p4-conditional-csob-interest-insurable" type="radio" class="custom-radio" data-toggle="radio" data-bind="checkedValue: 1, checked: interestInsurable" value="1">
<span class="icons">
<span class="icon-unchecked"></span>
<span class="icon-checked"></span>
</span>
Patří vozidlo zájemci o pojištění?
</label>
if-statement testing automation cypress
add a comment |
I have question about Cypress.
I have an element on page which doesn't appear allways. There is no logic when it shows and when not.
Is in Cypress some IF/THEN function or something how do you check if the element is displayed (so fill it up) and when you don't see it than skip that step?
My code:
if (Cypress.$('[data-bind="validationElement: interestInsurable"]').length > 0) {
cy.get('[for="p4-conditional-csob-interest-insurable-1"]').click()
}
else {cy.get('#car-info-serie')}
This is how it looks like in playground:
Picture
And there is HTML of that checkbox:
<label class="z-radio z-radio-inline primary" for="p4-conditional-csob-interest-insurable-1" data-bind="popover: { content: VehicleInsuranceTooltips.conditionalDataCsobInterestInsurable1Tooltip }" data-original-title="" title="">
<input id="p4-conditional-csob-interest-insurable-1" name="p4-conditional-csob-interest-insurable" type="radio" class="custom-radio" data-toggle="radio" data-bind="checkedValue: 1, checked: interestInsurable" value="1">
<span class="icons">
<span class="icon-unchecked"></span>
<span class="icon-checked"></span>
</span>
Patří vozidlo zájemci o pojištění?
</label>
if-statement testing automation cypress
Cypress docs - Conditional Testing contains a huge number of examples.
– Richard Matsen
Nov 23 '18 at 18:56
How is it possible that there is no logic as to when it shows or not? What makes it show? I guess I am confused as to how an element can just decide to show up in your dom by itself. Can you explain more?
– Maccurt
Nov 25 '18 at 19:11
There are products that you can choose. They're sorted by price and price is changing with every field before. Only two products have that element.
– Dominik Skála
Nov 26 '18 at 9:49
add a comment |
I have question about Cypress.
I have an element on page which doesn't appear allways. There is no logic when it shows and when not.
Is in Cypress some IF/THEN function or something how do you check if the element is displayed (so fill it up) and when you don't see it than skip that step?
My code:
if (Cypress.$('[data-bind="validationElement: interestInsurable"]').length > 0) {
cy.get('[for="p4-conditional-csob-interest-insurable-1"]').click()
}
else {cy.get('#car-info-serie')}
This is how it looks like in playground:
Picture
And there is HTML of that checkbox:
<label class="z-radio z-radio-inline primary" for="p4-conditional-csob-interest-insurable-1" data-bind="popover: { content: VehicleInsuranceTooltips.conditionalDataCsobInterestInsurable1Tooltip }" data-original-title="" title="">
<input id="p4-conditional-csob-interest-insurable-1" name="p4-conditional-csob-interest-insurable" type="radio" class="custom-radio" data-toggle="radio" data-bind="checkedValue: 1, checked: interestInsurable" value="1">
<span class="icons">
<span class="icon-unchecked"></span>
<span class="icon-checked"></span>
</span>
Patří vozidlo zájemci o pojištění?
</label>
if-statement testing automation cypress
I have question about Cypress.
I have an element on page which doesn't appear allways. There is no logic when it shows and when not.
Is in Cypress some IF/THEN function or something how do you check if the element is displayed (so fill it up) and when you don't see it than skip that step?
My code:
if (Cypress.$('[data-bind="validationElement: interestInsurable"]').length > 0) {
cy.get('[for="p4-conditional-csob-interest-insurable-1"]').click()
}
else {cy.get('#car-info-serie')}
This is how it looks like in playground:
Picture
And there is HTML of that checkbox:
<label class="z-radio z-radio-inline primary" for="p4-conditional-csob-interest-insurable-1" data-bind="popover: { content: VehicleInsuranceTooltips.conditionalDataCsobInterestInsurable1Tooltip }" data-original-title="" title="">
<input id="p4-conditional-csob-interest-insurable-1" name="p4-conditional-csob-interest-insurable" type="radio" class="custom-radio" data-toggle="radio" data-bind="checkedValue: 1, checked: interestInsurable" value="1">
<span class="icons">
<span class="icon-unchecked"></span>
<span class="icon-checked"></span>
</span>
Patří vozidlo zájemci o pojištění?
</label>
if-statement testing automation cypress
if-statement testing automation cypress
edited Nov 24 '18 at 8:23
Dominik Skála
asked Nov 23 '18 at 13:54
Dominik SkálaDominik Skála
227
227
Cypress docs - Conditional Testing contains a huge number of examples.
– Richard Matsen
Nov 23 '18 at 18:56
How is it possible that there is no logic as to when it shows or not? What makes it show? I guess I am confused as to how an element can just decide to show up in your dom by itself. Can you explain more?
– Maccurt
Nov 25 '18 at 19:11
There are products that you can choose. They're sorted by price and price is changing with every field before. Only two products have that element.
– Dominik Skála
Nov 26 '18 at 9:49
add a comment |
Cypress docs - Conditional Testing contains a huge number of examples.
– Richard Matsen
Nov 23 '18 at 18:56
How is it possible that there is no logic as to when it shows or not? What makes it show? I guess I am confused as to how an element can just decide to show up in your dom by itself. Can you explain more?
– Maccurt
Nov 25 '18 at 19:11
There are products that you can choose. They're sorted by price and price is changing with every field before. Only two products have that element.
– Dominik Skála
Nov 26 '18 at 9:49
Cypress docs - Conditional Testing contains a huge number of examples.
– Richard Matsen
Nov 23 '18 at 18:56
Cypress docs - Conditional Testing contains a huge number of examples.
– Richard Matsen
Nov 23 '18 at 18:56
How is it possible that there is no logic as to when it shows or not? What makes it show? I guess I am confused as to how an element can just decide to show up in your dom by itself. Can you explain more?
– Maccurt
Nov 25 '18 at 19:11
How is it possible that there is no logic as to when it shows or not? What makes it show? I guess I am confused as to how an element can just decide to show up in your dom by itself. Can you explain more?
– Maccurt
Nov 25 '18 at 19:11
There are products that you can choose. They're sorted by price and price is changing with every field before. Only two products have that element.
– Dominik Skála
Nov 26 '18 at 9:49
There are products that you can choose. They're sorted by price and price is changing with every field before. Only two products have that element.
– Dominik Skála
Nov 26 '18 at 9:49
add a comment |
2 Answers
2
active
oldest
votes
There is no built in way to do this in cypress. I am using this in my tests:
if (Cypress.$("#yourElement").length > 0) {
// element exists, do something
} else {
// element does not exist, do something else
}
That doesn't work. I'm clicking on checkbox if element exists and this code allways found nothing.
– Dominik Skála
Nov 23 '18 at 19:13
Post your code, HTML, and cypress command log and people will be able to help you better
– Brendan
Nov 23 '18 at 19:19
I post it in description.
– Dominik Skála
Nov 24 '18 at 10:02
You have the wrong selector, you're telling it to click the label, not the input. Try this:cy.get('input#p4-conditional-csob-interest-insurable-1').click()
instead ofcy.get('[for="p4-conditional-csob-interest-insurable-1"]').click()
– Brendan
Nov 25 '18 at 17:12
add a comment |
You have to click on the input element instead of the label:
cy.get('#p4-conditional-csob-interest-insurable-1').click();
Anyways have a look at the Cypress Docs as conditional testing is strongly discouraged.
New contributor
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448012%2fcypress-if-then-functions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is no built in way to do this in cypress. I am using this in my tests:
if (Cypress.$("#yourElement").length > 0) {
// element exists, do something
} else {
// element does not exist, do something else
}
That doesn't work. I'm clicking on checkbox if element exists and this code allways found nothing.
– Dominik Skála
Nov 23 '18 at 19:13
Post your code, HTML, and cypress command log and people will be able to help you better
– Brendan
Nov 23 '18 at 19:19
I post it in description.
– Dominik Skála
Nov 24 '18 at 10:02
You have the wrong selector, you're telling it to click the label, not the input. Try this:cy.get('input#p4-conditional-csob-interest-insurable-1').click()
instead ofcy.get('[for="p4-conditional-csob-interest-insurable-1"]').click()
– Brendan
Nov 25 '18 at 17:12
add a comment |
There is no built in way to do this in cypress. I am using this in my tests:
if (Cypress.$("#yourElement").length > 0) {
// element exists, do something
} else {
// element does not exist, do something else
}
That doesn't work. I'm clicking on checkbox if element exists and this code allways found nothing.
– Dominik Skála
Nov 23 '18 at 19:13
Post your code, HTML, and cypress command log and people will be able to help you better
– Brendan
Nov 23 '18 at 19:19
I post it in description.
– Dominik Skála
Nov 24 '18 at 10:02
You have the wrong selector, you're telling it to click the label, not the input. Try this:cy.get('input#p4-conditional-csob-interest-insurable-1').click()
instead ofcy.get('[for="p4-conditional-csob-interest-insurable-1"]').click()
– Brendan
Nov 25 '18 at 17:12
add a comment |
There is no built in way to do this in cypress. I am using this in my tests:
if (Cypress.$("#yourElement").length > 0) {
// element exists, do something
} else {
// element does not exist, do something else
}
There is no built in way to do this in cypress. I am using this in my tests:
if (Cypress.$("#yourElement").length > 0) {
// element exists, do something
} else {
// element does not exist, do something else
}
answered Nov 23 '18 at 17:00
BrendanBrendan
889616
889616
That doesn't work. I'm clicking on checkbox if element exists and this code allways found nothing.
– Dominik Skála
Nov 23 '18 at 19:13
Post your code, HTML, and cypress command log and people will be able to help you better
– Brendan
Nov 23 '18 at 19:19
I post it in description.
– Dominik Skála
Nov 24 '18 at 10:02
You have the wrong selector, you're telling it to click the label, not the input. Try this:cy.get('input#p4-conditional-csob-interest-insurable-1').click()
instead ofcy.get('[for="p4-conditional-csob-interest-insurable-1"]').click()
– Brendan
Nov 25 '18 at 17:12
add a comment |
That doesn't work. I'm clicking on checkbox if element exists and this code allways found nothing.
– Dominik Skála
Nov 23 '18 at 19:13
Post your code, HTML, and cypress command log and people will be able to help you better
– Brendan
Nov 23 '18 at 19:19
I post it in description.
– Dominik Skála
Nov 24 '18 at 10:02
You have the wrong selector, you're telling it to click the label, not the input. Try this:cy.get('input#p4-conditional-csob-interest-insurable-1').click()
instead ofcy.get('[for="p4-conditional-csob-interest-insurable-1"]').click()
– Brendan
Nov 25 '18 at 17:12
That doesn't work. I'm clicking on checkbox if element exists and this code allways found nothing.
– Dominik Skála
Nov 23 '18 at 19:13
That doesn't work. I'm clicking on checkbox if element exists and this code allways found nothing.
– Dominik Skála
Nov 23 '18 at 19:13
Post your code, HTML, and cypress command log and people will be able to help you better
– Brendan
Nov 23 '18 at 19:19
Post your code, HTML, and cypress command log and people will be able to help you better
– Brendan
Nov 23 '18 at 19:19
I post it in description.
– Dominik Skála
Nov 24 '18 at 10:02
I post it in description.
– Dominik Skála
Nov 24 '18 at 10:02
You have the wrong selector, you're telling it to click the label, not the input. Try this:
cy.get('input#p4-conditional-csob-interest-insurable-1').click()
instead of cy.get('[for="p4-conditional-csob-interest-insurable-1"]').click()
– Brendan
Nov 25 '18 at 17:12
You have the wrong selector, you're telling it to click the label, not the input. Try this:
cy.get('input#p4-conditional-csob-interest-insurable-1').click()
instead of cy.get('[for="p4-conditional-csob-interest-insurable-1"]').click()
– Brendan
Nov 25 '18 at 17:12
add a comment |
You have to click on the input element instead of the label:
cy.get('#p4-conditional-csob-interest-insurable-1').click();
Anyways have a look at the Cypress Docs as conditional testing is strongly discouraged.
New contributor
add a comment |
You have to click on the input element instead of the label:
cy.get('#p4-conditional-csob-interest-insurable-1').click();
Anyways have a look at the Cypress Docs as conditional testing is strongly discouraged.
New contributor
add a comment |
You have to click on the input element instead of the label:
cy.get('#p4-conditional-csob-interest-insurable-1').click();
Anyways have a look at the Cypress Docs as conditional testing is strongly discouraged.
New contributor
You have to click on the input element instead of the label:
cy.get('#p4-conditional-csob-interest-insurable-1').click();
Anyways have a look at the Cypress Docs as conditional testing is strongly discouraged.
New contributor
New contributor
answered Feb 1 at 17:17
DavidzDavidz
363
363
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448012%2fcypress-if-then-functions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Cypress docs - Conditional Testing contains a huge number of examples.
– Richard Matsen
Nov 23 '18 at 18:56
How is it possible that there is no logic as to when it shows or not? What makes it show? I guess I am confused as to how an element can just decide to show up in your dom by itself. Can you explain more?
– Maccurt
Nov 25 '18 at 19:11
There are products that you can choose. They're sorted by price and price is changing with every field before. Only two products have that element.
– Dominik Skála
Nov 26 '18 at 9:49