首页 > 解决方案 > 量角器角度 5 单选按钮选择无法始终如一地工作

问题描述

我正在尝试使用角度为 5 的量角器来选择与客户付款方式相关的单选按钮,但是单选按钮选择一旦选中就不稳定,十次则不稳定。在网络结帐中单击单选按钮的代码:

 browser.driver.findElement(by.xpath("//input[@name='paymentMethod' and @type='radio' and @id='874904645420181210']"));
 browser.driver.actions().mouseMove(element(by.xpath("//input[@name='paymentMethod' and @type='radio' and @id='874904645420181210']"))).perform();
 var e = element(by.xpath("//input[@name='paymentMethod' and @type='radio' and @id='874904645420181210']"));
 browser.wait(EC.presenceOf(e), 10000);
 element.all(by.xpath("//input[@name='paymentMethod' and @type='radio'")).then( async function(elm){
 await browser.waitForAngular();
 await browser.sleep(180000);
 await elm[0].click();
 await e.click();
 await browser.waitForAngular();
 await browser.sleep(180000);

标签: angulardomjasmineprotractor

解决方案


当您调用 时browser.driver.findElement,它会在不等待 Angular 稳定的情况下找到元素。基本上你是直接调用 selenium-webdriver 客户端,你的结果可能会有所不同。

我会避免使用显式睡眠和调用 waitForAngular。还要考虑不使用 xpath 来识别您的 Web 元素。这可能导致难以维护测试。

// Nit: Prefer not to use xpath because these can be brittle.
// Reference http://www.protractortest.org/#/style-guide
const paymentMethod = element(by.xpath("//input[@name='paymentMethod' and @type='radio' and @id='874904645420181210']"));

// We should wait to see if the element is present before doing anything.
// This should also wait for angular stability with waitForAngular
browser.wait(EC.presenceOf(paymentMethod), 10000);

不同的点击方式...

使用 selenium-webdriver 操作 API

注意:您可能会在此处遇到问题,因为 W3C 操作 API 与浏览器驱动程序提供程序和 selenium-webdriver 客户端不断变化。

// browser.driver.actions() mouseMove takes a WebElement object.
// So just moving the mouse does not click. You might want to click on it?
// Also assuming that this is the radio button
const paymentMethodWebEl = await paymentMethod.getWebElement();
await browser.driver.actions().mouseMove(paymentMethodWebEl).click().perform();

量角器的 ElementFinder 单击

相当标准的点击。

await paymentMethod.click();

Protractor 的 ElementArrayFinder:获取索引并单击。

// This might be the same step as above to click on a payment radio button
const paymentMethods = element.all(by.xpath("//input[@name='paymentMethod' and @type='radio'"));
// http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.get
await paymentMethods.get(0).click();

推荐阅读