java - 上下文单击不适用于 Marathon Java 驱动程序

标签 java swing selenium marathontesting

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

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. 您想要执行的右键单击是一个树项,因此您需要找到该树,然后从中获取节点。

检查此链接,了解如何查找定义了 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();
}

}

注意:我是 Marathon 的贡献者之一。

关于java - 上下文单击不适用于 Marathon Java 驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60428037/

相关文章:

java - 无法从Java客户端创建HBase模式

java - 选择两个 JTable 中的多个项目

java - 如何设置并锁定JScrollpane到底部?

Java 和 Selenium 显示 Google 搜索结果的最佳方式

java - 在不使用 testng.xml 的情况下使用 Selenium 和 TestNG 进行并行跨浏览器测试

css - Selenium 无法识别页面上第二个元素的 CSS 路径

java - 如何自定义java日志文件以仅包含最多N条日志记录而不是大小(以字节为单位)

java - 如何在 Java JAR 内部包含一个 SQL 语句文件?

Java 正则表达式检查包含网址的字符串

java - 在 Swing 中使用 ProgressMonitorInputStream 监视压缩文件解压缩时 UI 未更新