From de77e0bca8102c8ad6f2e147f6dc87f0f56b9fae Mon Sep 17 00:00:00 2001 From: Jacques Le Roux Date: Fri, 4 Dec 2009 09:27:30 +0000 Subject: [PATCH] A patch from Erwan de FERRIERES "seleniumXML improvement and corrections" (https://issues.apache.org/jira/browse/OFBIZ-3296) - OFBIZ-3296 corrected the XML tests added new functions in selenium for better conversion from selenese and use with the XML tests : * echo * verifyTextPresent * verifyTextNotPresent * assertTitle * assertConfirmation Removed comments * waitForPageToLoad : just wait the page to load before any other action (time in ms) * click : make a click on an element * assertContains : assert an element contains some code. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@887126 13f79535-47bb-0310-9956-ffa450edef68 --- .../seleniumxml/SeleniumIDEConverter.java | 29 ++++++++--- .../testtools/seleniumxml/SeleniumXml.java | 50 ++++++++++++++++--- .../seleniumxml/example/example_new.xml | 6 +-- .../seleniumxml/example/example_search.xml | 5 +- 4 files changed, 66 insertions(+), 24 deletions(-) diff --git a/framework/testtools/src/org/ofbiz/testtools/seleniumxml/SeleniumIDEConverter.java b/framework/testtools/src/org/ofbiz/testtools/seleniumxml/SeleniumIDEConverter.java index 8c4c38e29c..23ce727b8b 100644 --- a/framework/testtools/src/org/ofbiz/testtools/seleniumxml/SeleniumIDEConverter.java +++ b/framework/testtools/src/org/ofbiz/testtools/seleniumxml/SeleniumIDEConverter.java @@ -44,7 +44,7 @@ public class SeleniumIDEConverter { private Element xmlDestRoot; private Namespace ns = Namespace.getNamespace("http://www.w3.org/1999/xhtml"); private Map root; - + public void convert(String ideFile, String xmlFile) throws JDOMException, IOException, SAXException, ParserConfigurationException { readInputFile(ideFile); convertIDECommands(); @@ -54,7 +54,7 @@ public class SeleniumIDEConverter { private void readInputFile(String input) throws JDOMException, IOException, SAXException, ParserConfigurationException { File xmlFile = new File(input); SAXBuilder builder = new SAXBuilder(); - this.ideFile = builder.build(xmlFile); + this.ideFile = builder.build(xmlFile); } private void createSeleniumXml(String outputFile) { @@ -93,29 +93,42 @@ public class SeleniumIDEConverter { System.out.println("Found clickAndWait"); this.xmlDestRoot.addContent(buildCommand("click", "locator", cmd.get(1).getValue(), null, null)); this.xmlDestRoot.addContent(buildCommand("waitForPageToLoad", "value", "10000", null, null)); - } else if ("type".compareTo(cmdElem.getValue()) == 0 ) { System.out.println("Found type"); this.xmlDestRoot.addContent (buildCommand("type", "name", cmd.get(1).getValue(), "value", cmd.get(2).getValue())); - } else if ("select".compareTo(cmdElem.getValue()) == 0) { System.out.println("Found select"); this.xmlDestRoot.addContent(buildCommand("select", "locator", cmd.get(1).getValue(), "option", cmd.get(2).getValue())); - } else if ("open".compareTo(cmdElem.getValue()) == 0) { System.out.println("Found open"); this.xmlDestRoot.addContent(buildCommand("open", "value", cmd.get(1).getValue(), null, null)); - } else if ("click".compareTo(cmdElem.getValue()) == 0) { Element newCmd = new Element("click"); newCmd.setAttribute("locator", cmd.get(1).getValue()); this.xmlDestRoot.addContent(newCmd); - } else if ("doubleClick".compareTo(cmdElem.getValue()) == 0) { Element newCmd = new Element("doubleClick"); newCmd.setAttribute("locator", cmd.get(1).getValue()); this.xmlDestRoot.addContent(newCmd); - + } else if ("echo".compareTo(cmdElem.getValue()) == 0) { + System.out.println("Found echo"); + Element newCmd = new Element("print"); + newCmd.setAttribute("value", cmd.get(1).getValue()); + this.xmlDestRoot.addContent(newCmd); + } else if ("verifyTextPresent".compareTo(cmdElem.getValue()) == 0) { + System.out.println("Found verifyTextPresent"); + this.xmlDestRoot.addContent(buildCommand("getBodyText", "out", "bodySource", null, null)); + this.xmlDestRoot.addContent(buildCommand("assertContains", "test", cmd.get(1).getValue(), "src", "${bodySource}")); + } else if ("verifyTextNotPresent".compareTo(cmdElem.getValue()) == 0) { + System.out.println("Found verifyTextNotPresent"); + this.xmlDestRoot.addContent(buildCommand("getBodyText", "out", "bodySource", null, null)); + this.xmlDestRoot.addContent(buildCommand("assertNotContains", "test", cmd.get(1).getValue(), "src", "${bodySource}")); + } else if ("assertTitle".compareTo(cmdElem.getValue()) == 0) { + System.out.println("Found assertTitle"); + this.xmlDestRoot.addContent(buildCommand("assertTitle", "value", cmd.get(1).getValue(), null, null)); + } else if ("assertConfirmation".compareTo(cmdElem.getValue()) == 0) { + System.out.println("Found assertConfirmation"); + this.xmlDestRoot.addContent(buildCommand("assertConfirmation", "value", cmd.get(1).getValue(), null, null)); } else { System.out.println("WARNING: No definition for " + cmdElem.getValue() + " defaulting to us 'reflection'."); Element newCmd = new Element(cmdElem.getValue()); diff --git a/framework/testtools/src/org/ofbiz/testtools/seleniumxml/SeleniumXml.java b/framework/testtools/src/org/ofbiz/testtools/seleniumxml/SeleniumXml.java index 663c941188..720df81ce8 100644 --- a/framework/testtools/src/org/ofbiz/testtools/seleniumxml/SeleniumXml.java +++ b/framework/testtools/src/org/ofbiz/testtools/seleniumxml/SeleniumXml.java @@ -270,6 +270,8 @@ public class SeleniumXml { testcase(elem); } else if("assertContains" == thisName) { assertContains(elem); + } else if("assertNotContains" == thisName) { + assertNotContains(elem); } else if("getHtmlSource" == thisName) { getHtmlSource(elem); } else if("getBodyText" == thisName) { @@ -288,11 +290,11 @@ public class SeleniumXml { appendCmd(elem); } else if("loadParameter" == thisName) { loadParameter(elem); - }else if("partialRunDependency" == thisName) { + } else if("partialRunDependency" == thisName) { partialRunDependency(elem); - }else if("if" == thisName) { + } else if("if" == thisName) { ifCmd(elem); - }else if("open" == thisName) { + } else if("open" == thisName) { openCmd(elem); } else if("click" == thisName) { clickCmd(elem); @@ -316,9 +318,11 @@ public class SeleniumXml { openWindow(elem); } else if("selectWindow" == thisName) { selectWindow(elem); + } else if("assertConfirmation" == thisName) { + assertConfirmation(elem); } else if("runScript" == thisName) { runScript(elem); - } else { + } else { logger.info("Undefined command calling by reflection for command: " + thisName); callByReflection(elem); } @@ -362,7 +366,6 @@ public class SeleniumXml { } public void waitForValue(Element elem) { - String locator = replaceParam(elem.getAttributeValue("locator")); String timeout = elem.getAttributeValue("timeout"); String outParam = elem.getAttributeValue("out"); @@ -541,13 +544,36 @@ public class SeleniumXml { if(indxSearch == -1) { logger.info("assertContains didn't find " + test + " in the src"); throw new TestCaseException("assertContains didn't find: " + test); - } else { logger.info("assertContains found " + test + " in the src"); } //TODO: implement JUnit TestCase - Assert.assertTrue(indxSearch != -1); } + private void assertNotContains(Element elem) throws TestCaseException { + String src = replaceParam(elem.getAttributeValue("src")); + String test = replaceParam(elem.getAttributeValue("test")); + int indxSearch = src.indexOf(test); + if(indxSearch != -1) { + logger.info("assertNotContains found " + test + " in the src"); + throw new TestCaseException("assertContains didn't find: " + test); + } else { + logger.info("assertNotContains didn't find " + test + " in the src"); + } + } + + private void assertTitle(Element elem) throws TestCaseException { + String src = replaceParam(this.sel.getTitle()); + String test = replaceParam(elem.getAttributeValue("value")); + int indxSearch = src.indexOf(test); + if(indxSearch == -1) { + logger.info("assertTitle value " + test + " doesn't match exact "+src); + throw new TestCaseException("assertTitle value " + test + " doesn't match exact "+src); + } else { + logger.info("assertTitle matched title"); + } + } + private void selectPopup(Element elem) { String locator = elem.getAttributeValue("locator"); String timeout = elem.getAttributeValue("timeout"); @@ -591,12 +617,14 @@ public class SeleniumXml { logger.info("getSelectedValue: locator=" + locator + " text="+text); addParam(out, text); } + private void getSelectedId(Element elem) { String locator = elem.getAttributeValue("locator"); String out = elem.getAttributeValue("out"); String text = this.sel.getSelectedId(locator); addParam(out, text); } + private void getHtmlSource(Element elem) { String paramName = elem.getAttributeValue("out"); String text = this.sel.getHtmlSource(); @@ -609,6 +637,7 @@ public class SeleniumXml { String text = this.sel.getBodyText(); addParam(paramName, text); } + private void testcase(Element elem) { System.err.println("New testcase: " + elem.getAttributeValue("file")); String testFile = elem.getAttributeValue("file"); @@ -642,6 +671,7 @@ public class SeleniumXml { } return directory; } + private void clickAt(Element elem) { logger.debug("clickAt: " + replaceParam(elem.getAttributeValue("locator"))); String locator = replaceParam(elem.getAttributeValue("locator")); @@ -649,6 +679,12 @@ public class SeleniumXml { this.sel.clickAt(locator, coordString); } + private void assertConfirmation(Element elem) { + logger.debug("assertConfirmation: " + replaceParam(elem.getAttributeValue("value"))); + this.sel.waitForCondition("selenium.isConfirmationPresent();", "1000"); + this.sel.getConfirmation(); + } + /** * @param elem takes a Selenium String locator. See Javadocs for more information. Here are some * example locators: @@ -662,9 +698,7 @@ public class SeleniumXml { try { this.sel.click(replaceParam(elem.getAttributeValue("locator"))); } catch (SeleniumException e) { - logger.info("caught SeleniumException Name:"+elem.getName()+" , Value: "+elem.getAttributeValue("locator")); - e.printStackTrace(); } } diff --git a/framework/testtools/testdef/seleniumxml/example/example_new.xml b/framework/testtools/testdef/seleniumxml/example/example_new.xml index 455ac15242..fefef30074 100644 --- a/framework/testtools/testdef/seleniumxml/example/example_new.xml +++ b/framework/testtools/testdef/seleniumxml/example/example_new.xml @@ -20,11 +20,9 @@ - +