簡體   English   中英

TestNG拋出“ java.lang.NullPointerException”

[英]TestNG throws “java.lang.NullPointerException”

我正在嘗試使用testng.xml(使用@Test(groups = {“ General”}))執行我的webdriver代碼。

該代碼無需使用testng.xml即可完美運行。 我右鍵單擊“ ChapterOne.java”並執行運行方式> TestNG測試

但是當我配置testng.xml時,它會拋出異常。 控制台僅顯示以下信息:

> [TestNG] Running:
>  G:\Webdriver_JUnit\BookAutomatedTester_TestNG\testng.xml
> ===============================================
> Suite1
> Total tests run: 3, Failures: 3, Skips: 0
> ===============================================

對於所有使用@Test(groups = {“ General”})注釋的方法,生成的“ index.html”報告顯示以下消息:

>  VerifyChapter1DropDown java.lang.NullPointerException
> at test.ChapterOne.VerifyChapter1DropDown(ChapterOne.java:44)
> ... Removed 24 stack frames

這很混亂,因為NullPointerException由Java而不是TestNG引發。

下面是我的硒代碼:

package test;
import java.util.Set;

import org.testng.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.*;

public class ChapterOne  {

    WebDriver _driver;
    String ActualText;
    String baseurl = "http://book.theautomatedtester.co.uk/chapter1";
    WebElement AjaxTextLoc;

    @BeforeClass
    public void SetUp() {
        _driver = new FirefoxDriver();
        _driver.get(baseurl);   }

    @Test (groups = { "General" })
    public void VerifyChapter1Text()    {
        WebElement WDText = _driver.findElement(By.xpath("/html/body/div[2]/p[1]"));
        String ActualText = "If you have arrived here then you have installed Selenium IDE and are ready to start recording your first test.";
        Assert.assertEquals(ActualText, WDText.getText() );

        WebElement WDText1 = _driver.findElement(By.id("divontheleft"));
        ActualText = "Assert that this text is on the page";
        Assert.assertEquals(ActualText, WDText1.getText());
        ((JavascriptExecutor) _driver).executeScript("window.scrollBy(0,200)", "");
        ((JavascriptExecutor) _driver).executeScript("alert('hello world');");
                }           
    @Test (groups = { "General" })
    public void VerifyChapter1RadioButton() {
        WebElement RadioBtn = _driver.findElement(By.id("radiobutton"));
        Assert.assertEquals(RadioBtn.isSelected(), false);
        RadioBtn.click();
        Assert.assertEquals(RadioBtn.isSelected(), true);
        }
    @Test (groups = { "General" })
    public void VerifyChapter1DropDown()    {
        WebElement DropDn = _driver.findElement(By.id("selecttype"));
        DropDn.click();     // extra step
        DropDn.sendKeys("Selenium Grid");       
        }   
    @Test(groups = {"Different"})
    public void VerifyChapter1CheckBox()    {
        WebElement CheckBx = _driver.findElement(By.name("selected(1234)"));
        Assert.assertEquals(CheckBx.isEnabled(), true);
        Assert.assertEquals(CheckBx.isSelected(), false);
        CheckBx.click();
        Assert.assertEquals(CheckBx.isSelected(), true);        
        }
    @Test(groups = {"Different"})
    public void VerifyChapter1Btns()    {
        WebElement Btn1Text = _driver.findElement(By.id("html5div"));
        Btn1Text.clear();
        WebElement Btn1 = _driver.findElement(By.id("secondajaxbutton"));
        Assert.assertEquals(Btn1.isEnabled(), true);    
        Btn1.click();
        ActualText = "I have been added with a timeout";
        Assert.assertEquals(ActualText, Btn1Text.getText());            
        }
    @Test(groups = {"Different"})
    public void VerifyChapter1SwitchWindows()   {
        String ParentHandle = _driver.getWindowHandle();
        _driver.findElement(By.id("multiplewindow")).click();
        Set<String> AllHandles = _driver.getWindowHandles();

        for(String ChildHandle : AllHandles)    {
            _driver.switchTo().window(ChildHandle);     
        }
        _driver.close();
        _driver.switchTo().window(ParentHandle);        

        }
    @Test(groups = {"Different"})
    public void VerifyChapter1AJAX() throws Exception   {
        WebElement AjaxBtnTxt = _driver.findElement(By.id("loadajax"));
        AjaxBtnTxt.click();
        ActualText = "The following text has been loaded from another page on this site. It has been loaded in an asynchronous fashion so that we can work through the AJAX section of this chapter";
        Thread.sleep(1000);     
        WebElement AjaxTextLoc = _driver.findElement(By.xpath("//div[@id='ajaxdiv']/p"));
        Assert.assertEquals(ActualText, AjaxTextLoc.getText());
        ((RemoteWebDriver) _driver).executeScript("window.scrollBy(0,-200)", "");

        }

    @AfterClass
    public void Quit()  {
        System.out.println("@AfterClass was executed");
        _driver.quit();
    }       
    }

下面是testng.xml:

<suite name="Suite1">
  <test name="My ChapterOne Tests">
    <groups>  
        <run>
        <include name="General" />
        </run>
    </groups>
        <classes>
        <class name="test.ChapterOne" />
        </classes>
    </test>
</suite>

PS:我是webdriver編程的新手,我仍在學習。 我盡力找出可能是什么問題,但不知道。

更新:

我通過從函數中刪除所有的硒代碼來調整代碼。 我添加了打印語句。 請看下面:

    @Test(groups={"General"})
    public void VerifyChapter1Text()    {
        System.out.println("In VerifyChapter1Text");
                }           

    @Test (groups = { "General" })
    public void VerifyChapter1RadioButton() {
        System.out.println("In VerifyChapter1RadioButton");
        }
    @Test (groups = { "General" })
    public void VerifyChapter1DropDown()    {
        System.out.println("In VerifyChapter1DropDown");
        }   

令我驚訝的是它奏效了。 這意味着我的testng.xml文件是正確的。

但這怎么可能呢? 我的原始webdriver代碼無需使用testng.xml即可完美運行。

以下內容可能找不到任何內容,並返回null

WebElement DropDn = _driver.findElement(By.id("selecttype"));

測試_driver.findElement(By.id("selecttype"))實際返回一個元素。

您應該添加@BeforeClass(alwasyRun = true) ,然后重試。

暫無
暫無

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

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