WebElement.clear()触发javascript更改事件-替代方法? [英] WebElement.clear() fires javascript change event - Alternatives?

查看:83
本文介绍了WebElement.clear()触发javascript更改事件-替代方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Selenium IDE最初记录测试并将其保存为Java WebDriver测试.

I use selenium IDE to initially record the tests and save them as Java WebDriver tests.

当我进入一个输入字段时,删除所有文本并输入一个新值,它将记录为2条命令:

When I go into an input field, delete all the text and enter a new value, it records that as 2 commands:

driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys("johnnyleitrim");

对此我的一个问题是clear()事件触发用户名"字段的Javascript更改事件.当我使用浏览器本身时,不会发生这种情况-它会等到该字段失去焦点后才触发change javascript事件,而这正是我要在Selenium中模拟的内容.

One problem with this for me is that the clear() event fires a Javascript change event for the "username" field. This does not happen when I use the browser itself - it waits until the field loses focus before firing the change javascript event, and that's what I want to emulate in Selenium.

我需要这样做的原因是,我对change()事件进行了验证,当使用空值调用change时,它会显示一条警报,告知用户该信息无效-该警报会停止Selenium

The reason I need this is that I do validation on the change() event, and when change is called with an empty value, it displays an alert telling the user the information is invalid - and this alert stops Selenium

那么如何不使用WebElement.clear()清除字段?

So how do I clear the field without using WebElement.clear()?

推荐答案

似乎是已知的硒错误.在错误页面上有一些解决方法,但是它们都意味着必须大量"修改从Selenium IDE返回的代码.相反,我决定创建一个代理服务器,无需过多修改IDE生成的代码即可为我完成工作:

Seems like it's a known Selenium bug. There were a few options as workarounds mentioned on the bug page, but they all meant having to "heavily" modify the code returned from Selenium IDE. Instead, I decided to create a Proxy which would do the work for me without too much modification to the IDE generated code:

protected WebElement findElement(By criteria) {
    try {
        WebElementHandler webElementHander = new WebElementHandler(seleniumWebDriver.findElement(criteria));
        return (WebElement) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{WebElement.class}, webElementHander);
    } catch (NoSuchElementException e) {
        logger.error("Could not find " + criteria + " on page " + seleniumWebDriver.getCurrentUrl());
        throw e;
    }
}

private class WebElementHandler implements InvocationHandler {
    private WebElement proxiedElement;

    private WebElementHandler(WebElement proxiedElement) {
        this.proxiedElement = proxiedElement;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (method.getName().equals("clear")) {
            Keys[] keys = new Keys[proxiedElement.getAttribute("value").length()];
            for (int i = 0; i < keys.length; i++)
                keys[i] = Keys.BACK_SPACE;

            proxiedElement.sendKeys(Keys.chord(keys));
            return null;
        }
        return method.invoke(proxiedElement, args);
    }
}

这篇关于WebElement.clear()触发javascript更改事件-替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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