Javascript Selenium-使用换行符将文件写入codemirror
我正在编写一个selenium测试,它将JSON文件写入CodeMirror窗口。以下是我目前的代码:Javascript Selenium-使用换行符将文件写入codemirror,javascript,java,json,selenium,codemirror,Javascript,Java,Json,Selenium,Codemirror,我正在编写一个selenium测试,它将JSON文件写入CodeMirror窗口。以下是我目前的代码: public void setJson(String jsonPath) throws IOException, InterruptedException { // Read a file to a string byte[] encoded = Files.readAllBytes(Paths.get(jsonPath)); String jsonText = ne
public void setJson(String jsonPath) throws IOException, InterruptedException {
// Read a file to a string
byte[] encoded = Files.readAllBytes(Paths.get(jsonPath));
String jsonText = new String(encoded, StandardCharsets.UTF_8);
// Get rid of line breaks
jsonText = jsonText.replace("\n", "");
// Escape the quotes
jsonText = jsonText.replace("\"", "\\\"");
// Write to the CodeMirror
WebElement queryInput = driver.findElement(By.xpath("//*[@id=\"content\"]/div/div[2]/div/div[1]/div[1]/div/div/div/div"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].CodeMirror.setValue(\"" + jsonText + "\");", queryInput);
}
这个代码有效。但它在一行中写入整个JSON文件。以下是我尝试过的几件事
有什么想法吗?我猜有一种方法可以逐行执行此操作,但我更希望能够将整个文件作为一个字符串发送。以避免换行 尝试: 或
消除换行 尝试: 或
你的JSON是什么?它是否已经换行了,或者您正在寻找一台漂亮的打印机?请同时粘贴您的JSON,这样我们可以更好地帮助您。这也是一个疯狂的猜测。。尝试使用“\\n”替换“\n”,JSON有换行符(“\n”),但如果我将它们留在那里,就会出现“未终止字符串文字”错误。我用“\\n”替换了它们,一切正常!谢谢韦丹舒!对我来说,它与
\\r
一起工作。。谢谢大家!!!你的JSON是什么?它是否已经换行了,或者您正在寻找一台漂亮的打印机?请同时粘贴您的JSON,这样我们可以更好地帮助您。这也是一个疯狂的猜测。。尝试使用“\\n”替换“\n”,JSON有换行符(“\n”),但如果我将它们留在那里,就会出现“未终止字符串文字”错误。我用“\\n”替换了它们,一切正常!谢谢韦丹舒!对我来说,它与\\r
一起工作。。谢谢大家!!!
jsonText = jsonText.replaceAll("\n", "\\\\\\\\\\\\\\\\r\\\\\\\\\\\\\\\\n"); // for all occurrence
// This will replace \n with \\\\r\\\\n a new line for codemirror
jsonText = jsonText.replace("\n", "\\\\\\\\\\\\\\\\r\\\\\\\\\\\\\\\\n");