简体   繁体   English

并行测试返回java.lang.NullPointerException时调用另一个方法

[英]Calling another method when Parallel Testing returns java.lang.NullPointerException

I am running into an issue when executing Parallel Tests using Java, Selenium, and TestNG. 使用Java,Selenium和TestNG执行并行测试时遇到问题。 I have a 2 test methods that search google for two different keywords. 我有2种测试方法,可以在google中搜索两个不同的关键字。 I would like a third method that is called by both test methods to avoid repeating similar code. 我希望第三个测试方法都调用该方法,以避免重复类似的代码。

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

When my code hits the object1.executeGoogle(); 当我的代码命中object1.executeGoogle(); and object2.executeGoogle(); 和object2.executeGoogle(); commands, it returns a java.lang.NullPointerException. 命令,则返回java.lang.NullPointerException。 I have an idea that the error is related to the object but I am not sure how to proceed. 我有一个错误与该对象有关的想法,但是我不确定如何继续。

Here are additional classes that are being used. 这是正在使用的其他类。

Method class: 方法类别:

// 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 File: 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 -->

Any help would be appreciated! 任何帮助,将不胜感激!

It looks like you are using a method, executeGoogle() which is annotated with @Test, but it is not a test. 看起来您正在使用executeTest()方法,该方法带有@Test注释,但它不是测试。 Remove the annotation 删除注释

You are trying to instantiate googleTestClass, from within googleTestClass (which should have a capital G, so GoogleTestClass). 您正在尝试从googleTestClass(应该有大写G,因此是GoogleTestClass)中实例化googleTestClass。 This seems wrong 这似乎是错误的

You don't need an instance of googleTestClass to call the methods in Methods class. 您不需要googleTestClass实例即可调用Methods类中的方法。 You can just call them directly since your googleTestClass inherits them 您可以直接调用它们,因为您的googleTestClass继承了它们

Also Methods is quite a generic name when this class contains methods specific to browser testing. 当此类包含特定于浏览器测试的方法时,Methods也是一个通用名称。 Could you call it BrowserTestBaseFunctions or something similar? 您可以将其命名为BrowserTestBaseFunctions还是类似的名称?

I would also suggest that your executeGoogle() function goes into a Google specific class, which could inherit from the BrowserTestBaseFunctions class... That's if executeGoogle is actually specific to Google, otherwise you could call it loadUrl and put in BrowserTestBaseFunctions with params to make it more reusable 我还建议您将executeGoogle()函数放入一个特定于Google的类,该类可以从BrowserTestBaseFunctions类继承……这就是如果executeGoogle实际上是特定于Google的,否则您可以将其称为loadUrl并放入带有参数的BrowserTestBaseFunctions进行制作更可重用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用Method.invoke时获取java.lang.NullPointerException - Getting java.lang.NullPointerException when calling Method.invoke 从另一个类调用方法时,DiscoveryClient上的java.lang.NullPointerException - java.lang.NullPointerException on DiscoveryClient when calling the method from another class 调用方法将字符串写入 JAVA 中的 Excel 表时,线程“主”java.lang.NullPointerException 中的异常错误 - Exception in thread “main” java.lang.NullPointerException error when calling a method to write a string into Excel sheet in JAVA 调用类方法时发生错误java.lang.NullPointerException - Error java.lang.NullPointerException while calling class method 返回null的方法导致java.lang.NullPointerException - Method that returns null is causing java.lang.NullPointerException Mockito:从模拟的类调用方法时出现java.lang.NullPointerException - Mockito : java.lang.NullPointerException when calling method from mocked Class java.lang.NullPointerException调用int数组时 - java.lang.NullPointerException When calling an int array 为什么在调用JasperFillManager时在java.lang.Class.isAssignableFrom(Native Method)中得到java.lang.NullPointerException? - Why do I get java.lang.NullPointerException at java.lang.Class.isAssignableFrom(Native Method) when calling JasperFillManager? 使用Mockito java.lang.nullpointerexception测试 - testing with Mockito java.lang.nullpointerexception Ejb返回java.lang.NullPointerException - Ejb returns java.lang.NullPointerException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM