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

huangapple go评论75阅读模式
英文:

Call/Setup webdriver from DriverSetup.java file in the NameLocator file

问题

以下是您要翻译的内容:

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

public class NameLocator 
{
    String fName;  
    static WebDriver driver;  
    
    public WebDriver setupDriver()
    {
        /*从DriverSetup文件调用getWebDriver方法*/
        DriverSetup ds = new DriverSetup();
       
        driver =  ds.getWebDriver();
        return driver;
    }
    
    public String getNameLocator()
    {
       /*识别名字
         获取占位符的值
         将占位符的值存储在静态变量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);
    }

}

DriverSetup.java类中包含所有驱动程序详细信息,我需要在namelocator类中使用它并查找名字的占位符值。请帮助我让代码工作。

英文:

I am not able to setup the driver here correctly. My output returns NULL.
i am trying to setup driver from driverSetup class and get the placeholder value.

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);
	}

}

DriverSetup.java class where all the driver details are given i need to use this in namelocator class and find the placeholder value for firstname. Kindly help me get the code working

    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;
	}
}

答案1

得分: 1

您的程序从main函数开始,然后调用getNameLocator方法,该方法使用webdriver对象。您的setupDriver方法从getWebDriver获取webdriver对象,但由于您没有在任何地方调用它,所以从未执行过。

改为这样 -- >

public WebDriver setupDriver()
{
    /*从DriverSetup文件中调用getWebDriver方法*/
    DriverSetup ds = new DriverSetup();
   
    driver =  ds.getWebDriver();
    return driver;
}

使用构造函数 -- >

public NameLocator ()
{
    /*从DriverSetup文件中调用getWebDriver方法*/
    DriverSetup ds = new DriverSetup();
   
    driver =  ds.getWebDriver();
    return driver;
}

关于我的构造函数代码如何工作的解释 -- > 一旦您在main方法中为NameLocator类创建对象,NameLocator的构造函数就会被调用。构造函数将从getWebDriver方法中获得已初始化的webdriver对象。

英文:

Your program starts from main function and it calls the method getNameLocator which uses the webdriver object. Your setupDriver method which gets the webdriver object from getWebDriver has never got executed as you have not called it anywhere.

Instead of this -- >

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

Go with constructor -->

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

Explaination on how my constructor code works --> as soon you create object for NameLocator class in main method , Constructor of NameLocator gets invoked. The constructor will inturn gets the initialized webdriver object from getWebDriver method

huangapple
  • 本文由 发表于 2020年8月6日 17:20:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63280538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定