2017-10-04 17 views
0

我正在编写一个登录到非角度应用程序的测试。等待整个页面加载不能按预期在量角器中工作

describe('login Test for App', function() { 

    beforeEach(function() { 
     browser.ignoreSynchronization=true; 
     browser.get(loginPageURL,2000); 
    }); 


    it('It should Login a User', function() { 
     element(by.id('username')).sendKeys(constants.userName); 
     element(by.id('password')).sendKeys(constants.password); 
     element(by.id('Login')).click().then(function() { 
      // Waiting to open modal 
      browser.wait(function() { 
       return browser.getCurrentUrl().then(function (url) { 
        return url==dashboardUrl; 
       }); 
      }); 
     }); 
    }); 
}); 

登录后,我想检查currentUrl。但登录按钮点击后,它会等到仪表板网址出现。 但是,当登录按钮点击后,它会转到不同的网址,然后它也会等待仪表板网址无限。 我想在登录事件后检查当前URL,如果它不是仪表板网址,那么它应该失败,并且不要运行下一个测试套件的情况,因为登录测试失败。

Like-

当点击登录按钮。

  1. 等待整页加载。

2.然后检查当前网址,如果不是仪表板网址,则测试应该失败,并且不能进行任何测试用例。

回答

1

最好不要等待页面url加载,以便知道页面是否已加载,因为可能存在重定向或其他内容。

最好的做法是等待下一页上的特定元素(登录后)。这里是你的代码重构为使用自定义wait()的功能,将等因素继续检索当前网址前会出现:

describe('login Test for App', function() { 
    browser.ignoreSynchronization = true; 

    it('should load the log-in page', function(done) { 
     browser.driver.get(loginPageURL).then(function() { 
      browser.driver.sleep(2000); 
      wait('#username', 10000); 
      done(); 
     }); 
    }); 

    it('It should Login a User', function (done) { 
     element(by.id('username')).sendKeys(constants.userName); 
     element(by.id('password')).sendKeys(constants.password); 
     element(by.id('Login')).click().then(function() { 
      // Waiting to open modal 
      wait('#element_on_the_next_page', 10000); 

      browser.getCurrentUrl().then(function (url) { 
       // do anything with the url 
       done(); 
      }); 
     }); 
    }); 

    function wait(selector, timeout) { 
     browser.driver.wait(function() { 
     return browser.driver.isElementPresent(by.css(selector)).then(function(present) { 
      return present; 
     }); 
     }, timeout); 
     browser.driver.wait(function() { 
     return browser.driver.findElement(by.css(selector)).isDisplayed().then(function(displayed) { 
      return displayed; 
     }); 
     }, timeout).then(function() { 
     return; 
     }); 
    } 
}); 

希望这有助于!

+0

足够接近,但超时后,如果该元素没有显示或存在,我不想移动进一步的测试套件。因为我的登录测试失败。怎么做?现在,当我的登录测试失败时,它也移动到更多的测试套件上,我已经在conf.js的spec属性中定义了 –

+0

如果这是您的愿望,请在此处查看此答案以使套件在初始失败后失败: https://stackoverflow.com/a/34462368/5623572 – GoLGo13