提问人:Sakhi 提问时间:8/30/2023 最后编辑:Abhay ChaudharySakhi 更新时间:9/2/2023 访问量:35
NullPointerException:
NullPointerException :
问:
我想尝试在我的代码中实现页面对象模型。第一页将负责打开网址,并提供用户名和密码。 目前,我的代码在不找到元素的情况下打开 url 并加载网页,然后打开一个新的空白窗口并重复三到四次。 尝试定位元素时崩溃。谁能告诉我我哪里犯了错误。我已将以下详细信息存储在 Config 中。我一遍又一遍地检查代码,但找不到问题
public class Base {
public static WebDriver driver;
public static Properties prop;
public Base(){
try {
prop = new Properties();
FileInputStream ip = new FileInputStream("C:\\config\\config.properties");
prop.load(ip);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void initilization()
{
String browsername = prop.getProperty("browser");
if (browsername.equals("chrome")){
System.setProperty("webdriver.chrome.driver","C:\\Users\\chromedriver.exe");
driver= new ChromeDriver();
}
driver.manage().window().maximize();
driver.get(prop.getProperty("url"));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(70));
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(70));
}
}
登录页面 :
public class LoginPage extends Base {
@FindBy(name = "username")
WebElement username;
@FindBy(name = "password")
WebElement password;
@FindBy(xpath = "//input[@type='submit']")
WebElement loginbutton;
@FindBy(xpath = "//button[contains(text(),'Sign Up')]")
WebElement SignUpButton;
@FindBy(xpath= "//img[contains(@class='img-responsive')]")
WebElement crmlogo;
public void LoginPage()
{
PageFactory.initElements(driver,this);
}
public boolean logo()
{
return crmlogo.isDisplayed();
}
public String validatetitleofpage()
{
return driver.getTitle();
}
public HomePage login(String un,String psd)
{
username.sendKeys(un);
password.sendKeys(psd);
loginbutton.click();
return new HomePage();
}
}
这是我调用所有方法的 LoginTest:
public class LoginPageTest extends Base {
LoginPage LoginPage;
HomePage HomePage;
public LoginPageTest()
{ super();
}
@BeforeMethod
public void setup()
{
initilization();
LoginPage = new LoginPage();
}
@Test (priority = 1)
public void loginPageTitleTest()
{
String title = LoginPage.validatetitleofpage();
Assert.assertEquals(title,"title details");
}
@Test (priority = 2)
public void logoTest()
{
boolean logotrue=LoginPage.logo();
Assert.assertTrue(logotrue);
}
@Test(priority = 3)
public void logintest()
{
HomePage= LoginPage.login(prop.getProperty("unme"),prop.getProperty("pwd"));
}
@AfterMethod
public void teardown()
{
driver.quit();
}
}
下面是错误的
java.lang.NullPointerException
at com.crm.qa.pages.LoginPage.logo(LoginPage.java:31 undefined)
at com.crm.qa.Testcases.LoginPageTest.logoTe0st(LoginPageTest.java:37 undefined)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at `enter code
here`java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566 undefined)
at`org.testng.internal.MethodInvocationHelper.invokeMethod`(MethodInvocationHelper.java:133)
at `org.testng.internal.TestInvoker.invokeMethod`(TestInvoker.java:598 undefined)
at org.testng.internal.TestInvoker.invokeTestMethod
(TestInvoker.java:173)
at org.testng.internal.MethodRunner.runInSequence
(MethodRunner.java:46)
at
`org.testng.internal.TestInvoker$MethodInvocationAgent.invoke`(TestInvoker.java:824)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146 undefined)
配置文件
url = https://classic.crmpro.com/
unme = abcd
pwd = Abcd
browser = chrome**
答: 暂无答案
评论
crmlogo
isDisplayed()