Search code examples
javaswingseleniummarathontesting

Context Click doesn't work with Marathon Java Driver


I am currently trying to automate JMeter (as an sample application) using Marathon Java Drivers. I am able to open JMeter but when i try to right click on Test Plan under the left pane, i am not able to do so. Can you please tell me what i am doing wrong. Thanks.

package javadriver;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Window;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import net.sourceforge.marathon.javadriver.JavaDriver;
import net.sourceforge.marathon.javadriver.JavaProfile;
import net.sourceforge.marathon.javadriver.JavaProfile.LaunchMode;
import net.sourceforge.marathon.javadriver.JavaProfile.LaunchType;
import org.openqa.selenium.support.PageFactory;

public class JavaDriverTest {



    public static void main(String[] args) {
        // TODO Auto-generated method stub

        JavaProfile profile = new JavaProfile(LaunchMode.JAVA_COMMAND_LINE);
        profile.setLaunchType(LaunchType.SWING_APPLICATION);
        profile.setMainClass("org.apache.jmeter.NewDriver");
        profile.addClassPath("C:\\apache-jmeter-5.1.1\\bin\\ApacheJMeter.jar");
        profile.setWorkingDirectory("C:\\\\apache-jmeter-5.1.1\\\\bin");


        WebDriver driver = new JavaDriver(profile);

        Window window = driver.manage().window();
        window.maximize();

        WebElement elementLocator = driver.findElement(By.cssSelector("label[text='Test Plan']"));
        new Actions(driver).moveToElement(elementLocator, 50, 25).contextClick(elementLocator).build().perform();   
        //new Actions(driver).clickAndHold(elementLocator);
        //new Actions(driver).contextClick(elementLocator).perform();



        //driver.quit();


    }

}

Solution

    1. Looks like you are trying to get the components before the application is completely opened. So Marathon is not able to find the component.
    2. The right click you wanted to perform is a tree item so you need to find the tree and then get node from that.

    Check this link for how to locate different types of components where for Swing and JavaFX both are defined. https://marathontesting.com/marathonite-user-guide/selenium-webdriver-bindings/

    Please check the following code this will give a better understanding.

    package javadriver;
    
    import org.openqa.selenium.By;
    
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.interactions.Actions;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;
    
    import net.sourceforge.marathon.javadriver.JavaDriver;
    import net.sourceforge.marathon.javadriver.JavaProfile;
    import net.sourceforge.marathon.javadriver.JavaProfile.LaunchMode;
    import net.sourceforge.marathon.javadriver.JavaProfile.LaunchType;
    
    public class JMeterTest {
    
    private JavaDriver driver;
    
    @BeforeTest
    public void createJavaProfile_ExecJarLauncher() {
        // We prefer Executable jar rather than using Java Command Line
        // launcher as the application loads with a blurb and then the main
        // window opens. So that we can specify the Start window from where the
        // operations are performed. Other wise after creating driver you need to put sleep until the main window is opens.
    
        JavaProfile profile = new JavaProfile(LaunchMode.EXECUTABLE_JAR);
        profile.setLaunchType(LaunchType.SWING_APPLICATION);
        profile.setExecutableJar("/Users/adityakarra/Projects/apache-jmeter-5.2.1/bin/ApacheJMeter.jar");
        profile.setWorkingDirectory("/Users/adityakarra/Projects/apache-jmeter-5.2.1/bin");
        // As the application title differs based on the Version number we have
        // passed regex to match the window title.
        profile.setStartWindowTitle("/^Apache JMeter.*");
    
        driver = new JavaDriver(profile);
    
        // Finally printing the window title after every thing is loaded.
        System.out.println("Window Title ::: " + driver.getTitle());
    }
    
    @Test
    public void getTreeItem() throws InterruptedException {
        // As the context menu you wanted to click is a tree item. So find the
        // tree initally.
        WebElement tree = driver.findElementByTagName("tree");
        // Now for getting the tree item we use select by properties and get the
        // node.
        WebElement node = tree.findElement(By.cssSelector(".::select-by-properties('{\"select\":\"/Test Plan\"}')"));
        // Performing right click on the tree item
        new Actions(driver).moveToElement(node, 50, 25).contextClick(node).build().perform();
        // Clicking on one of the menu items in the context menu.
        driver.findElementByCssSelector("menu-item[text='Disable']").click();
        new Actions(driver).moveToElement(node, 50, 25).contextClick(node).build().perform();
        driver.findElementByCssSelector("menu-item[text='Enable']").click();
    }
    
    @AfterTest
    public void tearDown() {
        if (driver != null)
            driver.quit();
    }
    
    }
    

    Note: I'm one of the contributor for Marathon.