首页 文章

Selenium pageFactory驱动程序的NullPointerException

提问于
浏览
0

Selenium pageFactory NullPointerException . 任何帮助将不胜感激 . 它适用于 setUp() 的登录,但在该驱动程序出现后为null .

public class TestBase {

    public WebDriver driver;
    String url = PropertiesFile.readPropertiesFile("autUrl");

    public WebDriver getDriver() {
        System.setProperty("webdriver.chrome.driver", 
        System.getProperty("user.dir") + "/driver/chromedriver.exe");

        driver = new ChromeDriver();

        return driver;
    }

    public void getUrl(String url) {
        driver.get(url);
        driver.manage().window().maximize();
    }

    public void init() {
        getDriver();
        getUrl(url);
    }
}



public class LogInPage extends TestBase {
    WebDriver driver;
    @FindBy(id = "loginEmail")public WebElement userName_field;
    @FindBy(name = "password")public WebElement password_field;
    @FindBy(xpath = "//*[@id='main-content']/aside/div/form/input[2]")public WebElement SignMeIn_btn;

    public LogInPage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    // Login
    public void logIn(String userName, String password) {
        userName_field.sendKeys(userName);
        password_field.sendKeys(password);
        SignMeIn_btn.click();
    }

}


   public class LogIn extends TestBase {

    LogInPage logInPage;

    private String user = PropertiesFile.readPropertiesFile("user");
    private String password = PropertiesFile.readPropertiesFile("password");



    public LogIn(WebDriver driver) {
        this.driver = driver;
    }


    public void setUp(){
        init();
    }


    public void logIn(){
        logInPage = new LogInPage(driver);
        logInPage.logIn(user, password);
    }

}

public class PortalsPage extends TestBase {

    WebDriver driver;

    @FindBy(id = "loginEmail") public WebElement userName_field;
    @FindBy(xpath=".//*[@id='tabDetail']") public WebElement tenantPage_li;
    @FindBy(id="tabDetail") public WebElement ownerPage_li;
    @FindBy(xpath="//a[contains(@href,'tenant.action')]") public WebElement tenantPortal_link;
    @FindBy(xpath="//a[contains(@href,'owner.action')]") public WebElement ownerPortal_link;

    public PortalsPage(WebDriver driver){
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }


    public void goToPortals(){      
        userName_field.sendKeys("a");
        tenantPage_li.click();
    }

}

public class Portals extends TestBase {
    PortalsPage portals;
    WebDriver driver;

    @BeforeClass
    public void setUp(){
        LogIn login = new LogIn(driver);
        login.setUp();
        login.logIn();

    }

    @Test
    public void goToPortal(){
        portals = new PortalsPage(driver);
        portals.goToPortals();
    }
}

我得到以下例外:

FAILED:org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)中的orT.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java)中的goToPortal java.lang.NullPointerException :38)在com.sun.proxy . $ Proxy6.sendKeys(未知来源)com.demo.pages.PortalsPage.goToPortals(PortalsPage.java:30)com.demo.control.Portals.goToPortal(Portals.java: 28)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)来自org.testng.internal.InvokeTethod(Invoker.java:710)org.test.invoke.invokeTethod(Invoker.java:714)的org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)org.test.Invoker.invokeTestMethod(Invoker.java) :901)在org.testng.internal.Tes的org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)位于org.testng.TestRunner.run上org.testng.TestRunner.privateRun(TestRunner.java:767)的org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)的tMethodWorker.invokeTestMethods(TestMethodWorker.java:127) (testRunner.java:617)org.testng.SuiteRunner.runTest(SuiteRunner.java:334)org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)org.testng.SuiteRunner.privateRun(SuiteRunner.java: 291)org.testng.SuiteRunner.run(SuiteRunner.java:240)org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)atg.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)at org . testg.TestNG.runSuitesSequentially(TestNG.java:1198)org.testng.TestNG.runSuitesLocally(TestNG.java:1123)atg.testng.TestNG.run(TestNG.java:1031)atg.testng.remote.AbstractRemoteTestNG .run(AbstractRemoteTestNG.java:132)org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

1 回答

  • 2

    您当前的方法似乎有点复杂 . 但是,如果不深入研究可以用来修复整体设计的其他方法的细节,这里有一个清理版本的代码,应该可行 .

    这里的想法是,您只需要页面类(以 *Page 结尾,代表特定页面,并公开可以在该特定页面上执行的一些业务功能)和测试类,测试类本身从您的 TestBase 扩展

    这是课程

    LoginPage.java

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.PageFactory;
    
    public class LogInPage {
        @FindBy(id = "loginEmail")
        private WebElement userNameTextField;
        @FindBy(name = "password")
        private WebElement passwordTextField;
        @FindBy(xpath = "//*[@id='main-content']/aside/div/form/input[2]")
        private WebElement signInButton;
    
        public LogInPage(WebDriver driver) {
            PageFactory.initElements(driver, this);
        }
    
        public void logIn(String userName, String password) {
            userNameTextField.sendKeys(userName);
            passwordTextField.sendKeys(password);
            signInButton.click();
        }
    
    }
    

    PortalsPage.java

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.PageFactory;
    
    public class PortalsPage {
        @FindBy(id = "loginEmail")
        private WebElement usernameTextField;
        @FindBy(xpath = ".//*[@id='tabDetail']")
        private WebElement tenantPage;
        @FindBy(id = "tabDetail")
        private WebElement ownerPage;
        @FindBy(xpath = "//a[contains(@href,'tenant.action')]")
        private WebElement tenantPortalLink;
        @FindBy(xpath = "//a[contains(@href,'owner.action')]")
        private WebElement ownerPortalLink;
    
    
        public PortalsPage(WebDriver driver) {
            PageFactory.initElements(driver, this);
        }
    
        public void goToPortals() {
            usernameTextField.sendKeys("a");
            tenantPage.click();
        }
    }
    

    TestBase.java

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class TestBase {
        private WebDriver driver;
    
        protected WebDriver getDriver() {
            if (driver != null) {
                return driver;
            }
            System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/driver/chromedriver.exe");
            driver = new ChromeDriver();
            return driver;
        }
    
        public void getUrl(String url) {
            getDriver().get(url);
            getDriver().manage().window().maximize();
        }
    
    }
    

    PortalsTest.java

    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Test;
    
    public class PortalsTest extends TestBase {
        private PortalsPage portals;
        private String user = PropertiesFile.readPropertiesFile("user");
        private String password = PropertiesFile.readPropertiesFile("password");
        private String url = PropertiesFile.readPropertiesFile("autUrl");
    
    
        @BeforeClass
        public void setUp() {
            LogInPage login = new LogInPage(getDriver());
            getUrl(url);
            login.logIn(user, password);
        }
    
        @Test
        public void goToPortal() {
            portals = new PortalsPage(getDriver());
            portals.goToPortals();
        }
    }
    

相关问题