Context Click 不适用于 Marathon Java 驱动程序

Context Click doesn't work with Marathon Java Driver

我目前正在尝试使用 Marathon Java 驱动程序自动化 JMeter(作为示例应用程序)。我可以打开 JMeter,但是当我尝试右键单击左窗格下的测试计划时,我无法这样做。你能告诉我我做错了什么吗?谢谢

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();


    }

}
  1. 看起来您正试图在应用程序完全打开之前获取组件。所以 Marathon 无法找到该组件。
  2. 您要执行的右键单击是树项,因此您需要找到树,然后从中获取节点。

检查此 link 以了解如何在定义了 Swing 和 JavaFX 的地方找到不同类型的组件。 https://marathontesting.com/marathonite-user-guide/selenium-webdriver-bindings/

请检查下面的代码,这样可以更好地理解。

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();
}

}

注意:我是马拉松的贡献者之一。