运行JUnit时发生NullPointerException。无法读取字符串数据

时间:2019-03-04 09:12:20

标签: java junit

在运行JUnit时抛出NullPointerException。在“ driver.get(baseUrl)”行显示错误。不知何故它无法读取String baseUrl的数据。字符串baseUrl是在@Before定义的,我正在尝试读取数据@Test。请提出建议。

Below is my code :

public class JunitTest1 {
    WebDriver driver;
    String baseUrl;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.out.println("Execution starts here");
    }

    @Before
    public void setUp() throws Exception {
        System.setProperty("webdriver.chrome.driver", "E:\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver= new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
        baseUrl="http://spezicoe.wipro.com:81/opencart1/";
    }

    @Test
    public void test() {

        driver.get(baseUrl);
        WebElement registerBtn = driver.findElement(By.xpath("//*[@id=\"top-links\"]/ul/li[2]/a/span[1]"));
        registerBtn.click();
        WebElement loginlink = driver.findElement(By.xpath("//*[@id=\"top-links\"]/ul/li[2]/ul/li[2]/a"));
        loginlink.click();

        //Login steps
        WebElement emailId=driver.findElement(By.xpath("//input[@id=\"input-email\"]"));
        emailId.sendKeys("username");

        WebElement password=driver.findElement(By.xpath("//input[@id=\"input-password\"]"));
        password.sendKeys("Suren!123");

        WebElement loginBtn=driver.findElement(By.xpath("//input[@class=\"btn btn-primary\"]"));
        loginBtn.click();
    }

    @After
    public void tearDown() throws Exception {
        System.out.println("Execution completed");
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {

            System.out.println("Execution completed");

    }

}

0 个答案:

没有答案