title: Protractor - How to add if condition if element is visible so skip on this test case else do the actions below
body: <p>I've tried to add an "if" condition for test case with 2 alternative use cases,</p>
  
  <p>The first one is <strong>IF</strong> it exists user so it should looking for the string of "idan+free@gmail.com" on the screen and skip on the test case to next,</p>
  
  <p><strong>ELSE -</strong> Perform the actions below.</p>
  
  <p><strong>Test cases -</strong></p>
  
  <pre><code>describe ('Production - New Free Account Test Case', function() {
  
      var EC = protractor.ExpectedConditions;
      var dashboardBtn = element(by.xpath('//div/md-sidenav/f-main-menu/div[1]/ul[1]/li[1]/button/div/div[1]/md-icon'));
      var noActivation = element(by.linkText("idan+free@gmail.com"));
  
      beforeEach(function() {
        browser.ignoreSynchronization = true;
      });
  
      afterEach(function() {
        browser.ignoreSynchronization = false;
      });
  
       it ('Production - Free Account - Activation', function() {
        browser.wait(EC.elementToBeClickable(dashboardBtn), 30000);
        dashboardBtn.click();
        browser.wait(EC.visibilityOf(element(by.css("div.task-header.font-smd.task-clickable.ng-binding.flex"))), 30000);
        noActivation.isDisplayed().then(function(isVisible) {
          if (isVisible) {
             browser.driver.sleep(1000);
             console.log("SKIPPED");
          } else {
            $$("div.task-header.font-smd.task-clickable.ng-binding.flex").get(1).click();
             var mainWindow;
                browser.driver.getAllWindowHandles().then(
                  function(handles) {
                     mainWindow = handles[0]; //at this point there should be only 1 window
                  }
                );
  
              browser.driver.getAllWindowHandles().then(function (handles) {    
                 handles.forEach(function(handle) {
                   if (handle !== mainWindow) {
                      browser.driver.switchTo().window(handle)
                       //-=- switch to iFrame handler -=-
                      browser.driver.switchTo().defaultContent(); // you are now outside both frames
                      browser.driver.wait(EC.visibilityOf(element(by.css("div.clean-white-box.short-registration"))), 30000);
                      browser.driver.sleep(2000); 
                    }
                });
             }); 
          }
        });
     });
  
     it ('Production - Free Account - From Activation to pricing table', function() {
        noActivation.isDisplayed().then(function(isVisible) {
          if (isVisible) {
             browser.driver.sleep(1000);
             console.log("SKIPPED");
          } else {
           element(by.id("pivot_user_attributes_password")).sendKeys("password");
           element(by.id("pivot_info_business_size")).click();
           browser.wait(EC.visibilityOf(element(by.css('option[value="I do not have a business"]'))), 30000);
           element(by.css('option[value="I do not have a business"]')).click();
           browser.driver.sleep(1000);
           element(by.id("pivot_country_name")).click();
           browser.wait(EC.visibilityOf(element(by.css('option[value="United States"]'))), 30000);
           element(by.css('option[value="United States"]')).click();
           browser.driver.sleep(1000);
           element(by.id("pivot_phone_info")).sendKeys("1234567890");
           element(by.id("pivot_info_is_web_designer_false")).click();
           browser.driver.sleep(1000);
           element(by.css("input.green-button")).click();
           browser.driver.wait(EC.visibilityOf(element(by.css("div.packages"))), 30000);
         }
       });
    });
  
      it ('Production - Free Account - Activation process has been completed', function() {
         noActivation.isDisplayed().then(function(isVisible) {
          if (isVisible) {
              browser.driver.sleep(1000);
              console.log("SKIPPED");
          } else {
            element(by.css("a.upgrade-button.signup-button.free-button")).click();
            browser.wait(EC.visibilityOf(element(by.css("div.tasks-container.layout-column"))), 30000);
            element(by.xpath("//div/div/div[2]/div/md-content/div[3]/f-tasks/div[2]/div[1]/f-task-block[2]/div/div[2]/div[1]/div[1]")).click();
            expect(element(by.css("div.task-header.font-smd.task-clickable.ng-binding.flex")).isDisplayed()).toBe(true);
          }
        });
      });
  
    it ('Production - Free Account - Empty pages - Calendar first launch', function() {
       noActivation.isDisplayed().then(function(isVisible) {
          if (isVisible) {
             browser.driver.sleep(1000);
             console.log("SKIPPED");
          } else {
            browser.driver.getAllWindowHandles().then(function (handles) {
            browser.driver.close();
            browser.driver.switchTo().window(handles[0]);
           });
          }
       });
    });
  
  });
  </code></pre>
  
  <p><strong>Error when running the test if the user is not exits -</strong></p>
  
  <pre><code>[31m Production - Free Account - Activation - fail[0m
  
  
    1) Test cases pul Frontage Sanity Test Production - New Free Account Test C Production - Free Account - Activation
     Message:
       [31mNoSuchElementError: No element found using locator: By.linkText("idan+free@gmail.com")[0m
     Stacktrace:
       NoSuchElementError: No element found using locator: By.linkText("idan+free@gmail.com")
      at Array.forEach (native)
      at runMicrotasksCallback (node.js:337:7)
  Error
      at [object Object].&lt;anonymous&gt; (/var/lib/jenkins/workspace/Sanity_Testing_node_12_2/testCases/newApp/freeUserTC.js:209:20)
  From: Task: Asynchronous test function: it()
  Error
      at [object Object].&lt;anonymous&gt; (/var/lib/jenkins/workspace/Sanity_Testing_node_12_2/testCases/newApp/freeUserTC.js:205:3)
      at Object.&lt;anonymous&gt; (/var/lib/jenkins/workspace/Sanity_Testing_node_12_2/testCases/newApp/freeUserTC.js:1:63)[31m Production - Free Account - From Activation to pricing table - fail[0m
  
  
    2) Test cases pul Frontage Sanity Test Production - New Free Account Test C Production - Free Account - From Activation to pricing table
     Message:
       [31mNoSuchElementError: No element found using locator: By.linkText("idan+f.com")[0m
     Stacktrace:
       NoSuchElementError: No element found using locator: By.linkText("idan+f.com")
      at Array.forEach (native)
      at runMicrotasksCallback (node.js:337:7)
  Error
      at [object Object].&lt;anonymous&gt; (/var/lib/jenkins/workspace/Sanity_Testing_node_12_2/testCases/newApp/freeUserTC.js:238:20)
  From: Task: Asynchronous test function: it()
  Error
      at [object Object].&lt;anonymous&gt; (/var/lib/jenkins/workspace/Sanity_Testing_node_12_2/testCases/newApp/freeUserTC.js:237:4)
      at Object.&lt;anonymous&gt; (/var/lib/jenkins/workspace/Sanity_Testing_node_12_2/testCases/newApp/freeUserTC.js:1:63)[31m Production - Free Account - Activation process has been completed - fail[0m
  </code></pre>
  
  <p><strong>The element:</strong> - </p>
  
  <pre><code>&lt;div class="email font-xs ng-binding"&gt;idan+free@gmail.com&lt;/div&gt;
  </code></pre>