I am using HtmlUnit for testing some SAML SSO flow. I have multiple tests written and they work fine when running individually. When I run them in a test suite or run all the tests. Only 1 works fine and other fails. The problem seems to be that once the first page is requested using webclient.getPage('url')
and a button is clicked to be redirected to next page. The click doesnt happen. When running the tests individually it clicks fine and works.
I am using htmlunit version 2.46.0 as follows the only static data shared between tests is the URLS and USERNAME AND PASSWORD:
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
public void test1() throws Exception {
try (final WebClient webClient = new WebClient(BrowserVersion.CHROME)) {
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setRedirectEnabled(true);
webClient.getCookieManager().setCookiesEnabled(true);
final HtmlPage loginPage= webClient.getPage(URL);
assertThat(loginPage.getTitleText()).isEqualTo(LOGIN_PAGE_TITLE);
HtmlButtonInput samlLoginButton = loginPage.getHtmlElementById("test");
samlLoginButton.click();
webClient.waitForBackgroundJavaScriptStartingBefore(10000);
webClient.waitForBackgroundJavaScript(60000);
HtmlPage nextLoginPage = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
assertThat(nextLoginPage.getUrl().toString()).isEqualTo(NEXT_LOGIN_URL);
}
}
public void test2() throws Exception {
//similar to above example
}
As I am using try with resources in java webclient.close()
is called in the end.
Is there some thing more I need to do?
I also tried to setup @BeforeEach
and @AfterEach
in Junit to setup the webclient
and close
it. But the result is same.
How can I stimulate the same behavior in test suite, as running individual test?
Thanks for reading.
question from:
https://stackoverflow.com/questions/65944923/htmlunit-individual-test-pass-but-running-in-test-suite-fail 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…