2

I want to read a JSON file and pass its content to a browser page using selenium-java. Using ObjectMapper, I am able to read the file OK.

ObjectMapper mapper = new ObjectMapper();
JsonNode payload = mapper.readTree(new File(baseUtil.getPayload()));
System.out.println(payload); //payload is printed out OK
driver.findElement(By.id("demo-payload")).sendKeys((payload)); //error on this line: Cannot resolve method 'sendKeys(com.fasterxml.jackson.databind.JsonNode)'

When attempting to pass the file content to selenium's sendKeys method, I got a compilation error:

Cannot resolve method 'sendKeys(com.fasterxml.jackson.databind.JsonNode)'

I tried resolving the issue by casting the argument to CharSequence, this way:

 driver.findElement(By.id("demo-payload")).sendKeys((CharSequence) payload);

Output:

java.lang.ClassCastException: com.fasterxml.jackson.databind.node.ObjectNode cannot be cast to java.lang.CharSequence

Is there a way to pass the JSON file content to Selenium's sendKeys?

matt
  • 25
  • 5
  • 2
    @AndreyKotov, this works perfectly. If you put the answer in the provided answer box, I can mark it as an acceptable answer. Thanks – matt Jul 04 '22 at 00:22

1 Answers1

0

Use .sendKeys(mapper.writeValueAsString(jsonNodePayload)) or .sendKeys(jsonNodePayload.toString()). Also you don't need to read this value from a file like a JsonNode because further you simply put everything as String to sendKeys method. Read it as a string from a file.

Andrey Kotov
  • 1,344
  • 2
  • 14
  • 26