我试图从一行上的表格单元格中读出值,以按预期检查值是否匹配。
我面临的情况是 Protractor 无法获取我感兴趣的特定单元格的文本,因为它找不到我正在查询的单元格。
这是我的测试套件步骤:
beforeAll(function() {
common.journeyValidationBtn.click();
helper.waitForUrlToContain('journey-validation');
helper.waitForElementToHide(common.spinner);
common.fabBtn.click();
helper.waitForPresenceOf(common.calculateBtn);
common.runCalculation(secondaryPeriod);
helper.waitForPresenceOf(common.fabBtn, 30000);
helper.waitForElementToHide(common.spinner);
common.selectComparisonPeriod(initialPeriod);
helper.waitForTableRows(page.reimbursementCalculations).then(function() {
console.log('*** Table Rows Found - promise resolved ***');
page.reimbursementCalculations.count().then(function(count){
console.log('*** Number of Table Rows Found - ' + count + ' ***');
row = page.reimbursementCalculations.first();
cells = row.all(by.css('td'));
cells.count().then(function(cellCount){
console.log('*** Number of Table Cells Found - ' + cellCount + ' ***');
cells.get(0).getText().then(function(text){
console.log('*** First Table Cell Text - ' + text + ' ***');
});
});
});
}, function(reason){
console.log('*** Table Rows Not Found - promise rejected ***');
console.log(reason);
});
});
it('Then I should get more than one cell returned', function() {
expect(cellCount.count()).toBeGreaterThan(0);
});
在运行测试时,我看到一些 Jasmine 超时错误,然后是我期望的失败,它无法对在 beforeAll 块中分配的单元格变量调用 count() 。也许这个超时与我所看到的有关,但我正在努力进一步调试。
[12:20:01] I/testLogger - [chrome #01-6] PID: 9609
[chrome #01-6] Specs: /var/jenkins/workspace/project_x/test/e2e/features/featureA/workflow/SW0005.spec.js
[chrome #01-6]
[chrome #01-6] [12:18:56] I/direct - Using ChromeDriver directly...
[chrome #01-6] Started
[chrome #01-6] *** Comparison period selected ***
[chrome #01-6] [12:19:07] W/element - more than one element found for locator By(css selector, .spinnerContainer) - the first result will be used
[chrome #01-6] [12:19:41] W/element - more than one element found for locator By(css selector, .spinnerContainer) - the first result will be used
[chrome #01-6] *** Table Rows Found - promise resolved ***
[chrome #01-6] *** Number of Table Rows Found - 1 ***
[chrome #01-6] *** Number of Table Cells Found - 11 ***
[chrome #01-6] *** First Table Cell Text - blaa ***
[chrome #01-6] A Jasmine spec timed out. Resetting the WebDriver Control Flow.
[chrome #01-6] [31mF[0mA Jasmine spec timed out. Resetting the WebDriver Control Flow.
[chrome #01-6] [31mF[0mA Jasmine spec timed out. Resetting the WebDriver Control Flow.
[chrome #01-6] [31mF[0mA Jasmine spec timed out. Resetting the WebDriver Control Flow.
[chrome #01-6] [31mF[0mA Jasmine spec timed out. Resetting the WebDriver Control Flow.
[chrome #01-6] [31mF[0mA Jasmine spec timed out. Resetting the WebDriver Control Flow.
[chrome #01-6] [31mF[0mA Jasmine spec timed out. Resetting the WebDriver Control Flow.
[chrome #01-6] [31mF[0mA Jasmine spec timed out. Resetting the WebDriver Control Flow.
[chrome #01-6] [31mF[0mA Jasmine spec timed out. Resetting the WebDriver Control Flow.
[chrome #01-6] [31mF[0mA Jasmine spec timed out. Resetting the WebDriver Control Flow.
[chrome #01-6] [31mF[0mA Jasmine spec timed out. Resetting the WebDriver Control Flow.
[chrome #01-6] [31mF[0mA Jasmine spec timed out. Resetting the WebDriver Control Flow.
[chrome #01-6] Failures:
[chrome #01-6] Message:
[chrome #01-6] [31m Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.[0m
[chrome #01-6] Stack:
[chrome #01-6] Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
[chrome #01-6] at ontimeout (timers.js:365:14)
[chrome #01-6] at tryOnTimeout (timers.js:237:5)
[chrome #01-6] at Timer.listOnTimeout (timers.js:207:5)
[chrome #01-6] Message:
[chrome #01-6] [31m Failed: Cannot read property 'count' of undefined[0m
[chrome #01-6] Stack:
[chrome #01-6] TypeError: Cannot read property 'count' of undefined
从我添加的控制台日志中可以看出, Protractor 能够找到一行并检索我分配给我的单元格属性的单元格(控制台日志显示 11 个单元格),但似乎这在预期的时候变得不确定跑。 cell 属性也在我的 beforeAll 之外定义,所以我认为在执行期望检查时这将保持分配状态而不是未定义。
这给我以后的测试带来了问题,我想读取和检查每个单元格的值。
我觉得我在做一些根本错误的事情,有人可以帮忙吗?
谢谢
最佳答案
默认 Jasmine beforeAll
, beforeEach
, afterEach
, afterAll
和 it
将期望超时变量作为第二/第三个参数。尽管没有明确提及您的示例,但请阅读 timeout-parameter of jasmine here
尽管您的回调来自您的辅助函数,但我的假设是,您的原因函数使其失败,因为 Jasmine 可能不支持 JavaScript 的所有方面。
尝试没有原因功能:
beforeAll(function() {
//all your test code
}); //don't add ", function(reason){etc});
});
it('Then I should get more than one cell returned', function() {
expect(cells.count()).toBeGreaterThan(0);
});
进一步说明,在这里你忘记重新设置“计数”,这就是为什么你的表格行和表格单元格是相同的数字:
cells = row.all(by.css('td'));
cells.count().then(function(count2){ //Here the "count" is missing
console.log('*** Number of Table Cells Found - ' + count2 + ' ***');
更新
你是对的,回调是你自己的函数,而不是助手。
我现在刚刚看到你有错误“无法读取'undefined'的属性'count'”。因此您的对象
page.reimbursementCalculations
可能是空的。目前这两行记录了相同的变量:
console.log('*** Number of Table Rows Found - ' + count + ' ***');
console.log('*** Number of Table Cells Found - ' + count + ' ***');
关于angularjs - 在套件运行期间努力保持对表格单元格的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47307617/