[英]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.