TestNG抛出" java.lang.NullPointerException#34;

时间:2014-07-19 10:49:08

标签: java selenium-webdriver testng

我正在尝试使用testng.xml执行我的webdriver代码(使用@Test(groups = {“General”}))

在不使用testng.xml的情况下,代码完美无缺。 我右键单击“ChapterOne.java”并执行Run As> TestNG测试

但是当我配置testng.xml时,它会抛出异常。 控制台仅显示以下信息:

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

生成的“index.html”报告显示了使用@Test(groups = {“General”})注释的所有方法的以下消息:

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

这很令人困惑,因为java抛出了NullPointerException而不是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编程的新手,我还在学习。我努力找出可能存在的问题,但无法弄明白。

更新

我通过从函数中删除所有Selenium Code来调整我的代码。 我添加了打印声明。请参阅以下内容:

    @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的情况下完美运行。

2 个答案:

答案 0 :(得分:0)

以下可能无法找到任何内容并返回null

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

测试_driver.findElement(By.id("selecttype"))是否实际返回元素。

答案 1 :(得分:-1)

您应该添加split,然后重试。