Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Selenium-使用换行符将文件写入codemirror_Javascript_Java_Json_Selenium_Codemirror - Fatal编程技术网

Javascript Selenium-使用换行符将文件写入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

我正在编写一个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 = 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文件。以下是我尝试过的几件事

  • 不替换“\n”。这会产生“未终止的字符串文字”错误
  • 将“\n”替换为“\r”。同样的错误
  • 将“\n”替换为“”(没有反斜杠。我甚至不能在堆栈溢出时进行换行)。这只是在json文本中添加了一堆“br”标记

  • 有什么想法吗?我猜有一种方法可以逐行执行此操作,但我更希望能够将整个文件作为一个字符串发送。

    以避免换行

    尝试:


    消除换行

    尝试:


    你的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");