我想验证年份列是否有2017年。下面是屏幕截图和html代码段table
html代码:
<table class="htCore table table-condensed table-bordered table-hover"><colgroup><col style="width: 200px;"><col style="width: 200px;"><col style="width: 200px;"><col style="width: 200px;"><col style="width: 200px;"></colgroup><thead><tr><th><div class="relative"><span class="colHeader columnSorting">Year</span><div class="manualColumnMover"></div></div></th><th><div class="relative"><span class="colHeader columnSorting">Created Date</span><div class="manualColumnMover"></div></div></th><th><div class="relative"><span class="colHeader columnSorting">Modified Date</span><div class="manualColumnMover"></div></div></th><th><div class="relative"><span class="colHeader columnSorting">Created By</span><div class="manualColumnMover"></div></div></th><th><div class="relative"><span class="colHeader columnSorting">Modified By</span><div class="manualColumnMover"></div></div></th></tr></thead><tbody><tr><td class="htDimmed current fill">All</td><td class="fill">08-Jan-2017</td><td class="fill">08-Jan-2017</td><td class="htDimmed fill">dbo</td><td class="htDimmed fill">dbo</td></tr><tr><td class="htDimmed fill">1986</td><td class="">15-Feb-2017</td><td class="">15-Feb-2017</td><td class="htDimmed">Ankush Bhatia</td><td class="htDimmed">Ankush Bhatia</td></tr><tr><td class="htDimmed fill">1987</td><td class="">16-Feb-2017</td><td class="">16-Feb-2017</td><td class="htDimmed">Ankush Bhatia</td><td class="htDimmed">Ankush Bhatia</td></tr><tr><td class="htDimmed fill">1988</td><td class="">16-Feb-2017</td><td class="">16-Feb-2017</td><td class="htDimmed">Ankush Bhatia</td><td class="htDimmed">Ankush Bhatia</td></tr><tr><td class="htDimmed fill">2000</td><td class="">15-Feb-2017</td><td class="">15-Feb-2017</td><td class="htDimmed">Ankush Bhatia</td><td class="htDimmed">Ankush Bhatia</td></tr><tr><td class="htDimmed fill">2001</td><td class="">17-Mar-2017</td><td class="">17-Mar-2017</td><td class="htDimmed">Anupama Gopakumar</td><td class="htDimmed">Anupama Gopakumar</td></tr><tr><td class="htDimmed fill">2006</td><td class="">05-Apr-2017</td><td class="">05-Apr-2017</td><td class="htDimmed">Ankush Bhatia</td><td class="htDimmed">Ankush Bhatia</td></tr><tr><td class="htDimmed fill">2008</td><td class="">05-Apr-2017</td><td class="">05-Apr-2017</td><td class="htDimmed">Ankush Bhatia</td><td class="htDimmed">Ankush Bhatia</td></tr><tr><td class="htDimmed fill">2013</td><td class="">09-Feb-2017</td><td class="">09-Feb-2017</td><td class="htDimmed">Ankush Bhatia</td><td class="htDimmed">Ankush Bhatia</td></tr><tr><td class="htDimmed fill">2014</td><td class="">14-Jan-2017</td><td class="">14-Jan-2017</td><td class="htDimmed">Ankush Bhatia</td><td class="htDimmed">Ankush Bhatia</td></tr><tr><td class="htDimmed fill">2015</td><td class="">14-Jan-2017</td><td class="">14-Jan-2017</td><td class="htDimmed">Ankush Bhatia</td><td class="htDimmed">Ankush Bhatia</td></tr><tr><td class="htDimmed fill">2016</td><td class="">14-Jan-2017</td><td class="">14-Jan-2017</td><td class="htDimmed">Ankush Bhatia</td><td class="htDimmed">Ankush Bhatia</td></tr><tr><td class="htDimmed fill">2017</td><td class="">09-Jan-2017</td><td class="">09-Jan-2017</td><td class="htDimmed">Ankush Bhatia</td><td class="htDimmed">Ankush Bhatia</td></tr><tr><td class="htDimmed fill">2018</td><td class="">09-Jan-2017</td><td class="">09-Jan-2017</td><td class="htDimmed">Ankush Bhatia</td><td class="htDimmed">Ankush Bhatia</td></tr><tr><td class="htDimmed fill">2019</td><td class="">14-Jan-2017</td><td class="">14-Jan-2017</td><td class="htDimmed">Ankush Bhatia</td><td class="htDimmed">Ankush Bhatia</td></tr><tr><td class="htDimmed fill">2020</td><td class="">14-Jan-2017</td><td class="">14-Jan-2017</td><td class="htDimmed">Ankush Bhatia</td><td class="htDimmed">Ankush Bhatia</td></tr><tr><td class="htDimmed fill">2021</td><td class="">15-Jan-2017</td><td class="">15-Jan-2017</td><td class="htDimmed">Ankush Bhatia</td><td class="htDimmed">Ankush Bhatia</td></tr><tr><td class="htDimmed fill">2022</td><td class="">18-Jan-2017</td><td class="">18-Jan-2017</td><td class="htDimmed">Ankush Bhatia</td><td class="htDimmed">Ankush Bhatia</td></tr><tr><td class="htDimmed fill">2023</td><td class="">06-Feb-2017</td><td class="">06-Feb-2017</td><td class="htDimmed">Ankush Bhatia</td><td class="htDimmed">Ankush Bhatia</td></tr><tr><td class="htDimmed fill">2025</td><td class="">06-Feb-2017</td><td class="">06-Feb-2017</td><td class="htDimmed">Ankush Bhatia</td><td class="htDimmed">Ankush Bhatia</td></tr><tr><td class="htDimmed fill">2026</td><td class="">06-Feb-2017</td><td class="">06-Feb-2017</td><td class="htDimmed">Ankush Bhatia</td><td class="htDimmed">Ankush Bhatia</td></tr><tr><td class="htDimmed fill">2027</td><td class="">10-Feb-2017</td><td class="">10-Feb-2017</td><td class="htDimmed">Rizwana Haji</td><td class="htDimmed">Rizwana Haji</td></tr></tbody></table>
我已尝试过以下解决方案但未按预期工作;有人可以看看吗?
browser.driver.findElement(by.css('.htCore.table.table-condensed.table-bordered.table-hover')).then(function (table) {
table.findElement(by.tagName('tbody')).then(function (tbody) {
tbody.findElements(by.tagName('tr')).then(function (rows) {
for (var i = 0; i < rows.length; i++) {
rows[i].findElements(by.tagName('td')).then(function (cols) {
expect(cols[1].getText()).toMatch('2017');
});
}
});
});
});
答案 0 :(得分:3)
正如聊天中所讨论的那样:
请在下面找到可行的代码: 消极的道路:
var year = element(by.xpath("//td[text()='20179']"));
if(expect(element(by.xpath("//td[text()='20179']")).isPresent()).toBe(true))
{
year.getText().then(function(x){
console.log("Year Prevails: " + x);
});
}
else
{
console.log("Entered Year missing")
}
积极路径:
var year = element(by.xpath("//td[text()='2017']"));
if(expect(element(by.xpath("//td[text()='2017']")).isPresent()).toBe(true))
{
year.getText().then(function(x){
console.log("Year Prevails: " + x);
});
}
else
{
console.log("Entered Year missing")
}
答案 1 :(得分:1)
你能试试吗?
expect(browser.driver.findElement(by.css('table tbody tr td:contains(“2017”)'))。isDisplayed())。toBe(true);
当没有任何内容时,这将导致测试用例失败,或者只是发现确切的元素然后通过。
添加评论中给出的答案。
您也可以尝试这些选择器。
by.deepCss('td:contains("2017")')
or
by.cssContainingText('table tbody tr td' , '2017')
希望这会有所帮助。