量角器脚本无法正常工作 [英] Protractor script doesn't work properly

查看:92
本文介绍了量角器脚本无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我正在尝试做的事情

Here is exactly what I'm trying to do

我打开一个页面,其中包含一个包含用户信息的表格

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

我的元素getText()表示表中的用户数量(fe列表中的11个用户)

I getText() of element that indicates a number of user in the table (f.e. "11 Users in list")

我删除了列表中的用户part并将字符串转换为整数以便稍后用于for循环

I remove " Users in list" part and convert a string into integer to use later in for loop

我需要按用户名(第9列)找到某个用户并获取 j 数字,这是该用户信息所在的行数(这是我被卡住的地方)

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)

我转到<第一列 j row(它将成为此特定用户的编辑按钮)并单击它以验证用户的类型

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

我试过的代码的一个示例

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

这是我得到的

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

因此即使用户名不同,它也会计数但总是返回true(似乎检查每次第一行)。我尝试了另一种方法

So it does count but always returns true even though username are different (seem to check first row everytime). 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");
            }       
        });     
    }       
});

我得到

true
11
false
false
false
false
false
false
false
false
false

现在它访问每一行似乎很好,但是在第一行它返回第11行。出了什么问题?如何解决?

Now it accesses each row and seem to be fine, but on the first row it returns number 11. What's wrong? How to fix it?

P.S。我将举例说明我正在做什么
页面截图
我想找到我的用户并点击与之关联的编辑按钮。我只需单击第6列第一列中的单元格即可。但是如果要添加更多用户,我想让它变得可靠。为此,我需要搜索用户名列,记录该用户在表中所在行的编号,然后导航到我找到的行号第一列中的单元格。

P.S. 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.

推荐答案

对我而言简单方法是访问我在表中寻找的用户tr,然后单击此行中的按钮编辑。两个例子

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

要理解的重要部分是第二个xpath中的一个点,表示当前节点。没有这个点//字面意思是指页面上的任何地方vs.//,这意味着从我已经选择的元素开始

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.

这篇关于量角器脚本无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆