【发布时间】:2019-08-14 16:48:24
【问题描述】:
我有一个包含 2 列和 n 行的动态表。在第一列中会有一个标题,在第二列中它将是进入该标题详细信息的按钮。
我想遍历每一行以找到特定的文本标题,然后单击该标题的第二列按钮。
我尝试了很多方法搜索谷歌,但找不到任何东西。
在 HTML 中的 AngularJS 代码如下。
<table>
<tbody>
<tr ng-repeat="registration in Registrations | orderBy:'title'" class="tableRow ng-scope">
<td style="padding-left: 20px;" ng-if="reg.title != 'Jabc'" class="ng-binding ng-scope">test</td>
<td style="padding-left: 20px;">
<button ng-click="enter(registration)" class="ng-binding">Enter community</button>
</td>
<tr ng-repeat="registration in Registrations | orderBy:'title'" class="tableRow ng-scope">
<td style="padding-left: 20px;" ng-if="reg.title != 'Jabc'" class="ng-binding ng-scope">test1</td>
<td style="padding-left: 20px;">
<button ng-click="enter(registration)" class="ng-binding">Enter community</button>
</td>
<tr ng-repeat="registration in Registrations | orderBy:'title'" class="tableRow ng-scope">
<td style="padding-left: 20px;" ng-if="reg.title != 'Jabc'" class="ng-binding ng-scope">test2</td>
<td style="padding-left: 20px;">
<button ng-click="enter(registration)" class="ng-binding">Enter community</button>
</td>
</tbody>
</table>
如上表所示,共有三行。我想按名称选择行以假设“test1”并想单击同一行的第二列按钮。
在这里,我通过编写下面的代码得到了我正在寻找的特定文本的索引。
let elm = element.all(by.repeater('registration in myRegistrations'));
let index;
elm.getText().then(function(text){
console.log(text);
for(var i=0; i<text.length; i++){
if(text[i] == 'protractor-testing Enter community'){
index = i;
console.log('Community found :: ' + text[i]);
}
}
});
console.log("Index :: " + index);
现在,通过这个索引,我可以点击表格中的按钮 row = index 和同一行的第二列吗?
下面是我的整个脚本。
conf.js 文件..
// An example configuration file
var Jasmine2HtmlReporter = require('protractor-jasmine2-html-reporter');
exports.config = {
// The address of a running selenium server.
seleniumAddress: 'http://localhost:4444/wd/hub',
baseUrl: 'http://localhost:' + (process.env.PORT || '9000'),
// Capabilities to be passed to the webdriver instance.
capabilities: {
browserName: 'chrome'
},
params: {
Firstname: 'virus',
Lastname: 'virus',
Email: 'virus@email.com',
Username: 'virus',
Password: 'virus',
TempUsername : 'temp',
TempPassword: 'temp',
AdminUsername: 'admin',
AdminPassword: 'admin',
CreateTitle: 'protractor-testing',
CreateKey: 'protractor-testing'
},
// Spec patterns are relative to the configuration file location passed
// to protractor (in this example conf.js).
// They may include glob patterns.
// specs: ['../spec/2 Login/spec.js'],
suites: {
login: '../spec/2 Login/spec.js'
},
// Options to be passed to Jasmine-node.
jasmineNodeOpts: {
showColors: true, // Use colors in the command line report.
},
// onPrepare: function(){
// jasmine.getEnv().addReporter(new Jasmine2HtmlReporter({
// savePath: '../test/reports'
// })
// );
// },
};
下面是我的 spec.js 文件
describe('Testing', function(){
it('perform login', function() {}
browser.get(browser.baseUrl);
browser.manage().window().maximize();
console.log("Admin :: " + browser.params.AdminUsername + " " + browser.params.AdminPassword);
element(by.model('user.userName')).sendKeys(browser.params.AdminUsername);
element(by.model('user.password')).sendKeys(browser.params.AdminPassword);
browser.sleep('2000');
element(by.buttonText('Login')).click();
element(by.model("newCommunity.title")).sendKeys(browser.params.CreateTitle);
element(by.model("newCommunity.key")).sendKeys(browser.params.CreateKey);
let buttons = element.all(by.css("button.ng-binding"));
let tds = element.all(by.css("td.ng-binding"));
console.log("Buttons :: " + buttons);
console.log("tds :: " + tds);
// var buttons = driver.findElements(By.css("button.ng-binding"));
// var tds = driver.findElements(By.css("td.ng-binding"));
for (var i = 0; i < tds.length; i++) {
console.log("get text for index " + i + " Value :: " + tds[i].getText());
if (tds[i].getText() == 'protractor-testing') {
console.log("Got the actual community at : " + i);
console.log("Clicking button :: " + buttons[i]);
buttons[i].click();
break;
}
}
});
})
请注意,我不想使用 xPath。
【问题讨论】:
标签: angularjs selenium selenium-webdriver css-selectors protractor