从NameLocator文件中的DriverSetup.java文件调用/设置webdriver

从NameLocator文件中的DriverSetup.java文件调用/设置webdriver,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我无法在此正确设置驱动程序。我的输出返回NULL。 我正在尝试从driverSetup类设置驱动程序并获取占位符值 public class NameLocator { String fName; static WebDriver driver; public WebDriver setupDriver() { /*Invoke the getWebDriver method from the DriverSetup File*

我无法在此正确设置驱动程序。我的输出返回NULL。 我正在尝试从driverSetup类设置驱动程序并获取占位符值

public class NameLocator 
{
    String fName;  
    static WebDriver driver;  
    
    public WebDriver setupDriver()
    {
        /*Invoke the getWebDriver method from the DriverSetup File*/
        DriverSetup ds = new DriverSetup();
       
        driver =  ds.getWebDriver();
        return driver;
    
    public String getNameLocator()
    {
       /*Identify the Firstname
         Get the placeholder value
         Store the placeholder value in the static variable fName.*/
     
        if (fName!=null)
        
        fName = driver.findElement(By.name("fname")).getAttribute("placeholder");
        return fName;
    }
    
    public static void main(String[] args) throws NullPointerException
    {
        NameLocator namLocator=new NameLocator();
        
        String name=namLocator.getNameLocator();
        System.out.println("The name is "+name);
    }

}
java类,其中提供了所有驱动程序详细信息。我需要在namelocator类中使用该类,并找到firstname的占位符值。请帮助我使代码工作

    public class DriverSetup
    { 
        private static WebDriver driver;
        
        @BeforeClass
        public static WebDriver getWebDriver()
        {
            System.setProperty("webdriver.gecko.driver", "/usr/bin/geckodriver");
            FirefoxBinary firefoxBinary = new FirefoxBinary();
            firefoxBinary.addCommandLineOptions("--headless");
            FirefoxProfile profile=new FirefoxProfile();
            //profile.setPreference("marionette.logging", "TRACE");
            FirefoxOptions firefoxOptions = new FirefoxOptions();
            firefoxOptions.setBinary(firefoxBinary);
            firefoxOptions.setProfile(profile);
            driver=new FirefoxDriver(firefoxOptions);
            String baseUrl = "http://webapps.tekstac.com/Shopify/";
            driver.get(baseUrl);
        return driver;
    }
}

程序从main函数开始,调用使用webdriver对象的方法getNameLocator。从getWebDriver获取webdriver对象的setupDriver方法从未执行过,因为您没有在任何地方调用它

而不是这个-->

 public WebDriver setupDriver()
{
    /*Invoke the getWebDriver method from the DriverSetup File*/
    DriverSetup ds = new DriverSetup();
   
    driver =  ds.getWebDriver();
    return driver;
}
public NameLocator ()
{
    /*Invoke the getWebDriver method from the DriverSetup File*/
    DriverSetup ds = new DriverSetup();
   
    driver =  ds.getWebDriver();
    return driver;
}
使用构造函数-->

 public WebDriver setupDriver()
{
    /*Invoke the getWebDriver method from the DriverSetup File*/
    DriverSetup ds = new DriverSetup();
   
    driver =  ds.getWebDriver();
    return driver;
}
public NameLocator ()
{
    /*Invoke the getWebDriver method from the DriverSetup File*/
    DriverSetup ds = new DriverSetup();
   
    driver =  ds.getWebDriver();
    return driver;
}

解释我的构造函数代码是如何工作的-->一旦您在main方法中为NameLocator类创建对象,就会调用NameLocator的constructor。构造函数将从getWebDriver方法中获取初始化的webdriver对象,但我的要求是使用给定的方法来设置webdriver。即使我在主方法中添加了调用setwebdriver方法。它返回NULLpublic静态void main(String[]args)抛出NullPointerException{NameLocator namLocator=new NameLocator();namLocator.setupDriver();String name=namLocator.getNameLocator();System.out.println(“名称为“+name”);}}fName的初始值是多少?我哪儿都没看到。如果条件是(fName!=null),则使用--fName=driver.findElement(By.name(“fName”)).getAttribute(“占位符”);获取fName;。由于尚未初始化fName,显然它将具有NULL值,并且永远不会执行if条件。所以你得到了空值。将您的if条件更改为if(fName==NULL),它现在应该可以工作了。如果它可以工作,请单击向下投票箭头下问题旁边的勾号接受我的答案