简体   繁体   中英

NullPointer exception in TestNG while run test using xml

Try to automate my test using TESTNG framework in eclipse. In Project I use one packet iEDGE and write all test methods in single Class named eLogin. But when I try to execute the code it shows nullPointer exceptions. Following is my sample code and xml settings that I use to run my test case.

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. 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:

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

For more details follow: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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