Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

java - htmlunit individual test pass but running in test suite fail

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...