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.










share|improve this question


























    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.










    share|improve this question
























      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.










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 2:10









      Allen Johnson

      11




      11
























          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.






          share|improve this answer





















          • 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











          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',
          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
          });


          }
          });














           

          draft saved


          draft discarded


















          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

























          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.






          share|improve this answer





















          • 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















          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.






          share|improve this answer





















          • 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













          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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


















          • 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


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Full-time equivalent

          さくらももこ

          13 indicted, 8 arrested in Calif. drug cartel investigation