我遇到了一个问题,我的测试按方法在浏览器中执行操作,但是在TestNG中显示为失败,标题中提到了错误。请查看并帮助解决此问题。我对自动化还很陌生,因此无法解决问题。
基类:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestListBase {
String chromeDriverPath = "src/main/resources/chromedriver.exe";
String ieDriverPath = "src/main/resources/IEDriverServer.exe";
WebDriver driver;
Properties testData;
static String testSuitName;
static String baseFolder = "config\\";
public static Object[][] findPropertyFiles(String testName) {
// First read test suite name.
Properties p = readProperties(baseFolder + "TestSuitToRun.txt");
testSuitName = p.getProperty("TestSuitName");
// Read test properties
File[] files = new File("config\\" + testSuitName + "\\" + testName + "\\").listFiles();
List<String> result = new ArrayList<String>();
for (File file : files) {
if (file.isFile()) {
result.add(file.getAbsolutePath());
}
}
Object[][] resultarray = new Object[result.size()][1];
for (int i=0; i<result.size(); i++) {
resultarray[i][0] = result.get(i);
}
return resultarray;
}
private static Properties readProperties(String path) {
Properties properties = new Properties();
InputStream input = null;
try {
input = new FileInputStream(path);
properties.load(input);
}
catch (Exception ex) {
}
finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return properties;
}
protected void readTestData(String testDataPath) {
// Read base properties
Properties baseProperties = readProperties(baseFolder + testSuitName + "\\baseProperties.txt");
testData = readProperties(testDataPath);
testData.putAll(baseProperties);
}
protected void waitForBrowser() {
waitForBrowser(1);
}
protected void waitForBrowser(int timeoutSec) {
try {
Thread.sleep(timeoutSec * 1000);
} catch (InterruptedException e) {
System.out.println("got interrupted!");
}
}
protected void setDriver(WebDriver driverparam) {
this.driver = driverparam;
}
protected WebDriver getDriver() {
return this.driver;
}
protected void intializeDriver() {
String browserType = testData.getProperty("browser");
if (browserType.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
driver = new ChromeDriver();
}
else if (browserType.equalsIgnoreCase("ie")) {
System.setProperty("webdriver.ie.driver", ieDriverPath);
driver = new InternetExplorerDriver();
}
else if (browserType.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
}
else {
System.out.println("Unknown Browser Type:" + browserType);
return;
}
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
}
儿童班:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class AddressableTarget extends TestListBase {
@DataProvider(name = "PropertyFile")
public static Object[][] getPropertyFiles() {
return findPropertyFiles("AddressableTarget");
}
@Test(priority = 0, dataProvider = "PropertyFile")
public void runTest(String filename) {
readTestData(filename);
intializeDriver();
openBrowser();
login();
gotoAddressableTarget();
terminateBrowser();
}
@Test(priority = 1)
void openBrowser() {
driver.get(testData.getProperty("baseUrl"));
}
@Test(priority = 2)
public void login() {
driver.findElement(By.xpath("//input[@name='logonIDmanual']"))
.sendKeys(testData.getProperty("user"));
driver.findElement(By.xpath("//input[@type='password']"))
.sendKeys(testData.getProperty("password"));
driver.findElement(By.xpath("//input[@type='submit']"))
.click();
waitForBrowser();
}
@Test(priority = 3)
public void gotoAddressableTarget() {
driver.findElement(By.xpath("//span[contains(text(), 'Addressable Target')]")).click();
waitForBrowser();
}
@Test(priority = 4)
public void terminateBrowser() {
driver.quit();
}
}
控制台中第一个失败的方法:
[RemoteTestNG] detected TestNG version 6.10.0
[TestNG] Running:
C:\AppData\Local\Temp\testng-eclipse--1577707927\testng-customsuite.xml
Starting ChromeDriver 2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a) on port 20262
Only local connections are allowed.
Nov 13, 2018 10:56:22 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
PASSED: runTest("C:\eclipse-workspace\Automation\config\QA\AddressableTarget\AddressableTarget.txt")
PASSED: terminateBrowser
FAILED: openBrowser
org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?
Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03-11T20:26:55.152Z'
System info: host: 'USLJTMWF72', ip: '10.5.152.19', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_181'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:125)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:545)
at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:273)
at com.AddressableTarget.openBrowser(AddressableTarget.java:47)
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:104)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:851)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1177)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:756)
at org.testng.TestRunner.run(TestRunner.java:610)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
at org.testng.TestNG.runSuites(TestNG.java:1133)
at org.testng.TestNG.run(TestNG.java:1104)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
看一下我的POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>AdVantics</groupId>
<artifactId>AdVanticsQAAutomation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- Selenium -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-htmlunit-driver</artifactId>
<version>2.52.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.11.0</version>
</dependency>
<!-- TestNG -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
<!-- <suiteXmlFile>src/main/resources/testng.xml</suiteXmlFile> -->
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
最佳答案
调用quit方法后,您将无法使用用户驱动程序。
只需从所有方法中删除@Test批注,并将其保留为runTest方法即可,它将解决您的问题。