本文介绍了如何使用onkeyup / onfocus javascript在文本字段中输入文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java,Web Driver和TestNG自动化测试的Web应用程序。我面临的最大挑战(失败)是保持领先于开发并防止测试框架因代码更改而中断。

We have a web application that I am trying to automate testing for, using Java, Web Driver and TestNG. The biggest challenge I'm facing (failing at) is to stay ahead of development and prevent the test framework from breaking with even minor code changes.

问题


  • 有一个接受数字的文本字段。到目前为止, sendKeys 为我做了这份工作。

  • 在最近更改页面源时,输入数字时,逗号自动插入。

  • 所以50000变成50,000例如。

  • 现在 sendKeys 正确输入值,但在我离开该字段以编辑其他字段后,该值将重置为0.。

  • There's a text field that accepts numbers. Up till now, sendKeys did the job for me.
  • In a recent change to the page source, when numbers are entered, commas get automatically inserted.
  • So "50000" becomes "50,000" for example.
  • Now sendKeys enters the value correctly, but after I move out of the field to editing other fields, the value resets to 0.

这是元素的来源。注意 onkeyup的值 onfocus

Here's the source for the element. Note the values for onkeyup and onfocus:

<input type="text" class="form-control tooltip-default" id="my_budget" value="" data-mask="fdecimal" data-rad="." data-toggle="tooltip" data-placement="top" data-original-title="test tooltip" onfocus="removeErrors('my_budget');" onkeyup="removeNegativeSign('my_budget');">

我之前遇到过这个问题,并尝试使用和其他研究。

I've faced this issue before, and tried to hack a solution using W3Schools and other research.

方法1:Keys.TAB

sendKeys(Keys)可以触发一个keyup事件,所以我尝试发送 Keys.TAB

I've read that sendKeys(Keys) can trigger a keyup event, so I tried sending Keys.TAB

driver.findElement(By.id("my_budget")).sendKeys(Keys.TAB);

这没有任何效果。

方法2:发送keyDown-keyUp

我使用了问题作为参考,只是模拟按下Ctrl键。

I used this question as a reference, and just simulated pressing the Ctrl-key.

    new Actions(driver).keyDown(driver.findElement(By.id("my_budget")), Keys.CONTROL).keyUp(driver.findElement(By.id("my_budget")), Keys.CONTROL).perform();

这没有任何效果。

方法3:手动执行Javascript

最后,我尝试手动执行Javascript,首先是 onfocus ,然后当它不起作用时, onkeyup

Finally, I tried manually executing the Javascript, first for onfocus, then when that didn't work, for onkeyup.

    SeleniumHelper.doJavascriptOnElement(driver, driver.findElement(By.id("my_budget")), driver.findElement(By.id("my_budget")).getAttribute("onfocus"));

    SeleniumHelper.doJavascriptOnElement(driver, driver.findElement(By.id("my_budget")), driver.findElement(By.id("my_budget")).getAttribute("onkeyup"));

这导致错误,表明找不到Javascript:

This caused errors that indicate that the Javascript was not found:

Caused by: java.lang.Exception: org.openqa.selenium.WebDriverException: unknown error: removeErrors is not defined

问题

所以 -


  • 如何在onkeyup或onfocus(或其他)Javascript处于活动状态的文本字段中输入文本?

  • 我如何编程将来这种情况(代码在我身上发生了变化)?是否有一个我不知道的智能sendKeys?

推荐答案

我找到了挖掘一天后的答案。

And I found the answer after a day of digging.

使用 onfocus在文本字段中输入文字 Javascript,我需要到 click() text-field webelement:

To enter text in text-fields with an onfocus Javascript, I need to click() the text-field webelement:

driver.findElement(By.id("my_budget")).click();

这很有效 - 在输入值时执行Javascript。

This worked - the Javascript executed upon entering the value.

为了让输入文字+生成事件更容易,我创建了一个方法:

To make entering text + generating events a little easier, I have created a method:

要调用此方法:

sendKeysWithEvent(driver, driver.findElement(By.id("my_budget")), "50000", "onfocus");

方法:

public static void sendKeysWithEvent(WebDriver driver, WebElement element, String text, String event) throws Exception {
    element.sendKeys(text);
    switch(event) {
    case "keyup":
        new Actions(driver).keyDown(element, Keys.CONTROL).keyUp(element, Keys.CONTROL).perform();
        break;
    case "onblurJS":
        doJavascriptOnElement(driver, element, element.getAttribute("onblur"));
        break;
    case "onfocus":
        element.click();
        break;
    case "keyupJS":
        doJavascriptOnElement(driver, element, element.getAttribute("onkeyup"));
        break;
    case "keyupTAB":
        element.sendKeys(Keys.TAB);
        break;
    }

}
public static void doJavascriptOnElement(WebDriver driver, WebElement element, String javascript) throws Exception {
    ApiHelper.doLog("Running Javascript: " + javascript);
    ((JavascriptExecutor) driver).executeScript(javascript, element);

}

这篇关于如何使用onkeyup / onfocus javascript在文本字段中输入文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:53