【问题标题】:Protractor - How to add if condition if element is visible so skip on this test case else do the actions below量角器 - 如果元素可见,则如何添加条件,因此请跳过此测试用例,否则请执行以下操作
【发布时间】:2016-10-29 11:58:22
【问题描述】:

我已尝试为具有 2 个替代用例的测试用例添加“if”条件,

第一个是如果它存在用户,所以它应该在屏幕上寻找“idan+free@gmail.com”的字符串,然后跳到下一个测试用例,

ELSE -执行以下操作。

测试用例 -

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]);
         });
        }
     });
  });

});

如果用户未退出,则运行测试时出错 -

[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].<anonymous> (/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].<anonymous> (/var/lib/jenkins/workspace/Sanity_Testing_node_12_2/testCases/newApp/freeUserTC.js:205:3)
    at Object.<anonymous> (/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].<anonymous> (/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].<anonymous> (/var/lib/jenkins/workspace/Sanity_Testing_node_12_2/testCases/newApp/freeUserTC.js:237:4)
    at Object.<anonymous> (/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

元素: -

<div class="email font-xs ng-binding">idan+free@gmail.com</div>

【问题讨论】:

  • @alecxe - stackoverflow.com/users/771848/alecxe请您帮忙!
  • 快速提问:by.partialLinkText() 代替 by.linkText() 有什么不同吗?谢谢。
  • 不,它收到相同的错误@alecxe - NoSuchElementError: No element found using locator: By.partialLinkText("idan+free@gmail.com")
  • 好的,如果你也在那里添加一个等待呢:browser.wait(EC.presenceOf(noActivation), 30000);?
  • 现在错误与此等待命令有关@alecxe - “错误:等待30020ms后超时”

标签: node.js selenium-webdriver automation jasmine protractor


【解决方案1】:

by.linkTextby.partialLinkText 仅适用于 a 元素

在您的情况下,您有一个div 元素,您需要按文本过滤。你可以选择:

element(by.xpath("//div[. = 'idan+free@gmail.com' and contains(@class, 'email')]"))

或者by.cssContainingText:

element(by.cssContainingText(".email", "idan+free@gmail.com"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-27
    • 2023-03-07
    • 2013-01-19
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多