量角器 - 如果可能的话,我想写IF ELSE语句DRYer

时间:2016-01-03 17:39:11

标签: javascript angularjs selenium-webdriver protractor

我可以D.R.Y这段代码吗?如果在用户的个人资料中显示了id invite-user标签,则用户可以点击请求玩游戏ELSE将选择新的随机用户,直到用户显示id invite-user为止# 39; s简介。

  browser.get("/index").then(function() {
   element.all(by.css('game-player')).get(randomUser).click();
   browser.sleep(sleep);
element(by.id('invite-user')).isDisplayed().then(function(clickUser) {
  if (clickUser) {
    element(by.id('invite-user')).click();
    element(by.id('role')).sendKeys('Player');
    button.click();
    browser.sleep(sleep);
    expect(element(by.id('pending-admin-approval')).first().isDisplayed()).toBe(true);

  } else {
    browser.get("/index").then(function() {
      element.all(by.css('game-player')).get(randomUser).click();
      browser.sleep(sleep);
      element(by.id('invite-user')).click();
      element(by.id('role')).sendKeys('Player');
      button.click();
      browser.sleep(sleep);
      expect(element(by.id('pending-admin-approval')).first().isDisplayed()).toBe(true);

    });
  }
});

});

2 个答案:

答案 0 :(得分:2)

Product

答案 1 :(得分:1)

更多优化方法:

    browser.get("/index").then(function() {
        element.all(by.css('game-player')).get(randomUser).click();
        waitForApi();
        element(by.id('invite-user')).isDisplayed().then(function(clickUser) {
            if (clickUser) {
                inviteUser();
                return; // Don't go further 
            }

            browser.get("/index").then(function() {
                element.all(by.css('game-player')).get(randomUser).click();
                waitForApi();
                inviteUser();
            });

        });

        function inviteUser(){
            element(by.id('invite-user')).click();
            element(by.id('role')).sendKeys('Player');
            button.click();
            waitForApi();
            expect(element(by.id('pending-admin-approval')).first().isDisplayed()).toBe(true);
        }

此功能的需要:例如,如果我们在browser.sleep中设置2000(即2秒),则将出现两种情况:1. API花费的时间超过2s,然后测试用例失败2. API花费的时间少于2s,那么我们就不必等待。因此,为了克服这个问题,我建议检查API成功执行后显示/隐藏的元素

        function waitForApi(){
            let EC = protractor.ExpectedConditions;
            // Waits for the api loader to be no longer visible
            browser.wait(EC.invisibilityOf(element(by.css('#test > .loader'))), 10000); // TODO : Change the condition as per visibility/invisibilty of element in your project
        }
    });