void () { String html = "<html><head /><body><hr /></body>\n</html>"; XmlFile file = (XmlFile)PsiFileFactory.getInstance(getProject()).createFileFromText("xxx.xhtml", html); XmlTag body = file.getDocument().getRootTag().findFirstSubTag("body"); final XmlTag hr = body.getSubTags()[0]; final XmlText text = XmlElementFactory.getInstance(getProject()).createDisplayText("p"); ApplicationManager.getApplication().runWriteAction(() -> { PsiElement element = hr.getParentTag().addAfter(text, hr); assertEquals(element.getParent(), hr.getParentTag()); }); }