Я пытаюсь реализовать PageObjectModel в Selenium. Но я получаю исключение NullPointer в @FindBy. Я надеюсь, что кто-то может помочь мне определить мою ошибку. Я создал свой базовый класс, который устанавливает файл config.properties (он содержит URL-адрес, параметры драйвера), а также устанавливает драйвер. HomePage имеет репозиторий объектов, а также действия, и PageFactory инициализируется. На HomePageTest есть все тесты. Когда я запускаю код, веб-сайт успешно запускается. Первый тест выполняется успешно. Но при втором тесте у FindBy не получается.
Вот мой код для справки.
Ниже приведен мой базовый класс:
package com.seleniumEasy.qa.base;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import com.seleniumEasy.qa.commonUtility.*;
public class Base {
public static WebDriver driver;
public static Properties prop;
@BeforeClass
public void setBaseline()
{
try {
setProperties("\\Devp\\SeleniumEasy_Maven_POM_DataDrivenProject\\src\\main\\java\\com\\seleniumEasy\\qa\\config\\config.properties");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//setProperties("\\Devp\\SeleniumEasy_Maven_POM_DataDrivenProject\\src\\main\\java\\com\\seleniumEasy\\qa\\config\\config.properties");
setDriver();
}
/*@AfterClass
public void tearDown()
{
driver.quit();
}*/
public static void setProperties(String sFilePath) throws IOException
{
prop = new Properties();
FileInputStream fip = new FileInputStream(sFilePath);
prop.load(fip);
}
public static void setDriver()
{
switch(prop.getProperty("browser").toLowerCase())
{
case "chrome":
System.setProperty(prop.getProperty("chromekey"),prop.getProperty("chromedriverpath"));
driver = new ChromeDriver();
break;
case "firefox":
System.setProperty(prop.getProperty("firefoxkey"), prop.getProperty("firefoxdriverpath"));
driver = new FirefoxDriver();
break;
case "ie":
System.setProperty(prop.getProperty("iekey"),prop.getProperty("iedriverpath"));
driver = new InternetExplorerDriver();
break;
case "edge":
System.setProperty(prop.getProperty("edgekey"), prop.getProperty("edgedriverpath"));
driver = new EdgeDriver();
break;
default:
System.out.println("Driver does not exists: "+ prop.getProperty("browser"));
}
driver.manage().timeouts().implicitlyWait(commonUtil.Implicit_Wait, TimeUnit.SECONDS);
//driver.manage().timeouts().pageLoadTimeout(commonUtil.PageLoad_Wait,TimeUnit.SECONDS);
driver.get(prop.getProperty("url"));
//driver.manage().window().maximize();
if(driver.findElement(By.partialLinkText("No, thanks!")).isDisplayed())
driver.findElement(By.partialLinkText("No, thanks!")).click();
}
}
Ниже приведен мой файл страницы
package com.seleniumEasy.qa.pages;
import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.seleniumEasy.qa.base.Base;
public class HomePage extends Base {
@FindBy(xpath="//*[@id=\"navbar-brand-centered\"]/ul[1]/li[1]/a")
WebElement InputForm;
@FindBy(css="#navbar-brand-centered > ul:nth-child(1) > li.dropdown.open")
List<WebElement> InputFormLst;
@FindBy(css="ul.nav.navbar-nav>li.dropdown.open>ul.dropdown-menu>li:first-of-type")
WebElement simpleFormDemo;
@FindBy(css="ul.nav.navbar-nav>li.dropdown.open>ul.dropdown-menu>li:nth-child(2)")
WebElement checkBoxDemo;
@FindBy(css="ul.nav.navbar-nav>li.dropdown.open>ul.dropdown-menu>li:nth-child(3)")
WebElement radioBtnDemo;
@FindBy(css="ul.nav.navbar-nav>li.dropdown.open>ul.dropdown-menu>li:nth-child(4)")
WebElement dropDownLstDemo;
@FindBy(css="ul.nav.navbar-nav>li.dropdown.open>ul.dropdown-menu>li:nth-child(5)")
WebElement inputFrmSubmit;
@FindBy(css="ul.nav.navbar-nav>li.dropdown.open>ul.dropdown-menu>li:nth-child(6)")
WebElement ajaxForm;
@FindBy(css="ul.nav.navbar-nav>li.dropdown.open>ul.dropdown-menu>li:nth-child(6)")
WebElement jQuerySelect;
List<WebElement> inputFormList = new ArrayList();
public HomePage()
{
PageFactory.initElements(driver, this);
}
public String validateTitle()
{
return driver.getTitle();
}
public SimpleForm validateSimpleFormDemo()
{
InputForm.click();
//driver.findElement(By.cssSelector("div.navbar-collapse.collapse.in>ul.nav.navbar-nav>li.dropdown.open>a")).click();
//driver.findElement(By.xpath("//*[@id=\"navbar-brand-centered\"]/ul[1]/li[1]/a")).click();
simpleFormDemo.click();
return new SimpleForm();
}
}
Ниже приведена моя тестовая страница
package com.seleniumEasy.qa.Test;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
import com.seleniumEasy.qa.base.Base;
import com.seleniumEasy.qa.pages.HomePage;
import com.seleniumEasy.qa.pages.SimpleForm;
public class HomePageTest extends Base {
HomePage homePage;
SoftAssert softAssert;
public HomePageTest()
{
homePage = new HomePage();
softAssert = new SoftAssert();
}
@Test(priority=1)
public void validateLoginPage()
{
String sTitle = homePage.validateTitle();
softAssert.assertEquals(sTitle, "Selenium Easy Demo");
}
@Test(priority=1)
public void validateSimpleForm()
{
SimpleForm simpleForm = homePage.validateSimpleFormDemo();
}
}
А вот трассировка стека
Starting ChromeDriver 2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e) on port 32000
Only local connections are allowed.
[1599758981.703][WARNING]: Timed out connecting to Chrome, retrying...
Sep 10, 2020 10:59:43 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\Default suite\Default test.xml
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\Default suite exists: true
FAILED: validateSimpleForm
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.$Proxy8.click(Unknown Source)
at com.seleniumEasy.qa.pages.HomePage.validateSimpleFormDemo(HomePage.java:66)
at com.seleniumEasy.qa.Test.HomePageTest.validateSimpleForm(HomePageTest.java:39)
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:100)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1129)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:746)
at org.testng.TestRunner.run(TestRunner.java:600)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1264)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1189)
at org.testng.TestNG.runSuites(TestNG.java:1104)
at org.testng.TestNG.run(TestNG.java:1076)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@42d3bd8b: 8 ms
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\testng-failed.xml
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output exists: true
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\Default suite\testng-failed.xml
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\Default suite exists: true
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 9 ms
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite\toc.html
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite exists: true
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite\Default test.properties
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite exists: true
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite\index.html
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite exists: true
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite\main.html
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite exists: true
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite\groups.html
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite exists: true
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite\classes.html
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite exists: true
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite\reporter-output.html
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite exists: true
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite\methods-not-run.html
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite exists: true
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite\testng.xml.html
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite exists: true
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\index.html
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old exists: true
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@6cd8737: 41 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@6aaa5eb0: 46 ms
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\junitreports\TEST-com.seleniumEasy.qa.Test.HomePageTest.xml
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\junitreports exists: true
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@3b81a1bc: 6 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@6b2fad11: 12 ms
Вот вид затмения