如何在不同的测试类之间共享相同的selenium测试?我一直得到nullpointexception,无法弄清楚如何解决它。我已经在这个问题上存了一段时间,并在几个小时内搜索了这个问题,但仍然没有解决它。请帮忙。
这是我的设置类
import static org.testng.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
public class SettingsClass {
WebDriver driver;
@BeforeTest
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "chromeDriver_path");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized", "chrome.switches", "--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.manage().deleteAllCookies(); // delete all cookies
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
@AfterTest
public void tearDown() throws Exception {
driver.quit();
}
这是我的测试类
import org.openqa.selenium.By;
import org.testng.annotations.Test;
import com.Settings;
public class ClassA extends SettingsClass {
boolean acceptNextAlert = true;
StringBuffer verificationErrors = new StringBuffer();
@Test
public void test() throws Exception {
driver.get("http://www.fakeurl.com");
driver.findElement(By.id("username")).sendKeys("admin");
driver.findElement(By.id("password")).sendKeys("admin");
driver.findElement(By.name("submit")).click();
}
}
以下是我不断收到的错误消息
java.lang.NullPointerException
at com.ClassA.test(ClassA.java:15)
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:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:659)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:845)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1153)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:771)
at org.testng.TestRunner.run(TestRunner.java:621)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
at org.testng.SuiteRunner.run(SuiteRunner.java:259)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1199)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1124)
at org.testng.TestNG.run(TestNG.java:1032)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
非常感谢任何建议和帮助。提前谢谢!
答案 0 :(得分:0)
尝试,
public class SettingsClass {
public WebDriver driver;
@BeforeTest
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "chromeDriver_path");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized", "chrome.switches", "--disable-extensions");
driver = new ChromeDriver(options);
driver.manage().deleteAllCookies(); // delete all cookies
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
@AfterTest
public void tearDown() throws Exception {
driver.quit();
}
}
答案 1 :(得分:0)
这种依赖<access origin="*" subdomains="true"></access>
<tizen:privilege name="http://tizen.org/privilege/internet"/>
实例化基类中的@BeforeTest
实例的方法仍然存在问题,并且所有测试类都扩展了这个基类(WebDriver
in这种情况)因为默认情况下,SettingsClass
方法被设计为在套件xml文件中每@BeforeTest
只涉及一次。因此,如果您有两个或更多此类基类的子类(在这种情况下为<test>
),您将最终在测试中看到相同的SettingsClass
,即
如果您执行以下操作,您将看到相同的NullPointerException
:
NullPointerException
的新类,该类也扩展ClassB
(和)SettingsClass
标记中加入ClassA
和ClassB
。您应该考虑转移到<test>
模型中,该模型将通过以下机制之一进行初始化:
ThreadLocal
带注释的方法@BeforeMethod
IInvokedMethodListener#beforeInvocation
接口的实现。您可以参考我创建的博客文章,其中讨论了使用TestNG监听器和org.testng.IHookable
变量来管理并发webdriver实例。请看看是否有帮助。
博客链接:https://rationaleemotions.wordpress.com/2013/07/31/parallel-webdriver-executions-using-testng/