问题
i want to verify whether the year column is having 2017 or not. below is the screenshot and html snippet table
html code:
<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>
i have tried below solution but not working as expected; can someone take a look?
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');
});
}
});
});
});
回答1:
As discussed in chat:
Please find below workable code: Negative path :
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")
}
Positive path :
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")
}
回答2:
Can you try this?
expect(browser.driver.findElement(by.css('table tbody tr td:contains("2017")')).isDisplayed()).toBe(true);
This will throw either fail of test case when nothing is there or just find that exact element then pass.
Adding the answers given in comments too.
You can try these selectors too.
by.deepCss('td:contains("2017")')
or
by.cssContainingText('table tbody tr td' , '2017')
Hope this helped.
来源:https://stackoverflow.com/questions/43245836/verify-table-data-in-protractor