@LocalData
  public void testMatrixBuildSummary() throws Exception {
    Hudson hudson = Hudson.getInstance();
    List<MatrixProject> projects = hudson.getAllItems(MatrixProject.class);
    MatrixProject testProject = null;
    for (MatrixProject project : projects) {
      System.out.println(project.getName());
      if (project.getName().equals("matrix-robot")) testProject = project;
    }
    if (testProject == null) fail("Couldn't find example project");
    Future<MatrixBuild> run = testProject.scheduleBuild2(0);

    while (!run.isDone()) {
      Thread.sleep(5);
    }
    Run lastBuild = testProject.getLastBuild();
    assertTrue("Build wasn't a success", lastBuild.getResult() == Result.SUCCESS);

    WebClient wc = getWebClient();
    HtmlPage page = wc.goTo("job/matrix-robot");
    WebAssert.assertElementPresentByXPath(
        page, "//div[@id='navigation']//a[@href='/job/matrix-robot/robot']");
    WebAssert.assertElementPresentByXPath(page, "//td[@id='main-panel']//img[@src='robot/graph']");

    page = wc.goTo("job/matrix-robot/3");
    WebAssert.assertElementPresentByXPath(
        page, "//div[@id='navigation']//a[@href='/job/matrix-robot/3/robot']");
    WebAssert.assertElementPresentByXPath(
        page, "//td[@id='main-panel']//h4[contains(.,'Robot Test Summary:')]");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='/job/matrix-robot/3/robot' and contains(text(),'Browse results')]");
  }
  public void testLogInWithOpenIDAndSignUp() throws Exception {
    openid = createServer();

    realm = new HudsonPrivateSecurityRealm(true);
    hudson.setSecurityRealm(realm);

    WebClient wc = new WebClient();
    // Workaround failing ajax requests to build queue
    wc.setThrowExceptionOnFailingAjax(false);

    // Login with OpenID as an unregistered user
    HtmlPage login = wc.goTo("federatedLoginService/openid/login?from=/");
    login
        .getDocumentElement()
        .getOneHtmlElementByAttribute("a", "title", "log in with OpenID")
        .click();
    HtmlForm loginForm = getFormById(login, "openid_form");
    loginForm.getInputByName("openid").setValueAttribute(openid.url);
    HtmlPage signUp = (HtmlPage) loginForm.submit();

    // Sign up user
    HtmlForm signUpForm =
        getFormByAction(signUp, "/securityRealm/createAccountWithFederatedIdentity");
    signUpForm.getInputByName("password1").setValueAttribute("x");
    signUpForm.getInputByName("password2").setValueAttribute("x");
    HtmlPage loggedIn = submit(signUpForm);

    assertNotNull(loggedIn.getAnchorByHref("/logout"));
    assertNotNull(loggedIn.getAnchorByHref("/user/aliceW"));

    wc.goTo("logout");

    // Re-login
    login(wc);
  }
  @LocalData
  public void testActionViewsWithNoRuns() throws Exception {
    WebClient wc = getWebClient();
    HtmlPage page = wc.goTo("job/robot/");

    WebAssert.assertElementPresentByXPath(
        page, "//div[@id='navigation']//a[@href='/job/robot/robot']");
    WebAssert.assertElementPresentByXPath(
        page, "//td[@id='main-panel']//p[contains(.,'No results available yet.')]");
    WebAssert.assertElementNotPresentByXPath(
        page, "//td[@id='main-panel']//img[@src='robot/graph']");

    page = wc.goTo("job/robot/robot/");
    WebAssert.assertTextPresent(page, "No robot results available yet!");
  }
  public void testDoDelete() throws Exception {
    UpdateSite target = new UpdateSite("test1", "http://example.com/test/update-center.json");

    Jenkins.getInstance().getUpdateCenter().getSites().clear();
    Jenkins.getInstance().getUpdateCenter().getSites().add(target);

    int initialSize = Jenkins.getInstance().getUpdateCenter().getSites().size();

    WebClient wc = new WebClient();

    HtmlPage deleteSitePage =
        wc.goTo(String.format("%s/%s/delete", UpdateSitesManager.URL, target.getId()));
    assertEquals(
        "UpdateSite must not be deleted yet.",
        initialSize,
        Jenkins.getInstance().getUpdateCenter().getSites().size());

    HtmlForm deleteSiteForm = deleteSitePage.getFormByName("deleteSiteForm");
    assertNotNull("There must be deleteSiteForm", deleteSiteForm);

    submit(deleteSiteForm);
    assertEquals(
        "UpdateSite must be deleted.",
        initialSize - 1,
        Jenkins.getInstance().getUpdateCenter().getSites().size());
  }
  public void testDoConfigure() throws Exception {
    UpdateSite target = new UpdateSite("test1", "http://example.com/test/update-center.json");

    // Multiple update site.
    Jenkins.getInstance().getUpdateCenter().getSites().clear();
    Jenkins.getInstance().getUpdateCenter().getSites().add(target);

    String originalId = target.getId();

    WebClient wc = new WebClient();

    HtmlPage editSitePage = wc.goTo(String.format("%s/%s", UpdateSitesManager.URL, target.getId()));

    HtmlForm editSiteForm = editSitePage.getFormByName("editSiteForm");
    assertNotNull("There must be editSiteForm", editSiteForm);

    String newId = "newId";
    String newUrl = "http://localhost/update-center.json";
    editSiteForm.getInputByName("_.id").setValueAttribute(newId);
    editSiteForm.getInputByName("_.url").setValueAttribute(newUrl);
    submit(editSiteForm);

    UpdateSite site = null;
    for (UpdateSite s : Jenkins.getInstance().getUpdateCenter().getSites()) {
      if (newId.equals(s.getId())) {
        site = s;
      }
      assertFalse("id must be updated(old one must not remain)", originalId.equals(s.getId()));
    }
    assertNotNull("id must be updated", site);
    assertEquals("url must be updated", newUrl, site.getUrl());
  }
  @LocalData
  public void testOldActionViewsWithData() throws Exception {
    WebClient wc = getWebClient();
    HtmlPage page = wc.goTo("job/oldrobotbuild/");
    WebAssert.assertElementPresentByXPath(
        page, "//div[@id='navigation']//a[@href='/job/oldrobotbuild/robot']");
    WebAssert.assertElementPresentByXPath(
        page, "//td[@id='main-panel']//h4[contains(.,'Latest Robot Results:')]");
    WebAssert.assertElementPresentByXPath(page, "//td[@id='main-panel']//img[@src='robot/graph']");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='/job/oldrobotbuild/1/robot' and contains(text(),'Browse results')]");
    HtmlTable table = page.getHtmlElementById("robot-summary-table");
    Assert.assertTrue(
        table
            .asXml()
            .replaceAll("\\s", "")
            .contains(
                "<tableclass=\"table\"id=\"robot-summary-table\"><tbodyalign=\"left\"><tr><th/><th>Total</th><th>Failed</th><th>Passed</th><th>Pass%</th></tr><tr><th>Criticaltests</th><tdclass=\"table-upper-row\"style=\"border-left:0px;\">8</td><tdclass=\"table-upper-row\"><spanclass=\"fail\">4</span></td><tdclass=\"table-upper-row\">4</td><tdclass=\"table-upper-row\">50.0</td></tr><tr><th>Alltests</th><tdstyle=\"border-left:0px;\">8</td><td><spanclass=\"fail\">4</span></td><td>4</td><td>50.0</td></tr></tbody></table>"));

    page = wc.goTo("job/oldrobotbuild/robot/");
    WebAssert.assertTitleEquals(page, "Testcases & Othercases Test Report");

    page = wc.goTo("job/oldrobotbuild/1/");
    WebAssert.assertElementPresentByXPath(
        page, "//div[@id='navigation']//a[@href='/job/oldrobotbuild/1/robot']");
    WebAssert.assertElementPresentByXPath(
        page, "//td[@id='main-panel']//h4[contains(.,'Robot Test Summary:')]");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='/job/oldrobotbuild/1/robot' and contains(text(),'Browse results')]");
    table = page.getHtmlElementById("robot-summary-table");
    Assert.assertTrue(
        table
            .asXml()
            .replaceAll("\\s", "")
            .contains(
                "<tableclass=\"table\"id=\"robot-summary-table\"><tbodyalign=\"left\"><tr><th/><th>Total</th><th>Failed</th><th>Passed</th><th>Pass%</th></tr><tr><th>Criticaltests</th><tdclass=\"table-upper-row\"style=\"border-left:0px;\">8</td><tdclass=\"table-upper-row\"><spanclass=\"fail\">4</span></td><tdclass=\"table-upper-row\">4</td><tdclass=\"table-upper-row\">50.0</td></tr><tr><th>Alltests</th><tdstyle=\"border-left:0px;\">8</td><td><spanclass=\"fail\">4</span></td><td>4</td><td>50.0</td></tr></tbody></table>"));

    page = wc.goTo("job/oldrobotbuild/1/robot/");
    WebAssert.assertTitleEquals(page, "Testcases & Othercases Test Report");
  }
 @LocalData
 public void testMatrixBuildReportLinks() throws Exception {
   WebClient wc = getWebClient();
   HtmlPage page = wc.goTo("job/matrix-robot/FOO=bar/2");
   WebAssert.assertElementPresentByXPath(
       page,
       "//td[@id='main-panel']//a[@href='/job/matrix-robot/FOO=bar/2/robot' and contains(.,'Browse results')]");
   WebAssert.assertElementPresentByXPath(
       page,
       "//td[@id='main-panel']//a[@href='/job/matrix-robot/FOO=bar/2/robot/report/report.html' and contains(.,'Open report.html')]");
 }
  /** Associates the OpenID identity of the user with {@link #realm}. */
  private void associateUserWithOpenId(User u) throws Exception {
    WebClient wc = new WebClient().login(u.getId(), u.getId() /*assumes password==name*/);

    // Associate an OpenID with an existing user
    HtmlPage associated =
        wc.goTo("federatedLoginService/openid/startAssociate?openid=" + openid.url);
    assertTrue(
        associated.getDocumentURI().endsWith("federatedLoginService/openid/onAssociationSuccess"));
    OpenIdUserProperty p = u.getProperty(OpenIdUserProperty.class);
    assertEquals(1, p.getIdentifiers().size());
    assertEquals(openid.getUserIdentity(), p.getIdentifiers().iterator().next());
  }
  private void login(WebClient wc) throws Exception {
    HtmlPage login = wc.goTo("federatedLoginService/openid/login?from=/");
    login
        .getDocumentElement()
        .getOneHtmlElementByAttribute("a", "title", "log in with OpenID")
        .click();
    HtmlForm loginForm = getFormById(login, "openid_form");
    loginForm.getInputByName("openid").setValueAttribute(openid.url);
    HtmlPage loggedIn = (HtmlPage) loginForm.submit();

    assertNotNull(loggedIn.getAnchorByHref("/logout"));
    assertNotNull(loggedIn.getAnchorByHref("/user/aliceW"));
  }
  @LocalData
  public void testMissingReportFileWithOld() throws Exception {
    Hudson hudson = Hudson.getInstance();
    List<Project> projects = hudson.getProjects();
    Project testProject = null;
    for (Project project : projects) {
      if (project.getName().equals("oldrobotbuild")) testProject = project;
    }
    if (testProject == null) fail("Couldn't find example project");

    WebClient wc = getWebClient();

    File buildRoot = testProject.getLastBuild().getRootDir();
    File robotHtmlReport = new File(buildRoot, RobotPublisher.FILE_ARCHIVE_DIR + "/report.html");
    if (!robotHtmlReport.delete()) fail("Unable to delete report directory");

    HtmlPage page = wc.goTo("job/oldrobotbuild/robot/");
    WebAssert.assertTextPresent(page, "No Robot html report found!");

    page = wc.goTo("job/oldrobotbuild/1/robot/");
    WebAssert.assertTextPresent(page, "No Robot html report found!");
  }
  @LocalData
  public void testReadOnlyConfigAccessWithPermissionEnabled() throws Exception {
    setPermissionEnabled(true);

    AuthorizationStrategy as = jenkins.getAuthorizationStrategy();
    assertTrue(
        "Expecting GlobalMatrixAuthorizationStrategy",
        (as instanceof GlobalMatrixAuthorizationStrategy));
    GlobalMatrixAuthorizationStrategy gas = (GlobalMatrixAuthorizationStrategy) as;
    assertTrue(
        "Charlie should have extended read for this test",
        gas.hasExplicitPermission("charlie", Item.EXTENDED_READ));

    WebClient wc = new WebClient().login("charlie", "charlie");
    HtmlPage page = wc.goTo("job/a/configure");
    HtmlForm form = page.getFormByName("config");
    HtmlButton saveButton = getButtonByCaption(form, "Save");
    assertNull(saveButton);
  }
  @LocalData
  public void testReportPage() throws Exception {
    Hudson hudson = Hudson.getInstance();
    List<Project> projects = hudson.getProjects();
    Project testProject = null;
    for (Project project : projects) {
      if (project.getName().equals("robot")) testProject = project;
    }
    if (testProject == null) fail("Couldn't find example project");
    Future<Run> run = testProject.scheduleBuild2(0);

    while (!run.isDone()) {
      Thread.sleep(5);
    }
    Run lastBuild = testProject.getLastBuild();
    assertTrue("Build wasn't a success", lastBuild.getResult() == Result.SUCCESS);

    WebClient wc = getWebClient();
    HtmlPage page = wc.goTo("job/robot/robot/");
    WebAssert.assertTextPresent(page, "Robot Framework test results");
    WebAssert.assertTextPresent(page, "4 failed tests, 4 critical");
    WebAssert.assertTextPresent(page, "Tests took 0:00:00.009 (+0:00:00.009)");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='Testcases%20&%20Othercases/Testcases/Not%20equal' and contains(.,'Testcases & Othercases.Testcases.Not equal')]");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='Testcases%20&%20Othercases/Othercases' and contains(.,'Testcases & Othercases.Othercases')]");

    page = wc.goTo("job/robot/1/robot/report/");
    WebAssert.assertElementPresentByXPath(
        page, "//td[@id='main-panel']//a[@href='output.xml' and contains(.,'output.xml')]");

    page = wc.goTo("job/robot/1/robot/Testcases%20&%20Othercases");
    WebAssert.assertTextPresent(page, "4 failed tests, 4 critical");
    WebAssert.assertTextPresent(page, "Tests took 0:00:00.009 (+0:00:00.009)");
    WebAssert.assertTextNotPresent(page, "All Testcases");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='Testcases/Not%20equal' and contains(.,'Testcases.Not equal')]");
    WebAssert.assertElementPresentByXPath(
        page, "//td[@id='main-panel']//a[@href='Othercases' and contains(.,'Othercases')]");

    page = wc.goTo("job/robot/1/robot/Testcases%20&%20Othercases/Othercases");
    WebAssert.assertTextPresent(page, "2 failed tests, 2 critical");
    WebAssert.assertTextPresent(page, "Tests took 0:00:00.005 (+0:00:00.005)");
    WebAssert.assertTextPresent(page, "All Testcases");
    WebAssert.assertElementPresentByXPath(
        page, "//td[@id='main-panel']//a[@href='Not%20equal' and contains(.,'Not equal')]");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='Contains%20string' and contains(.,'Contains string')]");

    page = wc.goTo("job/robot/1/robot/Testcases%20&%20Othercases/Othercases/Not%20equal");
    WebAssert.assertTextPresent(page, "Critical test case: \"Not equal\"");
    WebAssert.assertTextPresent(page, "Failed!");
    WebAssert.assertTextPresent(page, "Error message:");
    WebAssert.assertTextPresent(page, "Hello, world! != Good bye, world!");
    WebAssert.assertTextPresent(page, "Test took 0:00:00.001 (+0:00:00.001)");
    WebAssert.assertElementPresentByXPath(
        page, "//td[@id='main-panel']//img[@src='durationGraph']");

    page = wc.goTo("job/robot/1/robot/Testcases%20&%20Othercases/Othercases/Contains%20string");
    WebAssert.assertTextPresent(page, "Passed!");
    WebAssert.assertTextNotPresent(page, "Error message:");
  }
  @LocalData
  public void testSummariesWithData() throws Exception {
    Hudson hudson = Hudson.getInstance();
    List<Project> projects = hudson.getProjects();
    Project testProject = null;
    for (Project project : projects) {
      if (project.getName().equals("robot")) testProject = project;
    }
    if (testProject == null) fail("Couldn't find example project");
    Future<Run> run = testProject.scheduleBuild2(0);

    while (!run.isDone()) {
      Thread.sleep(5);
    }

    Run lastBuild = testProject.getLastBuild();
    assertTrue("Build wasn't a success", lastBuild.getResult() == Result.SUCCESS);

    WebClient wc = getWebClient();

    HtmlPage page = wc.goTo("job/robot/");
    WebAssert.assertElementPresentByXPath(
        page, "//div[@id='navigation']//a[@href='/job/robot/robot']");
    WebAssert.assertElementPresentByXPath(
        page, "//td[@id='main-panel']//h4[contains(.,'Latest Robot Results:')]");
    WebAssert.assertElementPresentByXPath(page, "//td[@id='main-panel']//img[@src='robot/graph']");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='/job/robot/1/robot' and contains(text(),'Browse results')]");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='/job/robot/1/robot/report/report.html' and contains(text(), 'Open report.html')]");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='/job/robot/1/robot/report/log.html' and contains(text(), 'Open log.html')]");

    HtmlTable table = page.getHtmlElementById("robot-summary-table");
    Assert.assertTrue(
        table
            .asXml()
            .replaceAll("\\s", "")
            .contains(
                "<tableclass=\"table\"id=\"robot-summary-table\"><tbodyalign=\"left\"><tr><th/><th>Total</th><th>Failed</th><th>Passed</th><th>Pass%</th></tr><tr><th>Criticaltests</th><tdclass=\"table-upper-row\"style=\"border-left:0px;\">8</td><tdclass=\"table-upper-row\"><spanclass=\"fail\">4</span></td><tdclass=\"table-upper-row\">4</td><tdclass=\"table-upper-row\">50.0</td></tr><tr><th>Alltests</th><tdstyle=\"border-left:0px;\">8</td><td><spanclass=\"fail\">4</span></td><td>4</td><td>50.0</td></tr></tbody></table>"));

    page = wc.goTo("job/robot/1/");
    WebAssert.assertElementPresentByXPath(
        page, "//div[@id='navigation']//a[@href='/job/robot/1/robot']");
    WebAssert.assertElementPresentByXPath(
        page, "//td[@id='main-panel']//h4[contains(.,'Robot Test Summary:')]");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='/job/robot/1/robot' and contains(text(),'Browse results')]");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='/job/robot/1/robot/report/report.html' and contains(text(), 'Open report.html')]");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='/job/robot/1/robot/report/log.html' and contains(text(), 'Open log.html')]");
    table = page.getHtmlElementById("robot-summary-table");
    Assert.assertTrue(
        table
            .asXml()
            .replaceAll("\\s", "")
            .contains(
                "<tableclass=\"table\"id=\"robot-summary-table\"><tbodyalign=\"left\"><tr><th/><th>Total</th><th>Failed</th><th>Passed</th><th>Pass%</th></tr><tr><th>Criticaltests</th><tdclass=\"table-upper-row\"style=\"border-left:0px;\">8</td><tdclass=\"table-upper-row\"><spanclass=\"fail\">4</span></td><tdclass=\"table-upper-row\">4</td><tdclass=\"table-upper-row\">50.0</td></tr><tr><th>Alltests</th><tdstyle=\"border-left:0px;\">8</td><td><spanclass=\"fail\">4</span></td><td>4</td><td>50.0</td></tr></tbody></table>"));
  }