0

Now i modified the code but still i am getting Null Pointer Exception

Below is my modified code enter code here

package lib;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeMethod;
//@SuppressWarnings("unused")
public class Login {
WebDriver driver;
@BeforeMethod
void Initalisation()
{
System.setProperty("webdriver.ie.driver", "C:\\Eclipse\\IEDriverServer.exe");
    DesiredCapabilities capability=new DesiredCapabilities();
    capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
    InternetExplorerDriver driver=new InternetExplorerDriver(capability);
    driver.get("http://segotn11540.rds.volvo.com/vss_connect_testr1/Login/Login.aspx");

}

public Login(String UserName,String BrandName)
{

    driver.findElement(By.xpath("//input[@name='UserNameInputText']")).sendKeys(UserName);
    driver.findElement(By.xpath("//input[@name='Brand']")).sendKeys(BrandName);
    driver.findElement(By.xpath("//input[@name='CmdLogin']")).click();
    String Title=driver.getTitle();

    if(!Title.contains("VSS 4.0"))
            {
        System.out.println(UserName+""+"does not exists");
        driver.quit();
            }
    CheckForCancel();
}
private void CheckForCancel() {
    if(!driver.findElements(By.id("Cancel")).isEmpty())
    {
    driver.findElement(By.id("Cancel")).click();
    }

}

}

Now I will create the main Java file

Blockquote This will initalise the login with the parameters supplied

Import Login library
public class MessageBoard {
public static void main(String[] args)
{
    Login login=new Login("TYP40FI","Volvo");

}


}

What is wrong in above code

Manish B
  • 113
  • 1
  • 2
  • 14

3 Answers3

0

Try to initialize the driver variable as WebDriver driver = new WebDriver();

Jai Shah
  • 56
  • 1
  • 8
0
public Login(String UserName,String BrandName)

{

//Add this line in your code as you are trying in IE
driver = new InternetExplorerDriver();


driver.findElement(By.xpath("//input[@name='UserNameInputText']")).sendKeys(UserName);
driver.findElement(By.xpath("//input[@name='Brand']")).sendKeys(BrandName);
driver.findElement(By.xpath("//input[@name='CmdLogin']")).click();
String Title=driver.getTitle();

if(!Title.contains("VSS 4.0"))
        {
    System.out.println(UserName+""+"does not exists");
    driver.quit();
        }
CheckForCancel();

}

CARE
  • 608
  • 3
  • 11
0
  1. Debug and check: Is Initalisation() being called in the beginning?
  2. Usually @BeforeMethod is called before test starts, so where is your @Test function. (syntax could be wrong)
  3. If you don't really care about @Test property, that means your Main function needs to call Initalisation() before calling Login(...), otherwise the driver is not set yet (aka Null)
kurakura88
  • 2,011
  • 2
  • 8
  • 17