Ниже приведен код. Я получаю сообщение об ошибке java.lang.NullPointerException.
error:[RemoteTestNG] detected TestNG version 6.14.2 Starting ChromeDriver 2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb) on port 29667 Only local connections are allowed. Jul 01, 2018 8:21:49 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: OSS FAILED: Login java.lang.NullPointerException at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69) at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38) at com.sun.proxy.$Proxy9.sendKeys(Unknown Source) at com.Agec.Actions.LoginPageActions.getLogin(LoginPageActions.java:20) at com.Agec.Tests.Bat_Suite.Login(Bat_Suite.java:22) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124) at org.testng.internal.Invoker.invokeMethod(Invoker.java:580) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:648) at org.testng.TestRunner.run(TestRunner.java:505) at org.testng.SuiteRunner.runTest(SuiteRunner.java:455) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) at org.testng.SuiteRunner.run(SuiteRunner.java:364) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208) at org.testng.TestNG.runSuitesLocally(TestNG.java:1137) at org.testng.TestNG.runSuites(TestNG.java:1049) at org.testng.TestNG.run(TestNG.java:1017) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
=============================================== Тест по умолчанию
=============================================== Набор по умолчанию
package com.Mil.Pages;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class LoginPage
{
@FindBy(xpath = "//input[@type='text']")
public WebElement CellNo;
@FindBy(xpath = "//input[@type='password']")
public WebElement Password;
@FindBy(xpath = "//span[text()=Sign in")
public WebElement SignInBtn;
}
package com.Mil.Actions;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import com.Mil.Pages.LoginPage;
import com.Mil.Tests.TestCaseBase;
public class LoginPageActions
{
WebDriver driver;
LoginPage lp=PageFactory.initElements(TestCaseBase.driver, LoginPage.class);
public LoginPageActions(WebDriver driver)
{
this.driver=driver;
}
public void getLogin()
{
lp.CellNo.sendKeys("123");
lp.Password.sendKeys("569");
lp.SignInBtn.click();
}
}
package com.Mil.Tests;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
public class TestCaseBase
{
public static WebDriver driver=null;
@BeforeTest
public void LaunchBrowser() throws Exception
{
Properties Prop=new Properties();
File Fil=new File("C:\\Users\\eclipse-
workspace\\Mil\\TestConfig\\config.properties");
FileInputStream fileInput=new FileInputStream(Fil);
Prop.load(fileInput);
System.setProperty("webdriver.chrome.driver",Prop.getProperty("Chrome_Path"));
driver=new ChromeDriver();
driver.get(Prop.getProperty("Url"));
}
@AfterTest()
public void CloseBrowser()
{
driver.quit();
}
}
package com.Mil.Tests;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;
import com.Mil.Actions.LoginPageActions;
public class Bat_Suite extends TestCaseBase
{
LoginPageActions lpa=PageFactory.initElements(TestCaseBase.driver,
LoginPageActions.class);
@Test(priority=1)
public void Login ()
{
lpa.getLogin();
}
}




Ваш драйвер имеет значение NULL, вы пытаетесь вызвать его, но он равен нулю для инициализации элементов. Чтобы решить вашу проблему, вам нужно изменить @BeforeTest на @BeforeClass, даже в этом случае вам не нужны аннотации TestNG.
public class TestCaseBase {
public static WebDriver driver=null;
@BeforeClass
public void LaunchBrowser() throws Exception {
Properties Prop=new Properties();
File Fil=new File("C:\\Users\\eclipse- workspace\\Mil\\TestConfig\\config.properties");
FileInputStream fileInput=new FileInputStream(Fil);
Prop.load(fileInput);
System.setProperty("webdriver.chrome.driver",Prop.getProperty("Chrome_Path"));
driver=new ChromeDriver();
driver.get(Prop.getProperty("Url"));
}
@AfterClass()
public void CloseBrowser() {
driver.quit();
}
}
Рекомендуемое решение - создать функцию получения драйвера с синглтоном, которая проверяет, является ли драйвер нулевым или нет, если да, создайте новый драйвер и вернитесь.
И вы должны разобраться в типах аннотаций:
@BeforeSuite: The annotated method will be run before all tests in this suite have run.
@AfterSuite: The annotated method will be run after all tests in this suite have run.@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run. @AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.
@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to runshortly before the first test method that belongs to any of these groups is invoked.
@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.@BeforeClass: The annotated method will be run before the first test method in the current class is invoked. @AfterClass: The annotated method will be run after all the test methods in the currentclass have been run.
@BeforeMethod: The annotated method will be run before each test method.
@AfterMethod: The annotated method will be run after each test method. from TestNG documentation.
@BeforeTest работает перед тегом в вашем xml runner.
Хорошо, большое спасибо, я изменил BeforeTest на BeforeClass, также переместил pageFactory и class Object в метод и работал нормально. Большое спасибо ..
Приятного вам тестирования. Пожалуйста, примите мой ответ
См. Что такое трассировка стека и как ее использовать для отладки ошибок приложения? и Что такое исключение нулевого указателя и как его исправить?