package com.epath.smoketest.tests;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import com.epath.smoketest.pageobjects.AiccAdd;
import com.epath.smoketest.pageobjects.AudioAdd;
import com.epath.smoketest.pageobjects.CustomAdd;
import com.epath.smoketest.pageobjects.DocumentAdd;
import com.epath.smoketest.pageobjects.ImageAdd;
import com.epath.smoketest.pageobjects.LearningActivityAdd;
import com.epath.smoketest.pageobjects.LinkAdd;
import com.epath.smoketest.pageobjects.Login;
import com.epath.smoketest.pageobjects.Navigation;
import com.epath.smoketest.pageobjects.RegistrationCreate;
import com.epath.smoketest.pageobjects.ScormAdd;
import com.epath.smoketest.pageobjects.VideoAdd;
public class Test1 extends TestWatcher {
private Login login;
private Navigation go;
private LearningActivityAdd addLa;
private ImageAdd addImage;
private DocumentAdd addDocument;
private VideoAdd addVideo;
private AudioAdd addAudio;
private LinkAdd addLink;
private CustomAdd addCustom;
private AiccAdd addAicc;
private ScormAdd addScorm;
private RegistrationCreate createRegistration;
WebDriver driver;
public Utils utils = new Utils(driver);
@Before
public void setUp()
{
System.setProperty("webdriver.chrome.driver", "C:\\Users\\admin\\Desktop\\Required Software For Automation\\chromedriver_win32\\chromedriver.exe");
// System.setProperty("webdriver.gecko.driver","D:\\Required automation files\\geckodriver-v0.19.1-win32\\geckodriver.exe");
// driver = new FirefoxDriver();
driver = new ChromeDriver();
login = new Login(driver);
go = new Navigation(driver);
addLa = new LearningActivityAdd(driver);
addImage = new ImageAdd(driver);
addDocument = new DocumentAdd(driver);
addVideo = new VideoAdd(driver);
addAudio = new AudioAdd(driver);
addLink = new LinkAdd(driver);
addCustom = new CustomAdd(driver);
addAicc = new AiccAdd(driver);
addScorm = new ScormAdd(driver);
createRegistration = new RegistrationCreate(driver);
}
@Rule
public TestRule listen = new TestWatcher() {
@Override
public void failed(Throwable t, Description description) {
try {
System.out.println("Test case failed");
captureScreenShot(description.getClassName() + "," + description.getMethodName());
} catch (Exception e) {
e.printStackTrace();
}
}
};
public void captureScreenShot(String fileName) {
System.out.println("Inside capture function");
try {
File scrFile =((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
System.out.println("Screenshot captured");
File targetFile = new File("\\\\192.168.11.32\\admin\\resources\\" + fileName + ".png");
FileUtils.copyFile(scrFile, targetFile);
System.out.println("Completed screenshot functionality");
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void Shallow() throws Exception {
int maxLAs = 1000;
int maxRegs = 1000;
// Print to screen to create log. Log can be copied and pasted to Word document or elsewhere as needed.
System.out.println("\n" + "---------------------------------------------------------------------------------");
System.out.println("---------------------------------------------------------------------------------");
System.out.println("Adding and Registering Learning Activities Automation");
System.out.println("---------------------------------------------------------------------------------");
System.out.println("---------------------------------------------------------------------------------");
// Load the properties
System.out.println("\n" + "---------------------------------------------------------------------------------");
System.out.println("Read in PROPERTIES file");
System.out.println("---------------------------------------------------------------------------------");
Properties prop = new Properties();
InputStream input = null;
String inputFS = "\\\\192.168.11.32\\admin\\resources\\AddRegisterLAs.properties";
input = new FileInputStream(inputFS);
prop.load(input);
// Input file with test data
String testInputFile = prop.getProperty("test_input_file");
System.out.println("\t" + "testInputFile =" + testInputFile);
// Input file for importing users
String userImportFile = prop.getProperty("user_import_file");
System.out.println("\t" + "user_import_file =" + userImportFile);
// URL to test
String testUrl = prop.getProperty("test_url");
System.out.println("\t" + "testUrl =" + testUrl);
// Testing client credentials
String ngage_username = prop.getProperty("ngage_username");
String ngage_password = prop.getProperty("ngage_password");
String ngage_orgcode = prop.getProperty("ngage_orgcode");
System.out.println("\t" + "nGage client credentials: U:" + ngage_username + " P:" + ngage_password + " O:" + ngage_orgcode);
// ePath client credentials
String epath_username = prop.getProperty("epath_username");
String epath_password = prop.getProperty("epath_password");
String epath_orgcode = prop.getProperty("epath_orgcode");
System.out.println("\t" + "ePath client credentials: U:" + epath_username + " P:" + epath_password + " O:" + epath_orgcode);
// Load the path of the resources folder, containing files to be tested
String resourcesPath = "\\\\192.168.11.32\\admin\\resources\\";
System.out.println("\t" + "resourcesPath =" + resourcesPath);
String uniqueString = "";
System.out.println("\n" + "---------------------------------------------------------------------------------");
System.out.println("Read in INPUT file describing LAs and Registrations");
System.out.println("---------------------------------------------------------------------------------");
String inputDataFS = resourcesPath + testInputFile;
InputStream inputData = null;
inputData = new FileInputStream(inputDataFS);
Workbook workbook = new XSSFWorkbook(inputData);
Sheet lasSheet = workbook.getSheet("LAs");
Iterator<Row> rowIterator = lasSheet.iterator();
// Maximum maxLAs learning activities may be specified, at this time
// First row must contain labels
String listLearningActivities[][] = new String[maxLAs+1][4];
int numberLearningActivities = 0;
int r = 0;
int c = 0;
while (rowIterator.hasNext()) {
Row nextRow = rowIterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
listLearningActivities[r][c] = cell.getStringCellValue();
// System.out.print("\n" + " r=" + r + " c=" + c + " content=" + listLearningActivities[r][c] );
c = c + 1;
}
r = r + 1;
c = 0;
}
workbook.close();
inputData.close();
numberLearningActivities = r-1;
System.out.println("\t" + "Completed reading Excel file that describes Learning Activities. # LAs = " + numberLearningActivities);
// Now let's read the Registration data
Sheet registrationSheet = workbook.getSheet("Registrations");
rowIterator = registrationSheet.iterator();
// First row must contain labels.
String listRegistrationInfo[][] = new String[maxRegs+1][9];
int numberRegistrations = 0;
r = 0;
c = 0;
while (rowIterator.hasNext()) {
Row nextRow = rowIterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
listRegistrationInfo[r][c] = cell.getStringCellValue();
// System.out.print("\n" + " r=" + r + " c=" + c + " content=" + listRegistrationInfo[r][c] );
c = c + 1;
}
r = r + 1;
c = 0;
}
workbook.close();
inputData.close();
numberRegistrations = r-1;
System.out.println("\t" + "Completed reading Excel file that describes requested Registrations. # Registrations = " + numberRegistrations);
System.out.println("\n" + "---------------------------------------------------------------------------------");
System.out.println("Begin working in client described in properties file. ");
System.out.println("---------------------------------------------------------------------------------");
login.with("testurl", ngage_username, ngage_password, ngage_orgcode, "welcome1");
assertTrue("-------------------- Dashboard welcome message not present.", login.successMessagePresent());
// Add the learning activities specified in the Excel input file
System.out.println("\n" + "----------------------------------------------------");
System.out.println("Add all learning activities described in input file ");
System.out.println("----------------------------------------------------");
r = 0;
c = 0;
// Start with row1, not row0, since row0 has column labels.
for (r=1; r<=numberLearningActivities; r++) {
switch (listLearningActivities[r][0]) {
case "Image":
go.toAddLearningActivity();
assertTrue("-------------------- Add LA page not present.", addLa.initSuccess());
addLa.addImage();
assertTrue("-------------------- Add image page not present.", addImage.initSuccess());
addImage.of(resourcesPath + listLearningActivities[r][1], listLearningActivities[r][2] + uniqueString, listLearningActivities[r][3]);
assertTrue("-------------------- Adding of image LA was not successful.", addImage.addSuccess());
break;
case "Document":
go.toAddLearningActivity();
assertTrue("-------------------- Add LA page not present.", addLa.initSuccess());
addLa.addDocument();
assertTrue("-------------------- Add document page not present.", addDocument.initSuccess());
addDocument.of(resourcesPath + listLearningActivities[r][1], listLearningActivities[r][2] + uniqueString, listLearningActivities[r][3]);
assertTrue("-------------------- Adding of document LA was not successful.", addDocument.addSuccess());
break;
case "Video":
go.toAddLearningActivity();
assertTrue("-------------------- Add LA page not present.", addLa.initSuccess());
addLa.addVideo();
assertTrue("-------------------- Add video page not present.", addVideo.initSuccess());
addVideo.of(resourcesPath + listLearningActivities[r][1], listLearningActivities[r][2] + uniqueString, listLearningActivities[r][3]);
assertTrue("-------------------- Adding of video LA was not successful.", addVideo.addSuccess());
break;
case "Audio":
go.toAddLearningActivity();
assertTrue("-------------------- Add LA page not present.", addLa.initSuccess());
addLa.addAudio();
assertTrue("-------------------- Add audio page not present.", addAudio.initSuccess());
addAudio.of(resourcesPath + listLearningActivities[r][1], listLearningActivities[r][2] + uniqueString, listLearningActivities[r][3]);
assertTrue("-------------------- Adding of audio LA was not successful.", addAudio.addSuccess());
break;
case "Link":
go.toAddLearningActivity();
assertTrue("-------------------- Add LA page not present.", addLa.initSuccess());
addLa.addLink();
assertTrue("-------------------- Add link page not present.", addLink.initSuccess());
addLink.of(listLearningActivities[r][1], listLearningActivities[r][2] + uniqueString, listLearningActivities[r][3]);
assertTrue("-------------------- Adding of link LA was not successful.", addLink.addSuccess());
break;
case "Custom":
go.toAddLearningActivity();
assertTrue("-------------------- Add LA page not present.", addLa.initSuccess());
addLa.addCustom();
assertTrue("-------------------- Add custom html page not present.", addCustom.initSuccess());
addCustom.of(resourcesPath + listLearningActivities[r][1], listLearningActivities[r][2] + uniqueString, listLearningActivities[r][3]);
assertTrue("-------------------- Adding of custom LA was not successful.", addCustom.addSuccess());
break;
case "AICC URL":
go.toAddLearningActivity();
assertTrue("-------------------- Add LA page not present.", addLa.initSuccess());
addLa.addAicc();
assertTrue("-------------------- Add aicc page not present.", addAicc.initSuccess());
addAicc.of("Remote", listLearningActivities[r][1], listLearningActivities[r][2] + uniqueString, listLearningActivities[r][3]);
assertTrue("-------------------- Adding of aicc by URL LA was not successful.", addAicc.addSuccess());
break;
case "AICC":
go.toAddLearningActivity();
assertTrue("-------------------- Add LA page not present.", addLa.initSuccess());
addLa.addAicc();
assertTrue("-------------------- Add aicc page not present.", addAicc.initSuccess());
addAicc.of("Local", resourcesPath + listLearningActivities[r][1], listLearningActivities[r][2] + uniqueString, listLearningActivities[r][3]);
assertTrue("-------------------- Adding of aicc by filename LA was not successful.", addAicc.addSuccess());
break;
case "SCORM":
go.toAddLearningActivity();
assertTrue("-------------------- Add LA page not present.", addLa.initSuccess());
addLa.addScorm();
assertTrue("-------------------- Add custom html page not present.", addScorm.initSuccess());
addScorm.of(resourcesPath + listLearningActivities[r][1], listLearningActivities[r][2] + uniqueString, listLearningActivities[r][3]);
assertTrue("-------------------- Adding of scorm LA was not successful.", addScorm.addSuccess());
break;
default:
System.out.println("\t" + " r=" + r + " is an unrecognized type of" + listLearningActivities[r][0]);
}
}
System.out.println("\n" + "----------------------------------------------------");
System.out.println("Perform all Registrations described in input file ");
System.out.println("----------------------------------------------------");
// //-Column indices
int cLaType = 0;
int cLaTitle = 1;
int cRegName = 2;
int cRegType = 3;
int cPeople = 4;
int pType =5;
int cDueDate = 6;
int cTrack = 7;
int cAccess = 8;
int calNo =2;
for (r=1; r<=numberRegistrations; r++) {
go.toWorkspace();
createRegistration.with
(listRegistrationInfo[r][cLaType] ,
listRegistrationInfo[r][cLaTitle] + uniqueString,
listRegistrationInfo[r][cRegName],
listRegistrationInfo[r][cRegType],
listRegistrationInfo[r][cPeople],
listRegistrationInfo[r][pType],
listRegistrationInfo[r][cDueDate],
listRegistrationInfo[r][cTrack],
listRegistrationInfo[r][cAccess],
calNo);
calNo=calNo+2;
assertTrue("-------------------- Creating a registration was not successful.", createRegistration.addSuccess());
}
// Logout as System Administrator
go.toSignOut();
System.out.println("\n" + "---------------------------------------------------------------------------------");
System.out.println("---------------------------------------------------------------------------------");
System.out.println("Adding and registering of LAs is Complete!");
System.out.println("---------------------------------------------------------------------------------");
System.out.println("---------------------------------------------------------------------------------");
}
@After
public void teardown () {
driver.quit();
}
}
我正在尝试捕获失败的测试用例的屏幕截图,但我正面临这个问题。 错误是
=============================================== ======
出现错误:
Test case failed
Inside capture function
org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:48:19 -0700'
System info: host: 'c02win', ip: '192.168.11.42', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_152'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:130)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:658)
at org.openqa.selenium.remote.RemoteWebDriver.getScreenshotAs(RemoteWebDriver.java:340)
at com.epath.smoketest.tests.Test1.captureScreenShot(Test1.java:105)
at com.epath.smoketest.tests.Test1$1.failed(Test1.java:95)
at org.junit.rules.TestWatcher.failedQuietly(TestWatcher.java:84)
at org.junit.rules.TestWatcher.access$300(TestWatcher.java:46)
at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:62)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:539)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:761)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:461)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:207)
答案 0 :(得分:0)
这里的问题是您正在执行代码以在“失败的”阶段截取屏幕截图。此时,测试已经失败,驱动程序已经关闭。您需要做的是将您的逻辑用于在“评估”阶段捕获屏幕截图。 在此阶段,您可以检查异常,并在发生异常时,截取屏幕截图。类似的东西:
@Override
public void evaluate() throws Throwable {
try {
statement.evaluate();
} catch (Throwable t) {
// exception will be thrown only when a test fails.
captureScreenShot(frameworkMethod.getName());
// rethrow to allow the failure to be reported by JUnit
throw t;
}
}