Selenium-使用换行符将文件写入代码镜像
Selenium - Writing a file to codemirror with line breaks
我正在编写一个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"替换为"<''br>"(没有反斜杠。我甚至不能在堆栈溢出时换行)。这只是在json文本中放了一堆"br"标记
有什么想法吗?我想有一种方法可以逐行完成,但我宁愿将整个文件作为一个字符串发送。
去除换行
尝试:
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");
相关文章:
- Markdown模式代码镜像正在创建
- 更改代码镜像中TextArea的高度和宽度
- 代码镜像批量更改
- 如何在代码镜像编辑器中设置隐藏值
- Selenium-使用换行符将文件写入代码镜像
- 默认情况下在代码镜像中启动全屏
- 如何在代码镜像编辑器中设置不同的维度
- 将变量中的文本放入代码镜像文本区域
- 多种模式代码镜像
- 如何在 Angular2 组件中访问代码镜像文本区域值
- 通过选择菜单即时更改代码镜像模式
- 代码镜像如何创建自定义自动完成下拉列表
- 确定代码镜像是否滚动到底部
- 代码镜像 更改事件上的合并构造函数
- 代码镜像 - 覆盖主页/结束按钮以正常工作
- 根据容器设置代码镜像实例的焦点
- 代码镜像 - 更新选择
- 代码镜像和 iFrame 问题
- 使用代码镜像编辑 img 的 src
- 代码镜像 - 最小行数