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
?
CodePudding user response:
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.