protected Object getPropertyObject(String propertyName) throws SAFSException { String rval = null; if (compObject == null) { int status = waitForObject(mapname, windowName, compName, secsWaitForComponent); if (status == 0) compObject = ((STestRecordHelper) testRecordData).getCompTestObject(); } if (compObject != null) { Log.debug("assignPropVar for " + compObject.getLocator() + ":" + propertyName); rval = sUtils.getAttribute(selenium, compObject.getLocator(), propertyName); if (rval == null) { rval = selenium.getEval( "var xpath = \"" + compObject.getLocator() + "\";var prop = \"" + propertyName + "\";SAFSgetAttribute(xpath,prop);"); } } else { Log.warn("component object is null."); } return rval; }
protected boolean exist() throws SAFSException { String debugmsg = StringUtils.debugmsg(false); try { boolean isShowing = false; // wait for the window - secTimout of 0 means a single try to search for given comps // waitForObject checks only component validity and tells nothing about its visibility int status = sUtils.waitForObject(mapname, windowName, compName, 0); if (status == 0) { // component search succeeded isShowing = compIsVisible("//HTML[1]"); SGuiObject obj; obj = sHelper.getCompTestObject(); if (isShowing && (obj != null)) { isShowing = compIsVisible(obj.getLocator()); } } // else status!=0, which means that component search failed return isShowing; } catch (SAFSObjectNotFoundException sonfe) { Log.warn(debugmsg + " Met Exception " + StringUtils.debugmsg(sonfe)); } return false; }
/** * <em>Purpose:</em> Remove the content from an input box.<br> * * @param _comp */ private void clearText(SGuiObject _comp) { String debugmsg = getClass().getName() + ".clearText(): "; try { selenium.type(_comp.getLocator(), ""); } catch (Exception e) { Log.debug(debugmsg + " Exception occur. " + e.getMessage()); selenium.click(_comp.getLocator()); RobotKeyEvent.doKeystrokes(keysparser.parseInput("^a{ExtDelete}"), robot, 0); // RobotKeyEvent.doKeystrokes(keysparser.parseInput("{ExtHome}+{ExtEnd}{ExtDelete}"), robot, // 0); } }
@SuppressWarnings("unchecked") protected Map<String, Object> getProperties() throws SAFSException { if (compObject != null) { return sUtils.getAttributes(selenium, compObject.getLocator()); } else { Log.debug("compObject is null, can't get properties."); return null; } }
/** * Scrolls to a component then left clicks on it. * * @param o component to be clicked */ protected void scrollToAndClickComponent(SGuiObject o) { scrollToComponent(o); Rectangle compRect = getComponentBounds(o); try { robot.mouseMove((int) compRect.getCenterX(), (int) compRect.getCenterY()); robot.mousePress(KeyEvent.BUTTON1_MASK); robot.mouseRelease(KeyEvent.BUTTON1_MASK); testRecordData.setStatusCode(StatusCodes.NO_SCRIPT_FAILURE); } catch (NullPointerException npe) { Log.debug("IGNORING Selenium NPE for scrollToAndClick '" + o.getWindowId() + "':", npe); testRecordData.setStatusCode(StatusCodes.NO_SCRIPT_FAILURE); } }
protected Collection<String> captureObjectData() throws SAFSException { Collection<String> contents = new ArrayList<String>(); String prop = PROPERTY_innerText; // this isn't the property for all objects... String browser = TestRecordHelper.getConfig().getNamedValue(DriverConstant.SECTION_SAFS_SELENIUM, "BROWSER"); if (!(browser.equals("*iexplore") || browser.equals("*piiexplore"))) { prop = PROPERTY_textContent; } String myClass = getClass().getSimpleName(); if (myClass.equals("CFList") || myClass.equals("CFComboBox")) { prop = PROPERTY_DOT_itemText; } // Get text from object... Object rval = null; if (compObject != null) { StringBuffer jsScript = new StringBuffer(); jsScript.append(JavaScriptFunctions.getSAFSgetElementFromXpathFunction()); jsScript.append(JavaScriptFunctions.getSAFSgetAttributeFunction()); jsScript.append(" var xpath = \"" + compObject.getLocator() + "\";"); jsScript.append(" var prop = \"" + prop + "\";"); jsScript.append(" SAFSgetAttribute(xpath,prop);"); rval = selenium.getEval(jsScript.toString()); } if (rval == null || rval.equals("null")) { throw new SAFSException( "read property(" + prop + ") value is null", SAFSException.CODE_CONTENT_ISNULL); } Log.info("..... real value is: " + rval + ", " + rval.getClass().getName()); String value = null; if (rval instanceof Collection) { @SuppressWarnings("rawtypes") Iterator ii = ((Collection) rval).iterator(); while (ii.hasNext()) { value = getStringValue(ii.next()); contents.add(value); } } else { value = getStringValue(rval); contents.add(value); } return contents; }
/** * <br> * <em>Purpose:</em> inputKeys and inputCharacters */ protected void inputKeystrokes() throws SAFSException { String keys = ""; if (params.size() < 1) { Log.error("Need at least one parameter."); paramsFailedMsg(windowName, compName); return; } keys = (String) iterator.next(); Log.debug("Input Parameter '" + keys + "'"); if (GenericMasterFunctions.INPUTKEYS_KEYWORD.equalsIgnoreCase(action)) { try { setFocusToWindow(); if (compObject == null) { Log.warn("component object is null."); } else { clearText(compObject); } Robot.inputKeys(keys); issuePassedSuccessUsing(keys); } catch (AWTException e) { Log.debug("Exception occur: " + e.getMessage()); issueActionFailure("Exception occur " + e.getMessage()); } } else { // InputCharacters try { if (compObject == null) { Log.debug("component object is null"); throw new SAFSException("component object is null"); } else { clearText(compObject); selenium.type(compObject.getLocator(), keys); issuePassedSuccessUsing(keys); } } catch (Exception e) { try { Log.debug("Can not input character by selenium API, try SAFS Robot."); Robot.inputChars(keys); issuePassedSuccessUsing(keys); } catch (AWTException awte) { Log.debug("Fail to inpu the character, Exception " + awte.getMessage()); issueActionFailure("Exception occur " + awte.getMessage()); } } } }
/** Maximize the current window */ protected void _maximize() throws SAFSException { String message = null; // The window can't be resotred if it is maximized by selenium API. // So use the SAFS Robot to maximize the windows firstly try { super._maximize(); return; } catch (SAFSException e) { message = "Can't maximize window by SAFS Robot. Try Selenium API to maximize. Exception=" + e.getMessage(); Log.warn(message); if (!sUtils.maximizeWindow(winObject.getWindowId(), selenium)) { throw new SAFSException("Fail to maximize by Selenium API."); } } }
/** * Scrolls to a component on the page * * @param o component to be scrolled to */ protected void scrollToComponent(SGuiObject o) { String xpath = sUtils.navigateFrames(o.getLocator(), selenium); long left = 0; long top = 0; try { left = selenium.getElementPositionLeft(xpath).longValue(); } catch (Exception x) { } try { top = selenium.getElementPositionTop(xpath).longValue(); } catch (Exception x) { } String eval = selenium.getEval("SAFSgetClientScrollInfo();"); String[] values = eval.split(";"); long width = 0; long height = 0; long scrollTop = 0; long scrollLeft = 0; try { width = Integer.parseInt(values[0]); } catch (Exception x) {; } try { height = Integer.parseInt(values[1]); } catch (Exception x) {; } try { scrollLeft = Integer.parseInt(values[2]); } catch (Exception x) {; } try { scrollTop = Integer.parseInt(values[3]); } catch (Exception x) {; } if ((left < scrollLeft || left > scrollLeft + width) || (top < scrollTop || top > scrollTop + height)) { eval = selenium.getEval("window.scrollTo(" + left + "," + top + ");"); } }
/** * <br> * <em>Purpose:</em> componentClick */ protected void componentClick() throws SAFSException { if (compObject == null) { throw new SAFSException("Component SGuiObject is null."); } String locator = compObject.getLocator(); Log.debug("component's locator=" + locator); setFocusToWindow(); try { if (action.equalsIgnoreCase(CLICK) || action.equalsIgnoreCase(COMPONENTCLICK)) { selenium.click(locator); testRecordData.setStatusCode(StatusCodes.NO_SCRIPT_FAILURE); } else if (action.equalsIgnoreCase(CTRLCLICK)) { selenium.controlKeyDown(); selenium.click(locator); selenium.controlKeyUp(); testRecordData.setStatusCode(StatusCodes.NO_SCRIPT_FAILURE); } else if (action.equalsIgnoreCase(DOUBLECLICK)) { selenium.doubleClick(locator); testRecordData.setStatusCode(StatusCodes.NO_SCRIPT_FAILURE); } else if (action.equalsIgnoreCase(RIGHTCLICK)) { selenium.mouseDownRight(locator); selenium.mouseUpRight(locator); testRecordData.setStatusCode(StatusCodes.NO_SCRIPT_FAILURE); } else if (action.equalsIgnoreCase(CTRLRIGHTCLICK)) { selenium.controlKeyDown(); selenium.mouseDownRight(locator); selenium.mouseUpRight(locator); selenium.controlKeyUp(); testRecordData.setStatusCode(StatusCodes.NO_SCRIPT_FAILURE); } else if (action.equalsIgnoreCase(SHIFTCLICK)) { selenium.shiftKeyDown(); selenium.click(locator); selenium.shiftKeyUp(); testRecordData.setStatusCode(StatusCodes.NO_SCRIPT_FAILURE); } } catch (Exception e) { // Try SFAS robot to do the work. scrollToComponent(compObject); Rectangle compRect = getComponentBounds(compObject); if (compRect == null) { Log.error("component rectangle is null."); throw new SAFSException("component rectangle is null."); } // Log.info("Selenium mouseMove to: " // +(int)compRect.getCenterX()+":"+(int)compRect.getCenterY()); if (action.equalsIgnoreCase(CLICK) || action.equalsIgnoreCase(COMPONENTCLICK)) { robot.mouseMove((int) compRect.getCenterX(), (int) compRect.getCenterY()); robot.mousePress(KeyEvent.BUTTON1_MASK); robot.mouseRelease(KeyEvent.BUTTON1_MASK); testRecordData.setStatusCode(StatusCodes.NO_SCRIPT_FAILURE); } else if (action.equalsIgnoreCase(CTRLCLICK)) { robot.keyPress(KeyEvent.VK_CONTROL); robot.mouseMove((int) compRect.getCenterX(), (int) compRect.getCenterY()); robot.mousePress(KeyEvent.BUTTON1_MASK); robot.mouseRelease(KeyEvent.BUTTON1_MASK); robot.keyRelease(KeyEvent.VK_CONTROL); testRecordData.setStatusCode(StatusCodes.NO_SCRIPT_FAILURE); } else if (action.equalsIgnoreCase(DOUBLECLICK)) { robot.mouseMove((int) compRect.getCenterX(), (int) compRect.getCenterY()); robot.mousePress(KeyEvent.BUTTON1_MASK); robot.mouseRelease(KeyEvent.BUTTON1_MASK); robot.mousePress(KeyEvent.BUTTON1_MASK); robot.mouseRelease(KeyEvent.BUTTON1_MASK); testRecordData.setStatusCode(StatusCodes.NO_SCRIPT_FAILURE); } else if (action.equalsIgnoreCase(RIGHTCLICK)) { robot.mouseMove((int) compRect.getCenterX(), (int) compRect.getCenterY()); robot.mousePress(KeyEvent.BUTTON3_MASK); robot.mouseRelease(KeyEvent.BUTTON3_MASK); testRecordData.setStatusCode(StatusCodes.NO_SCRIPT_FAILURE); } else if (action.equalsIgnoreCase(CTRLRIGHTCLICK)) { robot.keyPress(KeyEvent.VK_CONTROL); robot.mouseMove((int) compRect.getCenterX(), (int) compRect.getCenterY()); robot.mousePress(KeyEvent.BUTTON3_MASK); robot.mouseRelease(KeyEvent.BUTTON3_MASK); robot.keyRelease(KeyEvent.VK_CONTROL); testRecordData.setStatusCode(StatusCodes.NO_SCRIPT_FAILURE); } else if (action.equalsIgnoreCase(SHIFTCLICK)) { robot.keyPress(KeyEvent.VK_SHIFT); robot.mousePress(KeyEvent.BUTTON1_MASK); robot.mouseRelease(KeyEvent.BUTTON1_MASK); robot.keyRelease(KeyEvent.VK_SHIFT); testRecordData.setStatusCode(StatusCodes.NO_SCRIPT_FAILURE); } } // log success message and status if (testRecordData.getStatusCode() == StatusCodes.NO_SCRIPT_FAILURE) { String msg = genericText.convert( "success3", windowName + ":" + compName + " " + action + " successful.", windowName, compName, action); log.logMessage(testRecordData.getFac(), msg, PASSED_MESSAGE); return; // just in case. (normally any failure should have issued an Exception) } else { log.logMessage( testRecordData.getFac(), action + "\n" + testRecordData.getFilename() + " at Line " + testRecordData.getLineNumber() + ", " + testRecordData.getFac(), FAILED_MESSAGE); testRecordData.setStatusCode(StatusCodes.GENERAL_SCRIPT_FAILURE); } }
/** * <br> * <em>Purpose:</em> verifyProperty */ protected void verifyProperty() throws SAFSException { testRecordData.setStatusCode(StatusCodes.GENERAL_SCRIPT_FAILURE); if (params.size() < 2) { paramsFailedMsg(windowName, compName); return; } String prop = (String) iterator.next(); String val = (String) iterator.next(); String strcase = ""; boolean ignorecase = false; try { strcase = (String) iterator.next(); } catch (Exception x) {; } ignorecase = !StringUtils.isCaseSensitive(strcase); Log.info(".....CFComponent.process; ready to do the VP for prop : " + prop + " val: " + val); String rval = null; int status = sUtils.waitForObject(mapname, windowName, compName, 15); SGuiObject comp = null; String _type = null; if (status == 0) { comp = ((STestRecordHelper) testRecordData).getCompTestObject(); _type = ((STestRecordHelper) testRecordData).getCompType(); // TODO: Changed this! // rval = selenium.getEval("var xpath = \""+comp.getLocator()+"\";var prop = // \""+prop+"\";SAFSgetAttribute(xpath,prop);"); rval = sUtils.getAttribute(selenium, comp.getLocator(), prop); if (rval == null) { Log.info( "Selenium property '" + prop + "' for component '" + compName + "' of type '" + _type + "' not found. Trying alternatives..."); // assume attribute not found if (((_type.equalsIgnoreCase("CHECKBOX")) || (_type.equalsIgnoreCase("RADIOBUTTON"))) && (prop.equalsIgnoreCase("CHECKED")) && (val.equalsIgnoreCase("FALSE"))) { rval = "False"; } if (((_type.equalsIgnoreCase("LISTBOX")) || (_type.equalsIgnoreCase("COMBOBOX"))) && (prop.equalsIgnoreCase("SELECTED")) && (val.equalsIgnoreCase("FALSE"))) { rval = "False"; } } } Log.info( "Selenium property '" + prop + "' for component '" + compName + "' of type '" + _type + "' contains value '" + rval + "'"); // it is possible the property name is not valid for Selenium // it may be a property valid in a different engine // TODO: but do we want to assume we are running with other engines? if (rval == null || rval.equals("null")) { testRecordData.setStatusCode(StatusCodes.SCRIPT_NOT_EXECUTED); return; } if (((!ignorecase) && (val.equals(rval))) || (ignorecase && (val.equalsIgnoreCase(rval)))) { // set status to ok testRecordData.setStatusCode(StatusCodes.NO_SCRIPT_FAILURE); altText = genericText.convert( "bench_matches", compName + ":" + prop + " matches expected value \"" + val + "\"", compName + ":" + prop, val); log.logMessage(testRecordData.getFac(), altText, PASSED_MESSAGE); } else { // failed testRecordData.setStatusCode(StatusCodes.GENERAL_SCRIPT_FAILURE); altText = genericText.convert( "bench_not_match", compName + ":" + prop + " did not match expected result \"" + val + "\"", compName + ":" + prop, val); String detail = genericText.convert("actual_value", "ActualValue='" + rval + "'", rval); standardFailureMessage(altText, compName + ":" + prop + " " + detail); } }
private void setFocusToWindow() throws SAFSException { if (!sUtils.setWindowFocus(winObject.getWindowId(), selenium)) { Log.warn("Not able to focus window: " + testRecordData.getWindowGuiId()); } }