英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论