【问题标题】:Calling another method when Parallel Testing returns java.lang.NullPointerException当并行测试返回 java.lang.NullPointerException 时调用另一个方法
【发布时间】:2019-01-16 17:37:07
【问题描述】:

我在使用 Java、Selenium 和 TestNG 执行并行测试时遇到了问题。我有 2 种测试方法可以在 google 中搜索两个不同的关键字。我想要两种测试方法都调用的第三种方法,以避免重复类似的代码。

public class googleTestClass extends Methods{

@Test
public void executeGoogle() throws InterruptedException {
    googleTestClass object;
    object = new googleTestClass();
    object.goToURL("https://www.google.com");
    object.enterValue("name","q","google test 1");
}

@Test
public void test1() throws InterruptedException {

    googleTestClass object1;
    object1 = new googleTestClass();
    object1.launchBrowser();
    object1.executeGoogle();
}

@Test
public void test2() throws InterruptedException {

    googleTestClass object2;
    object2 = new googleTestClass();
    object2.launchBrowser();
    object2.executeGoogle();
}
}

当我的代码命中 object1.executeGoogle();和 object2.executeGoogle();命令,它返回一个 java.lang.NullPointerException。我知道错误与对象有关,但我不确定如何继续。

这里是正在使用的其他类。

方法类:

// import statements

public class Methods {

public WebDriver driver;

public void launchBrowser() {

     System.setProperty("webdriver.chrome.driver","C:\\chromedriver_win32\\chromedriver.exe");
    System.setProperty("webdriver.chrome.args", "--disable-logging");
    System.setProperty("webdriver.chrome.silentOutput", "true");
    driver = new ChromeDriver();
}

public void goToURL(String url) {
    driver.get(url);
}

    public void enterValue(String htmltype, String identifier, String value) throws InterruptedException {
    if (htmltype == "id") {
        WebElement element = driver.findElement(By.id(identifier));
        element.clear();
        element.sendKeys(value);
        element.submit();
    }
    if (htmltype =="name") {
        WebElement element = driver.findElement(By.name(identifier));
        element.clear();
        element.sendKeys(value);
        element.submit();
    }

    Thread.sleep(3000);
}

}

XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods">

  <test thread-count="5" name="Test" parallel="methods">
    <classes>
         <class name="webDrivertests.googleTestClass">
            <methods>
                <include name ="test1"/>
                <include name ="test2"/>
            </methods>
        </class>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

任何帮助将不胜感激!

【问题讨论】:

    标签: java selenium parallel-processing testng


    【解决方案1】:

    看起来您正在使用一个方法 executeGoogle(),它带有 @Test 注释,但它不是一个测试。删除注释

    您正在尝试从 googleTestClass 中实例化 googleTestClass(它应该有一个大写 G,所以是 GoogleTestClass)。这似乎是错误的

    您不需要 googleTestClass 的实例来调用 Methods 类中的方法。您可以直接调用它们,因为您的 googleTestClass 继承了它们

    当此类包含特定于浏览器测试的方法时,Also Methods 是一个非常通用的名称。你可以称它为 BrowserTestBaseFunctions 或类似的东西吗?

    我还建议您将 executeGoogle() 函数放入 Google 特定的类中,该类可以从 BrowserTestBaseFunctions 类继承...如果 executeGoogle 实际上是特定于 Google 的,否则您可以将其称为 loadUrl 并将 BrowserTestBaseFunctions 与参数使其更可重用

    【讨论】:

    • 感谢您的建议。此外,此解决方案有效,谢谢!
    • 如果我们删除@Test注解如何通过XML文件运行测试?
    • @Test 注释只能从 executeGoogle() 方法中删除,因为它不是测试。注释应该保留在实际的测试方法上
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多