问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/53245252/detox-how-to-check-if-an-element-is-present-without-using-expect