简体   繁体   English

量角器脚本无法正常工作

[英]Protractor script doesn't work properly

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 user in the table (fe "11 Users in list") 我的getText()元素指示表中的用户数(例如“列表中的11个用户”)

I remove " Users in list" part and convert a string into integer to use later in for loop 我删除“列表中的用户”部分,并将字符串转换为整数,以便稍后在for循环中使用

I need to find certain user by username (9th column) and get j number which is a number of a row this user's information is in (this is where I got stuck) 我需要通过用户名(第9列)找到某个用户,并获取j号,该数字是该用户信息所在的行数(这就是我被卡住的地方)

I go to first column of j row (its going to be edit button for this particular user) and click it to verify type of the user 我转到j行的第一列(该行将为此特定用户的编辑按钮),然后单击它以验证用户类型

One example of a code I tried 我尝试过的一个代码示例

it("Validation of ND account", function() {
    //login
    element(by.id("j_username")).sendKeys($usernameND);
    element(by.id("j_password")).sendKeys($passwordND);
    element(by.buttonText("Login")).click();
    //navigate to a page with list of users displayed in a table
    element(by.xpath('//a[@short-title="Users"]')).click();
    //narrow the search result by typing 'testuser'
    element(by.model("userList.searchBox.options.query")).sendKeys($usernameND);
    //the table has 11 results such as 'testuser', 'testuser1', 'testuser2'
    //I get text of element with text '11 users in list' to get just the number out of it
    element(by.xpath('//div[@viewid="userList"]//div[@class="results-count ng-binding"]')).getText().then(function(numberOfUsers) {
        numberOfUsers = Number(numberOfUsers.replace(" Users in list",""));
        // declare a variable which will be responsible for accessing to j row in a table
        var j=1
        //I search through 11 rows from the table for my username
        for (i=1; i<numberOfUsers; i++) {

            element(by.xpath("(//td[@data-field='username'])["+j+"]")).getText().then(function(username){   

                if (username===$usernameND){
                    console.log("true");
                    console.log(j);
                    j++
                } else {
                    console.log("false");
                    j++
                }   
            }); 
        }   
    });     
});

this is what I get 这就是我得到的

true
1
true
2
true
3
true
4
true
5
true
6
true
7
true
8
true
9
true
10

So it does count but always returns true even though username are different (seem to check first row everytime). 因此它确实很重要,但是即使用户名不同也总是返回true(似乎每次都要检查第一行)。 I try another approach 我尝试另一种方法

element(by.xpath('//div[@viewid="userList"]//div[@class="results-count ng-binding"]')).getText().then(function(numberOfUsers) {
    numberOfUsers = Number(numberOfUsers.replace(" Users in list",""));

    for (i=1; i<numberOfUsers; i++) {   
        element(by.xpath("(//td[@data-field='username'])["+i+"]")).getText().then(function(username){   

            if (username===$usernameND){
                console.log("true");
                console.log(i);
            } else {
                console.log("false");
            }       
        });     
    }       
});

I get 我懂了

true
11
false
false
false
false
false
false
false
false
false

Now it accesses each row and seem to be fine, but on the first row it returns number 11. What's wrong? 现在,它访问每一行,看起来还不错,但是在第一行中,它返回数字11。这是怎么回事? How to fix it? 如何解决?

PS I will give an example what I'm doing screenshot of the page I want to find my user and to click 'edit' button associated with it. 附言:我将举一个例子,说明我正在要查找用户并单击与之关联的“编辑”按钮的页面截图 I can just simply click on a cell in first column 6th row. 我只需在第一列第六行中单击一个单元格即可。 But I want to make it reliable in case if more users will be added. 但是,如果要添加更多用户,我想使其可靠。 To do this I need to search through username column, document the number of the row this user is located in the table, and then navigate to a cell in the first column on the row number I found. 为此,我需要搜索用户名列,记录该用户在表中所在的行号,然后导航到找到的行号第一列中的单元格。

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+"]")) element.all(by.xpath("(//td[@data-field='username'])"))而不是element(by.xpath("(//td[@data-field='username'])["+j+"]"))

It returns elementArrayFinder. 它返回elementArrayFinder。 Assign it to a variable (eg. tableRows ) and then you can handle with those data. 将其分配给变量(例如tableRows ),然后可以处理这些数据。

If you are trying to check whether the number in a string "11 Users in list" is correct, yuu can use tableRows.count() 如果您要检查字符串“ 11 Users in list”中的数字是否正确,则yuu可以使用tableRows.count()

You can also filter elements: http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.filter 您还可以filter元素: http : //www.protractortest.org/#/api?view=ElementArrayFinder.prototype.filter

use map : http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.map 使用maphttp : //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. 它单击已定义用户的EDIT链接(在这种情况下为TESTUSER26):唯一要做的更改是将defineSelectorForUsernameCell更改为实值。

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();
            }
    });
});

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. 对我来说,简单的方法是访问表中要查找的用户的tr,然后单击此行中的“编辑”按钮。 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. 要理解的重要部分是第二个xpath中的一个点,表示当前节点。 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. 我知道它看起来可能不合理,但没有其他对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM