Detox - How to check if an element is present without using expect
up vote
0
down vote
favorite
Is there a way to check if an element is present without using expect with Detox? Right now I'm having to nest my logic in try/catch blocks to control the flow of a test to mitigate flakiness as it checks the state of a screen before moving forward with the test. I would much rather be able to do with using if/else.
Thanks in advance for any suggestions.
detox
add a comment |
up vote
0
down vote
favorite
Is there a way to check if an element is present without using expect with Detox? Right now I'm having to nest my logic in try/catch blocks to control the flow of a test to mitigate flakiness as it checks the state of a screen before moving forward with the test. I would much rather be able to do with using if/else.
Thanks in advance for any suggestions.
detox
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Is there a way to check if an element is present without using expect with Detox? Right now I'm having to nest my logic in try/catch blocks to control the flow of a test to mitigate flakiness as it checks the state of a screen before moving forward with the test. I would much rather be able to do with using if/else.
Thanks in advance for any suggestions.
detox
Is there a way to check if an element is present without using expect with Detox? Right now I'm having to nest my logic in try/catch blocks to control the flow of a test to mitigate flakiness as it checks the state of a screen before moving forward with the test. I would much rather be able to do with using if/else.
Thanks in advance for any suggestions.
detox
detox
asked Nov 11 at 2:10
Allen Johnson
11
11
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Not the most elegant solution but I have used the following to stream line my code.
In a helper file, I create functions that wrap the try/catch, and return true/false:
For example:
const expectToBeVisible = async (id) => {
try {
await expect(element(by.id(id))).toBeVisible();
return true;
} catch (e) {
return false;
}
};
module.exports = { expectToBeVisible };
Then when I am performing tests that depend on that element being visible or not I can do the following:
import { expectToBeVisible } from './helpers';
describe('Test', () => {
...
it('If button is visible do X else do Y', async () => {
let buttonVisible = await expectToBeVisible('button');
if (buttonVisible) {
// do something with that button
} else {
// do something else as the button isn't visible
}
});
...
});
This isn't the best solution but until Detox come up with the ability to have if/else then it could suffice in a pinch.
This will help. It will certainly clean up what I have now. The logic has been getting harder to track with all of the nested try/catch blocks I've had to implement. And it's been difficult to frame it in a way that doesn't leave the test in a state where it is always passing.
– Allen Johnson
Nov 23 at 0:51
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Not the most elegant solution but I have used the following to stream line my code.
In a helper file, I create functions that wrap the try/catch, and return true/false:
For example:
const expectToBeVisible = async (id) => {
try {
await expect(element(by.id(id))).toBeVisible();
return true;
} catch (e) {
return false;
}
};
module.exports = { expectToBeVisible };
Then when I am performing tests that depend on that element being visible or not I can do the following:
import { expectToBeVisible } from './helpers';
describe('Test', () => {
...
it('If button is visible do X else do Y', async () => {
let buttonVisible = await expectToBeVisible('button');
if (buttonVisible) {
// do something with that button
} else {
// do something else as the button isn't visible
}
});
...
});
This isn't the best solution but until Detox come up with the ability to have if/else then it could suffice in a pinch.
This will help. It will certainly clean up what I have now. The logic has been getting harder to track with all of the nested try/catch blocks I've had to implement. And it's been difficult to frame it in a way that doesn't leave the test in a state where it is always passing.
– Allen Johnson
Nov 23 at 0:51
add a comment |
up vote
0
down vote
Not the most elegant solution but I have used the following to stream line my code.
In a helper file, I create functions that wrap the try/catch, and return true/false:
For example:
const expectToBeVisible = async (id) => {
try {
await expect(element(by.id(id))).toBeVisible();
return true;
} catch (e) {
return false;
}
};
module.exports = { expectToBeVisible };
Then when I am performing tests that depend on that element being visible or not I can do the following:
import { expectToBeVisible } from './helpers';
describe('Test', () => {
...
it('If button is visible do X else do Y', async () => {
let buttonVisible = await expectToBeVisible('button');
if (buttonVisible) {
// do something with that button
} else {
// do something else as the button isn't visible
}
});
...
});
This isn't the best solution but until Detox come up with the ability to have if/else then it could suffice in a pinch.
This will help. It will certainly clean up what I have now. The logic has been getting harder to track with all of the nested try/catch blocks I've had to implement. And it's been difficult to frame it in a way that doesn't leave the test in a state where it is always passing.
– Allen Johnson
Nov 23 at 0:51
add a comment |
up vote
0
down vote
up vote
0
down vote
Not the most elegant solution but I have used the following to stream line my code.
In a helper file, I create functions that wrap the try/catch, and return true/false:
For example:
const expectToBeVisible = async (id) => {
try {
await expect(element(by.id(id))).toBeVisible();
return true;
} catch (e) {
return false;
}
};
module.exports = { expectToBeVisible };
Then when I am performing tests that depend on that element being visible or not I can do the following:
import { expectToBeVisible } from './helpers';
describe('Test', () => {
...
it('If button is visible do X else do Y', async () => {
let buttonVisible = await expectToBeVisible('button');
if (buttonVisible) {
// do something with that button
} else {
// do something else as the button isn't visible
}
});
...
});
This isn't the best solution but until Detox come up with the ability to have if/else then it could suffice in a pinch.
Not the most elegant solution but I have used the following to stream line my code.
In a helper file, I create functions that wrap the try/catch, and return true/false:
For example:
const expectToBeVisible = async (id) => {
try {
await expect(element(by.id(id))).toBeVisible();
return true;
} catch (e) {
return false;
}
};
module.exports = { expectToBeVisible };
Then when I am performing tests that depend on that element being visible or not I can do the following:
import { expectToBeVisible } from './helpers';
describe('Test', () => {
...
it('If button is visible do X else do Y', async () => {
let buttonVisible = await expectToBeVisible('button');
if (buttonVisible) {
// do something with that button
} else {
// do something else as the button isn't visible
}
});
...
});
This isn't the best solution but until Detox come up with the ability to have if/else then it could suffice in a pinch.
answered Nov 22 at 15:56
Andrew
1668
1668
This will help. It will certainly clean up what I have now. The logic has been getting harder to track with all of the nested try/catch blocks I've had to implement. And it's been difficult to frame it in a way that doesn't leave the test in a state where it is always passing.
– Allen Johnson
Nov 23 at 0:51
add a comment |
This will help. It will certainly clean up what I have now. The logic has been getting harder to track with all of the nested try/catch blocks I've had to implement. And it's been difficult to frame it in a way that doesn't leave the test in a state where it is always passing.
– Allen Johnson
Nov 23 at 0:51
This will help. It will certainly clean up what I have now. The logic has been getting harder to track with all of the nested try/catch blocks I've had to implement. And it's been difficult to frame it in a way that doesn't leave the test in a state where it is always passing.
– Allen Johnson
Nov 23 at 0:51
This will help. It will certainly clean up what I have now. The logic has been getting harder to track with all of the nested try/catch blocks I've had to implement. And it's been difficult to frame it in a way that doesn't leave the test in a state where it is always passing.
– Allen Johnson
Nov 23 at 0:51
add a comment |
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%2f53245252%2fdetox-how-to-check-if-an-element-is-present-without-using-expect%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