2017-06-14 36 views
1

我是量角器新手。下面是我的配置文件运行量角器测试时出现问题

config

exports.config = { 
seleniumAddress: 'http://localhost:4444/wd/hub', 

capabilities: { 
    'browserName': 'chrome' 
}, 

specs: ['./protractor-tests/*.js'], 

jasmineNodeOpts: { 
    showColors: true 
} 
} 

在这里,我有我在哪里哪里测试通过Outlook用户登录,当点击登录它重定向到主页上登录的情况。

登录测试:

exports.login = function() { 
describe('Login Test', function() { 
    beforeEach(function() { 
     browser.get('http://domain/login'); 
    }); 
    afterEach(function() { 
     browser.ignoreSynchronization = false; 
    }); 
    it('It should redirect to outlook for auth', function() { 
     browser.ignoreSynchronization = true; 
     var currentUrl; 
     browser.getCurrentUrl().then(function(url) { 
      currentUrl = url; 
     }).then(function() { 
      browser.wait(function() { 
       return browser.getCurrentUrl().then(function(url) { 
        return url !== currentUrl; 
       }); 
      }); 
     }).then(function() { 
      setTimeout(function() { 
       var userName = element(By.xpath('/html/body/div[1]/header/div[1]/section/div[2]/apt-user-profile/div/div/button[1]/span/span')); 
       if (userName) 
        expect(browser.getCurrentUrl()).toContain('http://domain/home'); 
       else 
        expect(browser.getCurrentUrl()).toContain('https://login.microsoftonline.com'); 
      }, 10000); 


     }); 
    }); 

    it('It should login into the application and display user as Tom White', function() { 
     setTimeout(function() { 
      browser.driver.findElement(by.id('cred_userid_inputtext')).sendKeys('[email protected]'); 
      browser.driver.findElement(by.id('cred_password_inputtext')).sendKeys('****'); 
      browser.driver.findElement(by.id('cred_sign_in_button')).click(); 
      var userName = element(By.xpath('/html/body/div[1]/header/div[1]/section/div[2]/apt-user-profile/div/div/button[1]/span/span')); 
      expect(userName.getText()).toEqual('Hello Tom White'); 
     }, 10000); 
    }); 

}); 
} 

这些测试案例实际上是工作正常,但如果在主页上,我还需要点击重定向我是错误

没有一些其他的网页元素上
Failed: javascript error: document unloaded while waiting for result 

家庭测试:

var loginPage = require('./loginTest.js'); 

describe('Home Test', function() { 
    loginPage.login(); 
    it('It should click product', function() { 

      var productMenu = element(By.xpath('/html/body/div[1]/headerdiv[1]/section/div[1]/apt-nav-bar/div/div[2]/div[2]/ul/li[2]/a/span')); 
      productMenu.click(); 
      expect(browser.getCurrentUrl()).not.toEqual('http://domain/home'); 
      expect(browser.getCurrentUrl()).toEqual('http://domain/products'); 


     }) 

}); 

任何帮助将高度赞赏。

回答

0

你的问题是由于没有等待页面做些什么。您可以使用量角器提供的ExpectionConditions功能实现明确的等待。以下是API的链接。

肮脏的解决方案是使用sleep()方法,以便执行暂停一段时间,例如5秒。所以你可以试试browser.sleep(5000),这应该会消除这个错误。但是,这不是一个好的解决方案。

编辑1:在量角器配置文件中定义的exports.config对象的参数必须与包含您的ng-app指令的元素匹配。

在你的量角器,conf.js,请加

rootElement: '.my-app', 

当你的HTML是:

<div ng-app="myApp" class="my-app"> 

来源:SO Post

+0

,如果我做browser.sleep(5000) 我m如果失败的错误:错误而等待量角器与页同步:“window.angular未定义 –

+0

更新的答案,请参阅上文。 – demouser123

0

您正在调用it之外的login功能或在量角器控制流程下的“之前”或“之后”功能。移动你的loginPage.login();呼叫beforeEach()beforeAll()

describe('Home Test', function() { 
    beforeEach(function() { 
     loginPage.login(); 
    }); 

    it('It should click product', function() { 
     var productMenu = element(By.xpath('/html/body/div[1]/headerdiv[1]/section/div[1]/apt-nav-bar/div/div[2]/div[2]/ul/li[2]/a/span')); 
     productMenu.click(); 
     expect(browser.getCurrentUrl()).not.toEqual('http://domain/home'); 
     expect(browser.getCurrentUrl()).toEqual('http://domain/products'); 
    }) 
}); 
相关问题