Is there a way to pass ObjectMapper's JsonNode Object to selenium's sendKeys method?

Issue

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?

Solution

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.

Answered By – Andrey Kotov

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published