当我的代码首先执行时,它会执行LoginTest.Java
类,然后它转到Main.Java
,但在这些中我得到空指针异常,它不读取方法中的驱动程序。
任何人都可以帮我解决,我是初学者。
提前致谢.. !!
LoginTest.Java类:
public class LoginTest {
private static final String ADDRESS_TO_TEST = "http://www.gmail.com";
private WebDriver driver;
Main Login;
HomePage homePage;
@BeforeTest
public void setUp() {
FirefoxDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(ADDRESS_TO_TEST);
}
@Test(priority = 0)
public void testLoginPageAppear() {
// Create Login Page object
Login = new Main(driver);
System.out.println("Login-->" + Login);
Login.loginToGmail("abc@gmail.com", "abc");
homePage = new HomePage(driver);
// Verify home page
Assert.assertTrue(homePage.getHOmePageEmailAddress().toLowerCase()
.contains("Google Account: abc@gmail.com"));
}
}
Main.Java类:
package GmailLogin;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class Main {
/*
* Reference
* URL:http://www.guru99.com/page-object-model-pom-page-factory-in-
* selenium-ultimate-guide.html
*/
WebDriver driver;
By email = By.id("Email");
By nextButton = By.id("next");
By password = By.id("Passwd");
By signInButton = By.id("signIn");
public Main(WebDriver driver) {
System.out.println("Driver--->" + driver);
this.driver = driver;
}
public void setEmailAddress(String strEmailAddress) {
driver.findElement(email).sendKeys(strEmailAddress);
}
public void clickNextButton() {
driver.findElement(nextButton).click();
}
public void setPassword(String strPassword) {
driver.findElement(password).sendKeys(strPassword);
}
public void clickSignInButton() {
driver.findElement(signInButton).click();
}
public void loginToGmail(String strEmailAddress, String strPassword) {
System.out.println("Email-->" + strEmailAddress);
System.out.println("Password-->" + strPassword);
this.setEmailAddress(strEmailAddress);
this.clickNextButton();
this.setPassword(strPassword);
this.clickSignInButton();
}
}
空指针异常:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
Driver--->null
Login-->GmailLogin.Main@5940ce27
Email-->abc@gmail.com
Password-->abc
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 21.016 sec <<< FAILURE! - in TestSuite
testLoginPageAppear(GmailLogin.LoginTest) Time elapsed: 0.047 sec <<< FAILURE!
java.lang.NullPointerException
at GmailLogin.LoginTest.testLoginPageAppear(LoginTest.java:47)
答案 0 :(得分:2)
此行导致您的驱动程序对象仅在方法范围内:
FirefoxDriver driver = new FirefoxDriver();
将其更改为
driver = new FirefoxDriver();
改为填写您的类属性。