/** Assert the currently active window has the specified name */ protected static void assertWindowname(String name) throws InterruptedException, ComponentNotFoundException { logger.fine("Expecting window name: " + name); Window w = null; for (int i = 0; i < 100; i++) { guiSleep(); w = AWT.getActiveWindow(); if (w == null) continue; if (name.equals(w.getName())) return; Thread.sleep(100); } throw new ComponentNotFoundException( "Window name not found: " + name + (w != null ? (" (currently focused: " + w.getName() + ")") : "")); }
/** Gives focus to a {@linkplain Component} by its name */ protected static boolean focusByName(final String name) throws ComponentNotFoundException, MultipleComponentsFoundException { Component c = findByName(name); if (c.hasFocus()) return true; // try ordinary method if (c.requestFocusInWindow()) { while (!c.hasFocus()) guiSleep(); return true; } // press tab until we have the correct focus for (int i = 0; i < 25 /* TODO proper number */; i++) { tester.keyStroke('\t'); guiSleep(); if (name.equals(AWT.getActiveWindow().getFocusOwner().getName())) return true; } // failed ... logger.warning("Could not give focus to component: " + name); return true; }
/** Make screenshot taking part of unit tests */ @Test public static void testScreenshots() throws Exception { File shotdir = FileUtils.createTempDir("jgridstart-screenshots-"); try { doScreenshots(shotdir); } catch (Throwable e) { // on error, output final screenshot as base64 on debug log File errorshot = new File(shotdir, "error.png"); saveScreenshot(errorshot); Thread.sleep(500); FileInputStream in = new FileInputStream(errorshot); byte[] data = new byte[(int) errorshot.length()]; in.read(data, 0, data.length); // need to log in chunks because logger doesn't seem to be able to support >4096 chars String basedata = new String(Base64.encode(data)); logger.finest("Interactive UI testing failed, last screenshot (base64 encoded):"); logger.finest("=== BEGIN PNG ==="); int pos = 0; while (pos < basedata.length()) { int len = 1024; if (pos + len < basedata.length()) logger.finest(basedata.substring(pos, pos + len)); else logger.finest(basedata.substring(pos)); pos += len; } logger.finest("=== END PNG ==="); // destroy window Window mainwnd = AWT.getActiveWindow(); if (mainwnd != null && mainwnd.isVisible()) mainwnd.dispose(); // pass on error if (e instanceof Exception) throw (Exception) e; else if (e instanceof Error) throw (Error) e; else throw new Exception("Unknown throwable: ", e); } finally { // remove screenshot directory again FileUtils.recursiveDelete(shotdir); } }
public static void doScreenshots(File shotdir) throws Exception { shotdir.mkdirs(); String prefix = "jgridstart-screenshot-"; // setup temporary environment logger.info("Setting up jGridstart interactive screenshot and testing environment"); File tmphome = FileUtils.createTempDir("jgridstart-home"); Window mainwnd = null; try { System.setProperty("jgridstart.ca.provider", "LocalCA"); System.setProperty("jgridstart.ca.local.hold", "true"); System.setProperty("user.home", tmphome.getCanonicalPath()); // create standard gui nl.nikhef.jgridstart.gui.Main.main(new String[] {}); LogHelper.setupLogging(true); // move mouse here since closing window may give up focus later Thread.sleep(2000); guiSleep(); mainwnd = AWT.getActiveWindow(); assertNotNull(mainwnd); tester.mouseMove(mainwnd.getComponents()[0]); assertWindowname("jgridstart-main-window"); /* * Request new */ logger.info("Interactive testing scenario: Request New"); // start screen saveScreenshot(new File(shotdir, prefix + "newrequest01.png")); // new request wizard guiSleep(); tester.key(new Integer('N'), InputEvent.CTRL_MASK); guiSleep(); saveScreenshot(new File(shotdir, prefix + "newrequest02.png")); // enter details guiSleep(); assertWindowname("jgridstart-requestwizard-0"); focusByName("givenname"); keyString("John\t"); keyString("Doe\t"); keyString("[email protected]\t"); keyString("N\t"); keyString(password + "\t"); keyString(password + "\t"); // wait for submission tester.key(new Integer('N'), InputEvent.ALT_MASK); guiSleep(); saveScreenshot(new File(shotdir, prefix + "newrequest03.png")); assertWindowname("jgridstart-requestwizard-1"); waitEnabled(JButton.class, "Next"); // verification form System.setProperty("wizard.show.help1", "true"); // simulate help btn1 pressed tester.key(new Integer('N'), InputEvent.ALT_MASK); guiSleep(); guiSleep(); saveScreenshot(new File(shotdir, prefix + "newrequest04.png")); assertWindowname("jgridstart-requestwizard-2"); // form display JButton btn = (JButton) new BasicFinder() .find( new Matcher() { public boolean matches(Component c) { return c instanceof JButton && ((JButton) c).getText().startsWith("display form"); } }); btn.doClick(); waitEnabled(JButton.class, "Close"); guiSleep(); saveScreenshot(new File(shotdir, prefix + "newrequest05.png")); assertWindowname("jgridstart-verification-form"); tester.key(new Integer('C'), InputEvent.ALT_MASK); // close wizard guiSleep(); assertWindowname("jgridstart-requestwizard-2"); tester.key(new Integer('C'), InputEvent.ALT_MASK); guiSleep(); saveScreenshot(new File(shotdir, prefix + "newrequest06.png")); assertWindowname("jgridstart-main-window"); // enable certificate in LocalCA and refresh pane System.setProperty("jgridstart.ca.local.hold", "false"); tester.key(KeyEvent.VK_F5); Thread.sleep(1000); guiSleep(); saveScreenshot(new File(shotdir, prefix + "newrequest07.png")); assertWindowname("jgridstart-main-window"); // show request wizard again tester.key(new Integer('A'), InputEvent.ALT_MASK); tester.key('R'); guiSleep(); guiSleep(); saveScreenshot(new File(shotdir, prefix + "newrequest08.png")); assertWindowname("jgridstart-requestwizard-2"); // install step tester.key(new Integer('N'), InputEvent.ALT_MASK); Thread.sleep(1000); guiSleep(); saveScreenshot(new File(shotdir, prefix + "newrequest09.png")); assertWindowname("jgridstart-requestwizard-3"); // show final screen tester.key(new Integer('N'), InputEvent.ALT_MASK); guiSleep(); saveScreenshot(new File(shotdir, prefix + "newrequest10.png")); assertWindowname("jgridstart-requestwizard-4"); // exit wizard tester.key(new Integer('C'), InputEvent.ALT_MASK); // save final screenshot guiSleep(); saveScreenshot(new File(shotdir, prefix + "newrequest11.png")); assertWindowname("jgridstart-main-window"); guiSleep(); /* * Renewal */ logger.info("Interactive testing scenario: Renewal"); System.setProperty("jgridstart.ca.local.hold", "true"); // forget password so we certainly get the password dialog PasswordCache.getInstance().clear(); // start screen guiSleep(); saveScreenshot(new File(shotdir, prefix + "renew01.png")); assertWindowname("jgridstart-main-window"); // personal details tester.key(new Integer('A'), InputEvent.ALT_MASK); tester.key('W'); Thread.sleep(500); guiSleep(); saveScreenshot(new File(shotdir, prefix + "renew02.png")); assertWindowname("jgridstart-requestwizard-0"); focusByName("email"); keyString("\t"); keyString(password + "\t"); keyString(password + "\t"); keyString(password + "\t"); // wait for submission screen tester.key(new Integer('N'), InputEvent.ALT_MASK); // renew03.png used to be a password dialog, which was removed // submit page guiSleep(); saveScreenshot(new File(shotdir, prefix + "renew04.png")); assertWindowname("jgridstart-requestwizard-1"); waitEnabled(JButton.class, "Next"); // wait for approval page tester.key(new Integer('N'), InputEvent.ALT_MASK); guiSleep(); saveScreenshot(new File(shotdir, prefix + "renew05.png")); assertWindowname("jgridstart-requestwizard-2"); // close wizard guiSleep(); tester.key(new Integer('C'), InputEvent.ALT_MASK); guiSleep(); saveScreenshot(new File(shotdir, prefix + "renew06.png")); assertWindowname("jgridstart-main-window"); // enable certificate in LocalCA and refresh pane System.setProperty("jgridstart.ca.local.hold", "false"); tester.key(KeyEvent.VK_F5); Thread.sleep(1000); guiSleep(); saveScreenshot(new File(shotdir, prefix + "renew07.png")); assertWindowname("jgridstart-main-window"); // show request wizard again tester.key(new Integer('A'), InputEvent.ALT_MASK); tester.key('R'); waitEnabled(JButton.class, "Next"); Thread.sleep(500); guiSleep(); saveScreenshot(new File(shotdir, prefix + "renew08.png")); assertWindowname("jgridstart-requestwizard-2"); // install step tester.key(new Integer('N'), InputEvent.ALT_MASK); Thread.sleep(1000); guiSleep(); saveScreenshot(new File(shotdir, prefix + "renew09.png")); assertWindowname("jgridstart-requestwizard-3"); // exit wizard tester.key(new Integer('C'), InputEvent.ALT_MASK); // save final screenshot guiSleep(); saveScreenshot(new File(shotdir, prefix + "renew10.png")); assertWindowname("jgridstart-main-window"); guiSleep(); /* * Import/export */ logger.info("Interactive testing scenario: Import/Export"); // forget password so we certainly get the password dialog PasswordCache.getInstance().clear(); // starting screenshot (multiple certificates) guiSleep(); saveScreenshot(new File(shotdir, prefix + "importexport01.png")); assertWindowname("jgridstart-main-window"); // export dialog tester.key(new Integer('E'), InputEvent.CTRL_MASK); waitEnabled(JButton.class, "Export"); guiSleep(); saveScreenshot(new File(shotdir, prefix + "importexport02.png")); assertWindowname("jgridstart-export-file-dialog"); // enter name and do export tester.keyString("jgridstart_test_certificate.p12\n"); Thread.sleep(2000); saveScreenshot(new File(shotdir, prefix + "importexport03.png")); assertWindowname("jgridstart-password-entry-decrypt"); tester.keyString(password + "\n"); guiSleep(); assertWindowname("jgridstart-main-window"); // forget password so we certainly get the password dialog PasswordCache.getInstance().clear(); // import dialog tester.key(new Integer('I'), InputEvent.CTRL_MASK); waitEnabled(JButton.class, "Import"); guiSleep(); saveScreenshot(new File(shotdir, prefix + "importexport04.png")); assertWindowname("jgridstart-import-file-dialog"); guiSleep(); // enter name and do import tester.keyString("jgridstart_test_certificate.p12\n"); Thread.sleep(1000); saveScreenshot(new File(shotdir, prefix + "importexport05.png")); assertWindowname("jgridstart-password-entry-decrypt"); keyString(password + "\n"); guiSleep(); /* * Certificate details */ logger.info("Interactive testing scenario: Certificate details"); // certificate details view mainwnd.setSize(750, 480); System.setProperty("view.showdetails", "true"); URLLauncherCertificate.performAction("viewlist(false)", mainwnd); tester.key(KeyEvent.VK_F5); Thread.sleep(500); guiSleep(); saveScreenshot(new File(shotdir, prefix + "viewdetails01.png")); assertWindowname("jgridstart-main-window"); /* * Exit! */ logger.info("Interactive testing finished"); /* Quit does a {@link System.exit}, which JUnit doesn't like. The * error it gives is something like: * [junit] Test <testclass> FAILED (crashed) * So we leave it to the calling function to dispose of the window. */ // tester.key(new Integer('Q'), InputEvent.CTRL_MASK); } finally { guiSleep(); Thread.sleep(500); // for screenshot to complete ... FileUtils.recursiveDelete(tmphome); } // exit! return; }