Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Click not triggering all events #403

Closed
skyhirider opened this issue Nov 5, 2021 · 3 comments
Closed

Click not triggering all events #403

skyhirider opened this issue Nov 5, 2021 · 3 comments

Comments

@skyhirider
Copy link
Contributor

skyhirider commented Nov 5, 2021

Got another issue for you, test case in hand :)

When clicking via Selenium + Htmlunit only the click javascript event triggers, but doing the same in Firefox triggers mouse up, down and click.

The test case I tried:

class HtmlUnitClickTest {

    private WebDriver ffDriver;
    private WebDriver huDriver;
    private final static String buttonPage = "<button id=\"testButton\">Click me</button>\n" +
            "<div id=\"logArea\"></div>\n" +
            "<script>\n" +
            "    const button = document.getElementById('testButton');\n" +
            "    const div = document.getElementById('logArea');\n" +
            "\n" +
            "    button.addEventListener('mousedown', e => {\n" +
            "        console.log('up');\n" +
            "        div.innerHTML += \"up \";\n" +
            "    });\n" +
            "    button.addEventListener('mouseup', e => {\n" +
            "        console.log('down');\n" +
            "        div.innerHTML += \"down \";\n" +
            "    });\n" +
            "    button.addEventListener('click', e => {\n" +
            "        console.log('click');\n" +
            "        div.innerHTML += \"click \";\n" +
            "    });\n" +
            "\n" +
            "\n" +
            "</script>";


    @BeforeEach
    void initDrivers() {
        System.setProperty("webdriver.gecko.driver", "c:\\path\\geckodriver.exe");
        FirefoxOptions options = new FirefoxOptions();
        options.setLogLevel(FirefoxDriverLogLevel.FATAL);
        ffDriver = new FirefoxDriver(options);
        huDriver = new HtmlUnitDriver(BrowserVersion.FIREFOX, true) {
            @Override
            protected WebClient modifyWebClient(WebClient client) {
                final WebClient webClient = super.modifyWebClient(client);
                WebClientOptions options = webClient.getOptions();
                options.setCssEnabled(true);
                options.setThrowExceptionOnScriptError(false);
                webClient.setJavaScriptErrorListener(new SilentJavaScriptErrorListener());

                return webClient;
            }
        };
    }

    @AfterEach
    void closeDrivers(){
        ffDriver.close();
        huDriver.close();
    }

    @Test
    void givenFFandHtmlUnit_whenClicking_registerThreeEvents() {
        Assertions.assertEquals(testClicks(ffDriver), testClicks(huDriver));
    }

    private String testClicks(WebDriver driver) {
        driver.get("data:text/html;charset=utf-8," + buttonPage);
        driver.findElement(By.id("testButton")).click();
        return driver.findElement(By.id("logArea")).getText();
    }

}
@rbri
Copy link
Member

rbri commented Nov 6, 2021 via email

@rbri
Copy link
Member

rbri commented Nov 8, 2021

Wow, this was by far the most interesting puzzle to solve for the last weeks.
The problem was the handling of the data url - the plus sign in '+=' was replaced by a blank during decoding of the data url.

Many thanks for this report.
As always a new snapshot will be available soon - watch out on twitter.

@rbri rbri closed this as completed Nov 8, 2021
@skyhirider
Copy link
Contributor Author

skyhirider commented Nov 8, 2021

Interesting. I noticed this when running a bot on another site so though the events were broken in general as clicking an element was not selecting it (site is listening for down, up, click). Will need to re-investigate how that case is unique.

Thanks for the fix! The data url is really handy for test cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants