hollingsworthd / jBrowserDriver

A programmable, embeddable web browser driver compatible with the Selenium WebDriver spec -- headless, WebKit-based, pure Java
Other
812 stars 143 forks source link

Actions.contextClick( e ).perform() passed as normal click #360

Open therealryan opened 4 years ago

therealryan commented 4 years ago

As title - invoking contextClick results in the observable behaviour of a normal click. The attached test passes when the wrongBehaviour boolean is true, but it should pass when it's false.

File attachment seems to be broken, so: pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.foo.bar</groupId>
    <artifactId>contextmenu</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>contextmenu</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.machinepublishers</groupId>
            <artifactId>jbrowserdriver</artifactId>
            <version>1.1.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.nanohttpd</groupId>
            <artifactId>nanohttpd-webserver</artifactId>
            <version>2.3.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

src/test/java/com/contextmenu/ContextMenuTest.java:


package com.contextmenu;

import java.io.File;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import com.machinepublishers.jbrowserdriver.JBrowserDriver;

import fi.iki.elonen.SimpleWebServer;

public class ContextMenuTest {

    private static SimpleWebServer server;

    @BeforeClass
    public static void serve() throws Exception {
        server = new SimpleWebServer( "127.0.0.1", 8080, new File( "src/test/resources" ), true );
        server.start();
    }

    @Test
    public void test() {
        JBrowserDriver driver = new JBrowserDriver();
        Actions actions = new Actions( driver );

        driver.get( "http://127.0.0.1:8080/page.html" );

        WebElement clickable = driver.findElementById( "clickable" );
        WebElement left = driver.findElementById( "lc_count" );
        WebElement right = driver.findElementById( "rc_count" );

        Assert.assertEquals( "0", left.getText() );
        Assert.assertEquals( "0", right.getText() );

        actions.click( clickable ).perform();

        Assert.assertEquals( "1", left.getText() );
        Assert.assertEquals( "0", right.getText() );

        actions.contextClick( clickable ).perform();

        boolean wrongBehaviour = true;

        if ( wrongBehaviour ) {
            // this is what *actually* happens
            Assert.assertEquals( "2", left.getText() );
            Assert.assertEquals( "0", right.getText() );
        }
        else {
            // this is what *should* happen
            Assert.assertEquals( "1", left.getText() );
            Assert.assertEquals( "1", right.getText() );
        }
    }

    @AfterClass
    public static void shutdown() {
        server.stop();
    }
}

src/test/resources/page.html:

<html>
<head>
<script type="text/javascript">
    document.addEventListener('DOMContentLoaded', function() {
        document.getElementById("clickable").addEventListener('click',
                function(event) {
                    const count = document.getElementById('lc_count');
                    count.textContent = parseInt(count.textContent) + 1;
                });
        document.getElementById("clickable").addEventListener('contextmenu',
                function(event) {
                    event.preventDefault();
                    const count = document.getElementById('rc_count');
                    count.textContent = parseInt(count.textContent) + 1;
                    return false;
                });
    });
</script>
</head>
<body>
    <div id="clickable">Click me!</div>
    <div>
        Left click count = <span id="lc_count">0</span>
    </div>
    <div>
        Right click count = <span id="rc_count">0</span>
    </div>
</body>
</html>