简体   繁体   English

尝试从 testng.xml 运行我的测试时出现 NullPointerException

[英]NullPointerException while trying to run my tests from testng.xml

So, the issue is next - I'm trying to refactor my base test to do multiple browser parallel testing, but after running my code from the testng.xml file I've got the NullPointerExeption.所以,接下来的问题是——我正在尝试重构我的基础测试以进行多个浏览器并行测试,但是在从 testng.xml 文件运行我的代码之后,我得到了 NullPointerExeption。 My BaseTest:我的基础测试:

public class BaseTest {

    String browser = System.getProperty("browser");
    WebDriver driver = null;

    @Parameters("browser")
    @BeforeMethod(alwaysRun = true)
    public void setUp() {
        System.out.println("Current browser is " + browser);

        if (browser.equalsIgnoreCase("chrome")) {
            WebDriverManager.chromedriver().setup();
            driver = new ChromeDriver();
        } else if (browser.equalsIgnoreCase("firefox")) {
            WebDriverManager.firefoxdriver().setup();
            driver = new FirefoxDriver();
        } else if (browser.equalsIgnoreCase("edge")) {
            WebDriverManager.edgedriver();
            driver = new EdgeDriver();
        }
        driver.manage().window().maximize();
        driver.get("https://demo.prestashop.com/");
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        BasePage.setDriverThreadLocal(driver);
    }

    @AfterMethod(alwaysRun = true)
    public void tearDown() {
        getDriver().quit();
    }

}

My testng.xml configuration:我的 testng.xml 配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="2" name="Suite" parallel="methods">
    <test thread-count="2" name="Test" parallel="methods">
        <parameter name="browser" value="chrome"/>
        <classes>
            <class name="SubscribeWithInvalidEmailTest"/>
            <class name="CheckLanguagesTest"/>
        </classes>
    </test>
</suite>

I would appreciate it if you could provide me any help.如果您能为我提供任何帮助,我将不胜感激。

There are two things-有两件事——

  1. You are trying to get a browser property using System.getProperty("browser") but you haven't set it anywhere before, so, browser variable will always be null .您正在尝试使用System.getProperty("browser")获取浏览器属性,但您之前没有设置它,因此浏览器变量将始终为null Please set the property elsewhere first.请先在其他地方设置属性。
  2. You are passing a parameter browser but your setUp() function is not accepting any arguments, and thus your browser variable remains null and gives you the NullPointerException error.您正在传递参数浏览器,但您的setUp() function 不接受任何 arguments,因此您的浏览器变量仍然是 null 并给您 NullPointerException 错误。 You should use it like below你应该像下面这样使用它
    @Parameters("browser")
    @BeforeMethod(alwaysRun = true)
    public void setUp(String browser) {
        System.out.println("Current browser is " + browser); 

and now your browser variable will have the value which is passed by the testng.xml to your code and should not give the NullPointerException anymore.现在您的浏览器变量将具有由 testng.xml 传递给您的代码的值,并且不应再提供 NullPointerException。

If you still facing any issue then I recommend you to please share the stack trace which can be helpful in further debugging.如果您仍然遇到任何问题,那么我建议您分享堆栈跟踪,这有助于进一步调试。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM