【发布时间】:2022-01-22 03:33:42
【问题描述】:
我目前正在学习硒。我已经逐步完成了如何启动 selenium-chromedriver 的演练。但是我在这里遇到了这个错误,我不知道如何解决它。
以下是演练给我的源代码。
package driverUtilities;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class DriverUtilities {
private static DriverUtilities instanceOfDriverUtilities;
private WebDriver driver;
public static DriverUtilities getInstanceOfDriverUtilities() {
if (instanceOfDriverUtilities == null) {
instanceOfDriverUtilities = new DriverUtilities();
}
return instanceOfDriverUtilities;
}
public WebDriver getDriver() {
if (driver == null) {
CreateDriver();
}
return driver;
}
private String GetDriverName() {
Properties config = new Properties();
String driverName = "";
try {
config.load(new FileInputStream("config.properties"));
} catch (FileNotFoundException e) {
System.out.println("Config file is not present");
e.printStackTrace();
} catch (IOException e) {
System.out.println("Error when loading config file");
e.printStackTrace();
}
for (String key : config.stringPropertyNames()) {
if (key.equals("browser")) {
driverName = config.getProperty(key);
}
}
return driverName;
}
private void CreateDriver() {
String driverName = GetDriverName();
switch (driverName) {
case "Google Chrome":
System.setProperty("webdriver.chrome.driver", "chromedriver");
this.driver = new ChromeDriver();
break;
case "Firefox":
System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
this.driver = new FirefoxDriver();
break;
default:
break;
}
}
}
这是驱动程序类
package test;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import driverUtilities.DriverUtilities;
public class Mod08_Slide_16_Navigation_Commands {
@Test
public void navigationCommands() {
DriverUtilities myDriverUtilities = new DriverUtilities();
WebDriver driver = myDriverUtilities.getDriver();
System.out.println("Start the Test Case");
// Load the website - http://www.bbc.co.uk
driver.get("http://www.bbc.co.uk");
System.out.println("\nLoad the website - http://www.bbc.co.uk");
// Refresh the page
driver.navigate().refresh();
System.out.println("\nRefresh the page");
// Load the website - http://www.google.co.uk
driver.get("http://www.google.co.uk");
System.out.println("\nLoad the website - http://www.google.co.uk");
// Go back to the website - http://www.bbc.co.uk
driver.navigate().back();
System.out.println("\nGo back to the website - http://www.bbc.co.uk");
// Go forward to the website - http://www.google.co.uk
driver.navigate().forward();
System.out.println("\nGo forward to the website - http://www.google.co.uk");
// Close the browser window
driver.close();
System.out.println("\nClose the browser window");
System.out.println("\nEnd of Test Case \"Navigation Commands\"");
}
}
这是我正在尝试使用的 java 测试类
# Change the browser to use for testing purposes, accepted values are Google Chrome and Firefox
browser=Google Chrome
我还有 pom.xml 文件和一个 config.properties 文件,如果需要,我会分享它。
注意:我的假设是 setproperty 函数没有找到 chromedriver 的正确路径。但是,我是 Arch linux 用户,因此我不确定 setproperty 函数中的值是设置为“chromedriver”还是“/usr/bin/chromedriver”
注意 1:我已经导航到 Eclipse IDE 的 .metadata 并在阅读了一些 stackoverflow 帖子后将其删除。但是,这不起作用*
注意 2:我对 Selenium 完全陌生,但是我对 Java 有一些经验,并且了解当我声明一个变量但没有创建对象并将该变量分配给它时会发生 nullpointerexception。
编辑 1:我已包含指定的 config.properties
【问题讨论】:
-
你能把你的
config.properties文件也发一下吗?有些东西告诉我这是你的问题。 -
您还应该将该属性更改为“chrome”而不是“Google Chrome”。
-
我回答你的问题了吗?
标签: java selenium selenium-chromedriver