繁体   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