簡體   English   中英

並行測試返回java.lang.NullPointerException時調用另一個方法

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

使用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 -->

任何幫助,將不勝感激!

看起來您正在使用executeTest()方法,該方法帶有@Test注釋,但它不是測試。 刪除注釋

您正在嘗試從googleTestClass(應該有大寫G,因此是GoogleTestClass)中實例化googleTestClass。 這似乎是錯誤的

您不需要googleTestClass實例即可調用Methods類中的方法。 您可以直接調用它們,因為您的googleTestClass繼承了它們

當此類包含特定於瀏覽器測試的方法時,Methods也是一個通用名稱。 您可以將其命名為BrowserTestBaseFunctions還是類似的名稱?

我還建議您將executeGoogle()函數放入一個特定於Google的類,該類可以從BrowserTestBaseFunctions類繼承……這就是如果executeGoogle實際上是特定於Google的,否則您可以將其稱為loadUrl並放入帶有參數的BrowserTestBaseFunctions進行制作更可重用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM