Protractor script doesn't work properly

后端 未结 2 589
后悔当初
后悔当初 2020-12-22 05:14

Here is exactly what I\'m trying to do

I open a page with a table that contains information about users

I getText() of element that indicates a number of use

相关标签:
2条回答
  • 2020-12-22 05:51

    Easy way for me was accessing tr of the user I was looking for in the table and then clicking the button edit in this row. Two examples

    $username26="testuser26"
    element.all(by.xpath('//trLocator')).first().element(by.xpath("//tr[.//td[@data-field='username' and text()="+$username26+"]]")).element(by.xpath(".//a[text()='Edit']")).click()
    
    element(by.xpath("//td[@data-field='username' and text()='"+$username26+"']/..")).element(by.xpath(".//a[text()='Edit']")).click();
    

    The important part to understand is a dot in the second xpath which means the current node. Without this dot "//" literally mean anywhere on the page vs ".//" which means starting from the element I've already selected

    Now this is the long way, but this was the logic I was looking for

    var tableRows = element.all(by.xpath('xpathTableRowsLocator')); //returns 11 rows
    var mentricsLogo =  element(by.css('a.logo'));
    var searchedUsername = "TESTUSER26";
    
    //somehow the code didn't work by itself so I had to place it inside of random function whuch I didn't care about
    mentricsLogo.isPresent().then(function(result) {
            return tableRows.filter(function(eachRow, index) {
    //get text of the whole row and separate it by columns
                return eachRow.getText().then(function(text){
                    text=text.split(" ")
    // I get third element is username
                    if (text[2]===searchedUsername){
                        var xpathIndex = Number(index)+1
                        element(by.xpath("//*[@id='user-list-content']/div[2]/div[2]/div[1]/div[2]/div[1]/table/tbody/tr["+xpathIndex+"]/td[1]/a")).click()
                    }
                })
            });
    });
    

    I know it may look not rationally, but nothing else worked for me.

    0 讨论(0)
  • 2020-12-22 06:03

    To be honest I didn't what are you checking.

    First of all you should consider using

    element.all(by.xpath("(//td[@data-field='username'])")) instead of element(by.xpath("(//td[@data-field='username'])["+j+"]"))

    It returns elementArrayFinder. Assign it to a variable (eg. tableRows) and then you can handle with those data.

    If you are trying to check whether the number in a string "11 Users in list" is correct, yuu can use tableRows.count()

    You can also filter elements: http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.filter

    use map: http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.map

    Drop me a line what exactly test should do.

    Edit:

    Ok. Here you have code sample. It clicks EDIT link of defined user (TESTUSER26 in this case): The only change you have to make is change defineSelectorForUsernameCell into real value.

    var tableRows = element.all(by.css('td'));
    var searchedUsername = 'TESTUSER26';
    
    return tableRows.filter((eachRow) => {
        return eachRow.element(by.css('defineSelectorForUsernameCell')).getText((usernameCellText) => {
            if (usernameCellText === searchedUsername) {
                return eachRow.element(by.linkText('EDIT')).click();
                }
        });
    });
    
    0 讨论(0)
提交回复
热议问题