void () { final XmlElementFactory elementFactory = XmlElementFactory.getInstance(getProject()); final XmlTag aTag = elementFactory.createTagFromText("<a>1</a>"); final XmlText displayText = elementFactory.createDisplayText("2"); WriteCommandAction.runWriteCommandAction(null, () -> { final PsiElement psiElement = aTag.addAfter(displayText, aTag.getValue().getChildren()[0]); assertEquals(psiElement.getContainingFile(), aTag.getContainingFile()); }); }