英文:
NoSuchFieldException:driver
问题
我正在正常运行回归和冒烟测试,但在过程中,进程本身中断。然后我收到了上述错误。我更新了我的 Chrome 驱动,以及 Chrome 本身。但我仍然得到相同的错误,这是我第一次遇到这样的问题,我已经检查了代码,但找不到任何合理的错误。有什么建议吗?
以下是我基础代码的部分内容:
public class base {
public static WebDriver driver;
public Properties prop;
public WebDriver initializeDriver() throws IOException {
prop = new Properties();
FileInputStream file = new FileInputStream(System.getProperty("user.dir") + "/src/main/java/resources/data.properties");
prop.load(file);
String browserName = prop.getProperty("browser");
System.out.println(browserName);
if (browserName.equals("Chrome")) {
System.setProperty("webdriver.chrome.driver", "/Users/lisandrosilva/Documents/Drivers/chromedriver 6");
driver = new ChromeDriver();
}
if (browserName.equals("headless")) {
System.setProperty("webdriver.chrome.driver", "/Users/lisandrosilva/Documents/Drivers/chromedriver 5");
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--disable-gpu");
driver = new ChromeDriver(options);
}
else if (browserName.equals("Safari")) {
//driver = new SafariDriver();
}
else if (browserName.equals("FireFox")) {
System.setProperty("webdriver.gecko.driver", "/Users/lisandrosilva/Documents/Drivers/geckodriver 3");
driver = new FirefoxDriver();
}
}
}
英文:
I'm running my regression and smoke tests as usual, but in the middle of the process the process it self fails. And i receive the above error. I updated my chrome driver, as well as chrome itself. But i keep getting the same error, this is the first time i get such an issue, and i have reviewed the code but i can't find any reasonable mistake . Any suggestions?
Find part of my base code here...
public class base {
public static WebDriver driver;
public Properties prop;
public WebDriver initializeDriver() throws IOException
{
prop = new Properties();
//FileInputStream System.getProperty("user.dir")
FileInputStream file = new FileInputStream(System.getProperty("user.dir")+"/src/main/java/resources/data.properties");
prop.load(file);
String browserName = prop.getProperty("browser");
//Field field = Class.getField("driver");
//String browserName = System.getProperty("browser");
System.out.println(browserName);
if (browserName.equals("Chrome")){
//Execute
System.setProperty("webdriver.chrome.driver","/Users/lisandrosilva/Documents/Drivers/chromedriver 6");
//ChromeOptions options = new ChromeOptions();
driver = new ChromeDriver();
}
if(browserName.equals("headless")) {
System.setProperty("webdriver.chrome.driver","/Users/lisandrosilva/Documents/Drivers/chromedriver 5");
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--disable-gpu");
driver = new ChromeDriver(options);
}
else if (browserName.equals("Safari")) {
//Execute
//driver = new SafariDriver();
}
else if (browserName.equals("FireFox")) {
System.setProperty("webdriver.gecko.driver","/Users/lisandrosilva/Documents/Drivers/geckodriver 3");
driver = new FirefoxDriver();
//Execute
// driver = new FirefoxDriver();
}
答案1
得分: 0
NoSuchFieldException
NoSuchFieldException 表示类中没有指定名称的字段。
如果这个错误发生在您的测试过程中,可能是您的程序正在访问错误的文件。我建议将属性文件的名称从 data.properties
改为 myProp.properties
,然后执行您的测试。
英文:
NoSuchFieldException
NoSuchFieldException indicates that the class doesn't have a field of a specified name.
If this error is happening in the mid of your tests, probably your program is accessing the wrong file. I would suggest to rename the properties file as myProp.properties
instead of data.properties
and execute your tests.
答案2
得分: 0
我曾经面临相同的问题,现在已经解决了,就像Lisandro Fernando在他们的评论中提到的那样。
使用了getField
方法,而不是getDeclaredField
方法。
英文:
I had faced the same issue and it got resolved now as Lisandro Fernando mentioned in their comment.
Used getField
method instead of getDeclaredField
method.
专注分享java语言的经验与见解,让所有开发者获益!
评论