@BeforeClass我得到null null错误我相信这与我的before类有关。您需要可选的吗? 失败的配置:@BeforeClass setUp(null,null)我试图添加不同的Maven依赖关系,也许是因为硒版本仍然无法正常工作。
public class Practice {
WebDriver driver;
//Check this ont out
@BeforeClass(alwaysRun = true)
@Parameters({ "browser", "url" })
public void setUp(@Optional("browser") String browser, @Optional("url") String url) {
BaseTest base = new BaseTest(browser, url);
driver = base.getDriver();
}
@Test
public void Check() {
try {
System.out.println("Passed Test case...");
Assert.assertTrue(driver.getTitle().contentEquals("Google"));
} catch (Exception ee) {
System.out.println("NOOOOO ");
ee.printStackTrace();
Assert.assertEquals("noooo " + ee, "iT SHOULD NOT fAIL");
}
}
public class Selen {
private WebDriver driver;
private String browser;
private String url;
public Selen(String browser, String url) {
this.browser = browser;
this.url = url;
if (browser.equalsIgnoreCase("firefox")) {
System.setProperty("webdriver.firefox.marionette",
"C:\\Users\\geckodriver.exe");
final FirefoxProfile firefox = new FirefoxProfile();
driver = new FirefoxDriver();
driver.get(url);
}
else if (browser.equalsIgnoreCase("chrome")) {
// set path to chromedriver.exe
System.setProperty("webdriver.chrome.driver", "C:\\Drivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--enable-automation", "test-type=browser", "--disable-plugins", "--disable-infobars",
"--disable-notifications", "start-maximized");
driver = new ChromeDriver(options);
driver.get(url);
}
else if (browser.equalsIgnoreCase("Edge")) {
// set path to Edge.exe
System.setProperty("webdriver.edge.driver", ".\\MicrosoftWebDriver.exe");
driver = new EdgeDriver();
driver.get(url);
} else {
}
}
public String getBrowser() {
return this.browser;
}
public String getBaseUrl() {
return this.url;
}
public WebDriver getDriver() {
return this.driver;
}
@AfterClass
public void tearDown(WebDriver driver) {
quitDriver(driver);
}
protected static void quitDriver(WebDriver driver) {
try {
if (driver != null) {
driver.quit();
}
} catch (Exception ee) {
System.out.println("Failed: " + ee);
;
}
}
**Failed Configuration**
[RemoteTestNG] detected TestNG version 6.14.3
Check() test case...
There was a problem:
java.lang.NullPointerException
at com.seleniumae.exercise.Practice.Check(Practice.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
TeNg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test thread-count="5" name="Test">
<parameter name="browser" value= "Chrome" />
<parameter name="url" value="https://www.google.com/"/>
<classes>
<class name="com.seleniumae.exercise.Practice"/>
<class name="com.seleniumae.exercise.Practice1"/>
<class name="com.seleniumae.exercise.Practice2.java"/>
</classes>
</test> <!-- Test -->
<test thread-count="5" name="Test">
<parameter name="browser" value ="Firefox" />
<parameter name="url" value="https://www.google.com/"/>
<classes>
<class name="com.seleniumae.exercise.Practice"/>
<class name="com.seleniumae.exercise.Practice1"/>
<class name="com.seleniumae.exercise.Practice2"/>
</classes>
</test> <!-- Test -->
<test thread-count="5" name="Test">
<parameter name="browser" value="InternetExplore" />
<parameter name="url" value="https://www.google.com/"/>
<classes>
<class name="com.seleniumae.exercise.Practice"/>
<class name="com.seleniumae.exercise.Practice1"/>
<class name="com.seleniumae.exercise.Practice2.java"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
问题可能出在浏览器使用诸如chrome之类的一个参数作为其他参数时。
答案 0 :(得分:0)
定义@Optional注释的语法:
public void setUp(@Optional(“ browser_name”)字符串浏览器,@ Optional(“ site_name”)字符串url)
并且您正在以以下方式实现而不使用@Optional(“ value”)
public void setUp(@Optional String浏览器,@ OptionalString url)抛出MalformedURLException {
TestNG.xml-请编写如下参数
$_.Split("_")[0]
注意-TestNG使您可以灵活地将参数声明为可选。当我们将参数声明为可选参数并且如果在您的testng.xml文件中找不到定义的参数时,测试方法将收到在@Optional批注中指定的默认值。