我想把这段代码传递给我的其他类,这样我就不必继续粘贴它了。
package utility;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class BrowserType {
public static WebDriver driver;
@Parameters("browser")
@Test
public static void CallBrowser(String browser) {
if(browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
// If browser is IE, then do this
}else if (browser.equalsIgnoreCase("chrome")) {
// Here I am setting up the path for my IEDriver
{System.setProperty("webdriver.chrome.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/chromedriver.exe");}
driver = new ChromeDriver();
driver.get(Constant.URL);
}
}
}
package automationFramework;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import utility.Constant;
import appModule.SignIn_Action;
public class SignIn {
public WebDriver driver;
@BeforeMethod
@Parameters("browser")
public void SetUp(String browser) {
if(browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
// If browser is Chrome, then do this
}else if (browser.equalsIgnoreCase("chrome")) {
{ System.setProperty("webdriver.chrome.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/chromedriver.exe");}
driver = new ChromeDriver();
{ driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);}
{driver.manage().window().maximize();}
driver.get(Constant.URL);
}
}
@Test
public void signIn() {
SignIn_Action.Execute(driver, Constant.DevStudentUsername, Constant.DevStudentPassword);
}
@AfterMethod
public void Teardown() {
driver.quit();
}
}
以下是错误:
FAILED CONFIGURATION: @AfterMethod Teardown
java.lang.NullPointerException
at automationFramework.SignIn.Teardown(SignIn.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:786)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
FAILED: signIn
java.lang.NullPointerException
at appModule.SignIn_Action.Execute(SignIn_Action.java:27)
at automationFramework.SignIn.signIn(SignIn.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
Configuration Failures: 1, Skips: 0
===============================================
===============================================
Suite
Total tests run: 1, Failures: 1, Skips: 0
Configuration Failures: 1, Skips: 0
===============================================
这就是我试图调用代码的方式:
public WebDriver driver;
@BeforeMethod
@Parameters("browser")
public void SetUp(String browser) {
BrowserType.CallBrowser(browser);
}
您的问题是,当您调用TearDown时,您的驱动程序没有初始化。您需要确保在所有情况下都初始化了它。我将使用调试器检查您的代码,并进行检查,以确保当您单击setup and teardown方法时,驱动程序实际上是在setup时初始化的,并且在deordown时存在的。当事情运行时,你也有两个司机在场。一个是在代码的第一部分声明的静态驱动程序,然后在实际测试中找到另一个非静态驱动程序。我会解决这个问题,因为它可能会引起问题。
解决问题的最简单方法是确保在安装过程中始终初始化驱动程序,即使字符串与任一选项都不匹配。
public void SetUp(String browser) {
if(browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
// If browser is Chrome, then do this
}else {
System.setProperty("webdriver.chrome.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/chromedriver.exe");
driver = new ChromeDriver();
{ driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);}
{driver.manage().window().maximize();}
driver.get(Constant.URL);
}
}
有什么方法可以简化这段代码吗?我正好有一个白色的一块,想要得到它的位置 代码: 瓦片类: 件类:
我正在尝试获取角色id,但我不知道如何操作,因为它不起作用: 身份验证::用户- 对象(照亮\数据库\雄辩\收集)#843(1) {["项目":受保护]=
我有这段代码,但我不明白为什么会出现以下错误:
非法尝试将非集合映射为@onetomany、@manytomany或@collectionofelements:cloudcodes.schema.generator.model.useroudata.orgunitpathid 有人能帮我提前谢谢你吗。
我有一小段Java密码。我想用一种更有建设性的方式和更优雅的方式重写下面的代码。我怎样才能实现它? 非常感谢您的帮助
输入: 第一行:两个空间分隔的整数N和Q,分别表示数组A中的元素数和查询数 第二行:N个表示数组元素的分隔整数