目前,我有一个Selenium网格设置,有1个本地集线器和2个本地节点。集线器能够分发测试以并行运行并将其分发到节点。我正在并行运行测试。
以下是基础测试
public abstract class BaseTest
{
String testFolder;
String testName;
protected String envName;
protected Configuration config;
protected String host;
protected RemoteWebDriver driver;
protected String proxy;
protected SomeData someData;
protected SomeController someController;
public BaseTest() {
}
public BaseTest( String testFolder, String testName)
{
this.testFolder = testFolder;
this.testName = testName;
this.envName = System.getProperty("config");
this.proxy = System.getProperty("proxy");
config = this.envName;
}
@BeforeMethod
public void startTest(Method testMethod) {
LOG.info("Starting test: " + testMethod.getName());
try {
this.someData = new SomeData();
this.driver = WebDriverSetup.getDriver();
this.someController = new someController(this.driver, this.someData);
driver.navigate().to("https://" + this.host);
} catch (MalformedURLException e) {
System.out.println("MalformedURLException");
}
}
@AfterMethod
public void closeWindow() {
driver.close();
driver.quit();
}
}
以下是获取RemoteWebDriver的类:
public class WebDriverSetup {
public static RemoteWebDriver getDriver() throws MalformedURLException{
String SELENIUM_HUB_URL = "http://localhost:4444/wd/hub";
ThreadLocal<RemoteWebDriver> remoteWebDriver = null;
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
String proxy = System.getProperty("proxy");
if (proxy != null && !proxy.isEmpty()) {
System.out.println("Using proxy: " + proxy);
capabilities.setCapability(CapabilityType.PROXY, proxy);
}
try {
remoteWebDriver = new ThreadLocal<RemoteWebDriver>();
remoteWebDriver.set(new RemoteWebDriver(new URL(SELENIUM_HUB_URL),
capabilities));
} catch (MalformedURLException e) {
System.out.println("Tackle Issue with RemoteDriverSetup");
}
remoteWebDriver.get().manage().window()
.setSize(new Dimension(2880, 1524));
remoteWebDriver.get().manage().timeouts()
.pageLoadTimeout(10, TimeUnit.SECONDS);
remoteWebDriver.get().manage().timeouts()
.implicitlyWait(10, TimeUnit.SECONDS);
return remoteWebDriver.get();
}
}
我的测试套件就像:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Multiple Tests Suite" verbose="1" parallel="methods">
<test name="Test1">
<classes>
<class name="com.itesteverything.qa.Tests"></class>
</classes>
</test>
</suite>
测试如下:
public class Tests extends BaseTest {
@Parameters({"testName", "env" })
public Tests( @Optional String testName, @Optional String env ) {
super( null, testName, null, env );
}
@BeforeMethod
public void setup() throws Exception {
//setSomeData
}
public void test1() throws Exception {
use driver from super
use someData from super
use someController is using the driver from super
}
public void test2() throws Exception {
use driver from super
use someData from super
use someController is using the driver from super
}
运行这些测试时,我收到以下错误
构建信息:版本:&#39; 2.44.0&#39;,修订版:&#39; 76d78cf323ce037c5f92db6c1bba601c2ac43ad8&#39;,时间:&#39; 2014-10-23 13:11:40&#39; 驱动程序信息:driver.version:RemoteWebDriver org.openqa.selenium.remote.SessionNotFoundException:会话ID为空。调用quit()后使用WebDriver? 构建信息:版本:&#39; 2.44.0&#39;,修订版:&#39; 76d78cf323ce037c5f92db6c1bba601c2ac43ad8&#39;,时间:&#39; 2014-10-23 13:11:40&#39; 驱动程序信息:driver.version:RemoteWebDriver 在org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158) 在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:572) 在org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352) 在org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:393) 在org.openqa.selenium.By $ ById.findElement(By.java:214) 在org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344) 在org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:59) 在com.sun.proxy。$ Proxy25.sendKeys(未知来源) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80) 在org.testng.internal.Invoker.invokeMethod(Invoker.java:673) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:842) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1166) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615) 在java.lang.Thread.run(Thread.java:745) 测试失败:test2 FAILED REASON:会话ID为空。调用quit()后使用WebDriver?
有人知道吗?
提前致谢!
答案 0 :(得分:2)
不要在基类中设置驱动程序,根本没有驱动程序属性。同一个实例被不同的threadlocal驱动程序覆盖。
每当您想要运行测试时,请参考测试方法本身和After / before方法中的WebDriverSetup.getDriver()。
答案 1 :(得分:1)
@AfterMethod
在每种方法之后运行。 而且U只运行一次。因此,在第一个方法U关闭后,它将关闭,因为显示堆栈跟踪。