我想点击一个单选按钮,出现在网页上。代码如下:
HTML code:
<div class="small-checkbox red-theme raleway-regular text-muted2 position-relative">
<div class="city-checkbox inline-block position-relative" ng-class="{'rounded-checkbox': main.current_city_id == 1, 'mb-20': main.ifDeviceIsPhone}">
<label class="mdl-radio mdl-js-radio mdl-js-ripple-effect mh-20" for="mumbaiCity" ng-class="{'is-checked' : main.current_city_id == 1}">
<input type="radio" id="mumbaiCity" class="mdl-radio__button position-relative vertical-middle" name="city" value="1" ng-click="main.setCity('Mumbai', 1)">
<span class="mdl-radio__label position-relative font15"><img class="city-icon" src="../../../assets/img/cities/mumbai-icon.png">Mumbai</span>
</label>
</div>
</div>
Tesstcase:
// demo-test.js
describe('Protractor Demo App', function() {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000000;
it('check item count', function() {
browser.get('<link>');
element(by.id('mumbaiCity')).click();
});
});
此测试通过错误:
1)量角器演示App检查项目计数
消息:
失败:元素不可见
我也尝试过:
element(by.css('[ng-click="main.setCity('Mumbai', 1)"]')).click();
它给出错误:
[16:16:26] E / launcher - 错误:SyntaxError:missing)参数列表
请建议单选按钮如何点击?
答案 0 :(得分:84)
这是硒测试自动化中一个相当普遍的问题。
以下是常见的解决方案:
wait了解元素的visibility :
var EC = protractor.ExpectedConditions;
var mumbaiCity = element(by.id('mumbaiCity'));
browser.wait(EC.visibilityOf(mumbaiCity), 5000);
mumbaiCity.click();
有一个另一个元素,其中id
实际上是不可见的。在这种情况下,您需要改进定位器以匹配此特定元素。例如:
element(by.css(".city-checkbox #mumbaiCity")).click();
element(by.css(".city-checkbox input[ng-click*=Mumbai]")).click();
或者,如果您有多个匹配同一个定位器的元素 - 您可以"filter"出一个可见元素:
var mumbaiCity = element.all(by.id('mumbaiCity')).filter(function (elm) {
return elm.isDisplayed().then(function (isDisplayed) {
return isDisplayed;
});
}).first();
mumbaiCity.click();
移至元素,然后点击browser.actions()
:
var mumbaiCity = element(by.id('mumbaiCity'));
browser.actions().mouseMove(mumbaiCity).click().perform();
scroll into view元素,然后点击:
var mumbaiCity = element(by.id('mumbaiCity'));
browser.executeScript("arguments[0].scrollIntoView();", mumbaiCity.getWebElement());
mumbaiCity.click();
点击通过javascript (谨防differences):
var mumbaiCity = element(by.id('mumbaiCity'));
browser.executeScript("arguments[0].click();", mumbaiCity.getWebElement());
有时,您只需要最大化浏览器窗口:
browser.driver.manage().window().maximize();