From: David L. <leo...@gm...> - 2019-12-04 20:41:49
|
Hello I am new to htmlunit and I am using it for the first time. Currently I try to automatically fill and submit details in an iframe as part of some automated test suite. The problem I have: It seems that the text I add and the selections I made are not applied at all. I verified this by running a local web server with the same html and exchanged the javascript which is executed when submitting the data. I added debug output into the javascript and it shows me that all of the input elements still have their initial values. Here is the code I use (its scala code but as u can see its similar to java): import com.gargoylesoftware.htmlunit.{Page, WebClient} import com.gargoylesoftware.htmlunit.html._ import java.nio.file.{Files, Paths} import com.gargoylesoftware.htmlunit.CollectingAlertHandler import java.util object WebsiteAutofill { val webClient = new WebClient() webClient.getOptions.setJavaScriptEnabled(true) webClient.getOptions.setRedirectEnabled(true) webClient.getOptions.setCssEnabled(true) val collectedAlerts = new util.ArrayList[String] webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts)) val url = "http://localhost:8000/secupay.html" def main(args: Array[String]): Unit = { val iframe: HtmlPage = webClient.getPage(url) val cardName: HtmlTextInput = iframe.getElementById("card_name").asInstanceOf[HtmlTextInput] val cardNumber: HtmlTextInput = iframe.getElementById("card_number").asInstanceOf[HtmlTextInput] val cardExpiryMonth: HtmlSelect = iframe.getElementById("card_expiry_month").asInstanceOf[HtmlSelect] val cardExpiryYear: HtmlSelect = iframe.getElementById("card_expiry_year").asInstanceOf[HtmlSelect] val cardCvv: HtmlTextInput = iframe.getElementById("card_cvc").asInstanceOf[HtmlTextInput] val submitButton: HtmlSubmitInput = iframe.getElementById("btn_submit").asInstanceOf[HtmlSubmitInput] cardName.setValueAttribute("Card Owner") cardName.setText("Card Owner") cardNumber.setValueAttribute("4024007186180153") cardNumber.setText("4024007186180153") cardExpiryMonth.setSelectedAttribute( cardExpiryMonth.getOptionByValue("1"), true ): Page cardExpiryMonth.setAttribute("value", "1") cardExpiryYear.setSelectedAttribute( cardExpiryYear.getOptionByValue("2021"), true ): Page cardExpiryYear.setAttribute("value", "2021") cardCvv.setValueAttribute("123") cardCvv.setText("123") println(s""" |card name value: ${cardName .getAttribute("value")} | ${cardName.getText} |numbervalue: ${cardNumber.getAttribute("value")} |expire month: ${cardExpiryMonth.getAttribute("value")} |expire year: ${cardExpiryYear.getAttribute("value")} |cvv: ${cardCvv.getAttribute("value")} |""".stripMargin) val redirectPage: HtmlPage = submitButton.click().asInstanceOf[HtmlPage] println(s"Redirect url: ${redirectPage.getUrl.toString}") val htmlBody = redirectPage.getWebResponse.getContentAsString Files.write(Paths.get("out.html"), htmlBody.getBytes("utf-8")) println(s"CollectedAlerts: ${collectedAlerts.size()}") collectedAlerts.forEach(println(_)) } } In the println call I can see that the values are set but later when I print the debug alert outputs from the javascript the values seem to be lost. I am not experienced with using mailing lists so I dont know if I can attach files here, I will just try to attach also the html and javascript file which I am using so it can be fully reproduced. I would be really glad if somebody can help me cos I am struggling with this for days already. Br David |