Skip to content

Commit

Permalink
Convert tests to use selenium
Browse files Browse the repository at this point in the history
  • Loading branch information
volosied committed Feb 23, 2023
1 parent 26710e7 commit 77f83d5
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,54 +33,45 @@

public class AjaxTestsIT extends BaseITNG {


@Test
public void ajaxTagWrappingTest() throws Exception {
HtmlPage page = webClient.getPage(webUrl + "/faces/tagwrapper/ajaxTagWrap.xhtml");
WebPage page = getPage("faces/tagwrapper/ajaxTagWrap.xhtml");

// First we'll check the first page was output correctly
HtmlSpan span1 = (HtmlSpan) page.getElementById("out1");
assertNotNull(span1);
assertEquals("0", span1.asNormalizedText());
WebElement span1 = page.findElement(By.id("out1"));
assertEquals("0", span1.getText());

HtmlSpan span2 = (HtmlSpan) page.getElementById("checkedvalue");
assertNotNull(span2);
assertEquals("false", span2.asNormalizedText());
WebElement span2 = page.findElement(By.id("checkedvalue"));
assertEquals("false", span2.getText());

HtmlSpan span3 = (HtmlSpan) page.getElementById("outtext");
assertNotNull(span3);
assertEquals("", span3.asNormalizedText());
WebElement span3 = page.findElement(By.id("outtext"));
assertEquals("", span3.getText());

// Submit the ajax request
HtmlInput button1 = (HtmlInput) page.getElementById("button1");
WebElement button1 = page.findElement(By.id("button1"));
assertNotNull(button1);
page = button1.click();
button1.click();

webClient.waitForBackgroundJavaScript(3000);
page.waitReqJs();

// Check that the ajax request succeeds - eventually.
span1 = (HtmlSpan) page.getElementById("out1");
assertEquals("1",span1.asNormalizedText());
// Check that the ajax request succeeds
span1 = page.findElement(By.id("out1"));
assertEquals("1", span1.getText());

// // Check on the text field
HtmlInput intext = (HtmlInput) page.getElementById("intext");
assertNotNull(intext);
intext.focus();
intext.type("test");
intext.blur();
webClient.waitForBackgroundJavaScript(3000);

span3 = (HtmlSpan) page.getElementById("outtext");
assertNotNull(span3);
assertEquals("test", span3.asNormalizedText());
// Check on the text field
WebElement intext = page.findElement(By.id("intext"));
intext.sendKeys("test");
intext.sendKeys(TAB); // click out of intext to force onchange event
page.waitReqJs();
assertTrue(page.findElement(By.id("outtext")).getText().equals("test"));

// Check the checkbox
HtmlInput checkbox = (HtmlInput) page.getElementById("checkbox");
assertNotNull(checkbox);
page = (HtmlPage) checkbox.setChecked(true);
checkbox = (HtmlInput) page.getElementById("checkbox");
assertTrue(checkbox.isChecked());

// Check for "true" in outtext? Not originally tested (maybe due to HTMLUnit issues?). Will add to selenium run.
WebElement checkbox = page.findElement(By.id("checkbox"));
checkbox.click();
page.waitReqJs();
span2 = page.findElement(By.id("checkedvalue"));
assertEquals("true", span2.getText());

}// End ajaxAllKeywordTest

Expand All @@ -98,9 +89,9 @@ public void ajaxAllKeywordTest() throws Exception {
String EXPECTED = "testtext";

List<String> urls = new ArrayList<String>();
urls.add(webUrl + "/faces/keyword/ajaxAllKeyword1.xhtml");
urls.add(webUrl + "/faces/keyword/ajaxAllKeyword2.xhtml");
urls.add(webUrl + "/faces/keyword/ajaxAllKeyword3.xhtml");
urls.add("faces/keyword/ajaxAllKeyword1.xhtml");
urls.add("faces/keyword/ajaxAllKeyword2.xhtml");
urls.add("faces/keyword/ajaxAllKeyword3.xhtml");

String buttonId = "form:allKeyword";
String spanId = "form:out";
Expand All @@ -123,9 +114,9 @@ public void ajaxThisKeywordTest() throws Exception {
String EXPECTED = "testtext";

List<String> urls = new ArrayList<String>();
urls.add(webUrl + "/faces/keyword/ajaxThisKeyword1.xhtml");
urls.add(webUrl + "/faces/keyword/ajaxThisKeyword2.xhtml");
urls.add(webUrl + "/faces/keyword/ajaxThisKeyword3.xhtml");
urls.add("faces/keyword/ajaxThisKeyword1.xhtml");
urls.add("faces/keyword/ajaxThisKeyword2.xhtml");
urls.add("faces/keyword/ajaxThisKeyword3.xhtml");

String buttonId = "form:thisKeyword";
String spanId = "form:out";
Expand All @@ -141,37 +132,39 @@ public void ajaxThisKeywordTest() throws Exception {
*
* @since 2.0
*/
@Test
public void ajaxFormKeywordTest() throws Exception {

String EXPECTED = "testtext";

List<String> urls = new ArrayList<String>();
urls.add(webUrl + "/faces/keyword/ajaxFormKeyword1.xhtml");
urls.add(webUrl + "/faces/keyword/ajaxFormKeyword2.xhtml");
urls.add(webUrl + "/faces/keyword/ajaxFormKeyword3.xhtml");
urls.add("faces/keyword/ajaxFormKeyword1.xhtml");
urls.add("faces/keyword/ajaxFormKeyword2.xhtml");
urls.add("faces/keyword/ajaxFormKeyword3.xhtml");

String buttonId = "form:formKeyword";
String spanId = "form:out";

this.validateKeyword(urls, buttonId, spanId, EXPECTED);
} // End ajaxThisKeywordTest

/**
/**
* @testName: ajaxNoneKeywordTest
* @assertion_ids: PENDING
* @test_Strategy: Unsure the keyword 'none' works correctly with the f:ajax
* tag as value to 'execute' and 'render' attributes.
*
* @since 2.0
*/
@Test
public void ajaxNoneKeywordTest() throws Exception {

String EXPECTED = "testtext";

List<String> urls = new ArrayList<String>();
urls.add(webUrl + "/faces/keyword/ajaxNoneKeyword1.xhtml");
urls.add(webUrl + "/faces/keyword/ajaxNoneKeyword2.xhtml");
urls.add(webUrl + "/faces/keyword/ajaxNoneKeyword3.xhtml");
urls.add("faces/keyword/ajaxNoneKeyword1.xhtml");
urls.add("faces/keyword/ajaxNoneKeyword2.xhtml");
urls.add("faces/keyword/ajaxNoneKeyword3.xhtml");

String buttonId = "form:noneKeyword";
String spanId = "form:out";
Expand All @@ -189,18 +182,16 @@ public void ajaxNoneKeywordTest() throws Exception {
*
* @since 2.0
*/
@Ignore("Skipped in Old TCK for EE10")
@Test
public void ajaxPDLResourceTest() throws Exception {

HtmlPage page = webClient.getPage( webUrl + "/faces/jsresource/pdlApproach.xhtml");
HtmlScript script = (HtmlScript) page.getElementsByTagName("script").get(0);
assertNotNull(script);
WebPage page = getPage("faces/jsresource/pdlApproach.xhtml");
WebElement script = page.findElement(By.tagName("script"));

// Test by Resource name.
assertTrue(script.getSrcAttribute().contains("faces.js"));
// Test by Resource Library name.
assertTrue(script.getSrcAttribute().contains("jakarta.faces"));
// Verify Resource name.
assertTrue(script.getDomAttribute​("src").contains("faces.js"));
// Verify Resource Library name.
assertTrue(script.getDomAttribute​("src").contains("jakarta.faces"));

}// End ajaxPDLResourceTest

Expand All @@ -210,25 +201,22 @@ private void validateKeyword(List<String> urls, String buttonId,
String spanId, String expectedValue) throws Exception {

for (String url : urls) {
HtmlPage page = webClient.getPage(url);
HtmlSpan output = (HtmlSpan) page.getElementById(spanId);
assertNotNull(output);
WebPage page = getPage(url);

WebElement output = page.findElement(By.id(spanId));

// First we'll check the first page was output correctly
assertEquals(expectedValue, output.asNormalizedText());
assertEquals(expectedValue, output.getText());

// Submit the ajax request
HtmlSubmitInput button = (HtmlSubmitInput) page.getElementById(buttonId);
page = button.click();

webClient.waitForBackgroundJavaScript(3000);
WebElement button = page.findElement(By.id(buttonId));
button.click();
page.waitReqJs();

// Check that the ajax request succeeds - if the page is rewritten,
// this will be the same
output = (HtmlSpan) page.getElementById(spanId);
assertNotNull(output);
assertEquals(expectedValue, output.asNormalizedText());
output = page.findElement(By.id(spanId));
assertEquals(expectedValue, output.getText());
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@
import jakarta.faces.event.ActionListener;
import jakarta.servlet.http.HttpServletResponse;

@jakarta.inject.Named("ActionListener") @jakarta.enterprise.context.SessionScoped
@jakarta.inject.Named("ActionListener") @jakarta.enterprise.context.RequestScoped
public class SimpleActionListener implements ActionListener, Serializable {

private static final long serialVersionUID = -2123380871083456327L;


/* Selenium doens't allow response header inspection, so the result is set here and displayed on the page */
private String result = "";

/**
* <p>
* Invoked when the action described by the specified
Expand All @@ -62,15 +66,27 @@ public void processAction(ActionEvent event) throws AbortProcessingException {
if (expectedId == null) {
response.addHeader("actionEvent",
"Test error. Can't find expected" + " component ID.");
// Selenium Result ID Check
result = "FAILED. Can't find expected component ID"
} else {

if (!expectedId.equals(component.getId())) {
response.addHeader("actionEvent", "Expected component ID '" + expectedId
+ "', received: '" + component.getId() + '\'');
// For Selenium
result = "FAILED. " + "Expected component ID '" + expectedId
+ "', received: '" + component.getId() + '\'';
} else {
response.addHeader("actionEventOK", "PASSED");
// Selenium
result = "PASSED";
}
}
}

public String getResult() {
return result;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@
type="submit"/>
<input type="hidden" name="expectedId" value="link1"/>
</h:form>
<h:outputText id="result" value="#{ActionListener.result}"/>
</body>
</html>
Loading

0 comments on commit 77f83d5

Please sign in to comment.