简体   繁体   English

使用xml运行测试时TestNG中的NullPointer异常

[英]NullPointer exception in TestNG while run test using xml

Try to automate my test using TESTNG framework in eclipse.尝试在 Eclipse 中使用 TESTNG 框架自动化我的测试。 In Project I use one packet iEDGE and write all test methods in single Class named eLogin.在项目中,我使用一个数据包 iEDGE 并在名为 eLogin 的单个类中编写所有测试方法。 But when I try to execute the code it shows nullPointer exceptions.但是当我尝试执行代码时,它会显示 nullPointer 异常。 Following is my sample code and xml settings that I use to run my test case.以下是我用来运行测试用例的示例代码和 xml 设置。

Can any one help me to resolve my problem.任何人都可以帮助我解决我的问题。

Package com.iEDGE;
public class eLogIn {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();     

@Parameters ( { "platform", "browser", "ver" } )
@BeforeMethod (alwaysRun=true )
public void setUp(@Optional String platform  , @Optional String  browser   , @Optional String version     ) throws Exception {
baseUrl = "Gmail URL";              
DesiredCapabilities mCapability = new DesiredCapabilities();        

if (platform.equalsIgnoreCase("WINDOWS")){
    mCapability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
}

if (browser.equalsIgnoreCase("Firefox")) {
mCapability = DesiredCapabilities.firefox();
mCapability.setVersion("40");           
}

driver = new RemoteWebDriver(new URL(baseUrl), mCapability);            
driver.manage().timeouts().implicitlyWait(1000, TimeUnit.MILLISECONDS);           
driver.get(baseUrl);       
driver.findElement(By.cssSelector("input[name=username]")).clear();
driver.findElement(By.cssSelector("input[name=username]")).sendKeys("username");
driver.findElement(By.cssSelector("input[name=password]")).clear();
driver.findElement(By.cssSelector("input[name=password]")).sendKeys("password");
driver.findElement(By.id("button-1015-btnInnerEl")).click();
}
//OTEHR TEST METHODS ....
}     

TESTSuite.xml Settings

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite guice-stage="DEVELOPMENT" name="Default suite">
<test verbose="2" name="Default test">                  
<parameter name="platform" value="Windows"/>
    <parameter name="browser" value="Firefox"/>
    <parameter name="ver" value="40.0.3"/>                      
<classes>
  <class name="com.iEDGE.eLogIn"/>
</classes>
</test> <!-- Default test -->
</suite> <!-- Default suite -->

Since all your parameter are annotated with @Optional, you will have to check if they are null before calling their methods.由于您的所有参数都使用@Optional 进行了注释,因此您必须在调用它们的方法之前检查它们是否为空。 So, for example, you will have to do something like this:因此,例如,您必须执行以下操作:

if(platform != null){
  if (platform.equalsIgnoreCase("WINDOWS")){
    mCapability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
  }
}

Do this for all the instructions that involves these optional parameters.对涉及这些可选参数的所有指令执行此操作。

You are passing wrong URL into " RemoteWebDriver(new URL(baseUrl), mCapability); " You should pass selenium server url like below:您将错误的 URL 传递到“ RemoteWebDriver(new URL(baseUrl), mCapability); ”中,您应该传递如下所示的 selenium 服务器 url:

new RemoteWebDriver( new URL("http://localhost:4444/wd/hub"), 
                            mCapability);

For more details follow: seleniumHq更多详情请关注: seleniumHq

Let me know if you have any concern如果您有任何疑虑,请告诉我

Thanks Sadik谢谢萨迪克

you are intializing wrong Base url.您正在初始化错误的基本网址。

is supposed to be应该是

baseUrl = "http://127.0.0.1:4888/wd/hub";

you have to mention which transfer protocol is used in your local host.您必须提及在您的本地主机中使用哪种传输协议。

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

相关问题 使用testng.xml使用maven运行单个TestNG测试 - Run single TestNG test with maven by using testng.xml Webdriver TestNG NullPointer异常 - Webdriver TestNG NullPointer Exception MRUnit测试在使用MULTIPLEOUTPUTS写入HDFS时给出NULLPOINTER异常 - MRUnit test giving NULLPOINTER exception while writing to HDFS using MULTIPLEOUTPUTS 运行TestNG测试时出现异常 - Exception while running a TestNG test 如何使用 testng.xml 运行测试文件 - How to run the test files by using the testng.xml Selenium POM和TestNG NullPointer异常 - Selenium POM and TestNG NullPointer exception 硒测试只能通过Maven启动。 开始使用TestNG xml文件时,由于NullPointer失败(因为系统变量为&#39;null&#39;) - Selenium test can be started via maven only. Fails with NullPointer when starting using TestNG xml file (because system variable is 'null') 使用IntelliJ运行单独的testng测试并读取testng.xml参数 - Run individual testng test and read testng.xml parameters using IntelliJ 在Spring中使用多线程时出现NullPointer异常 - NullPointer exception while using Multithread in Spring 使用MyBatis连接到数据库时出现Nullpointer异常 - Nullpointer exception while connecting to DB using MyBatis
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM