Skip to content

Commit 81e5976

Browse files
authored
Merge pull request #11 from shivam198smart/master
Added Screenshot Functionality
2 parents 93677d8 + 64346e2 commit 81e5976

File tree

3 files changed

+55
-16
lines changed

3 files changed

+55
-16
lines changed

src/main/java/UITestFramework/CreateSession.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
import org.apache.commons.exec.ExecuteException;
2121
import org.apache.log4j.PropertyConfigurator;
2222
import org.openqa.selenium.WebDriver;
23+
import org.openqa.selenium.WebDriverException;
2324
import org.openqa.selenium.remote.DesiredCapabilities;
2425
import org.testng.annotations.AfterMethod;
26+
import org.testng.annotations.AfterSuite;
2527
import org.testng.annotations.BeforeMethod;
28+
import org.testng.annotations.BeforeSuite;
2629
import org.testng.annotations.Parameters;
2730

2831

@@ -43,8 +46,14 @@ public class CreateSession {
4346
protected File file = new File("");
4447
Properties configProp = new Properties();
4548
String OS;
46-
47-
49+
/**
50+
* Returns the instance of the webdriver.
51+
* @return webdriver instance
52+
*/
53+
public WebDriver getDriver() {
54+
return driver;
55+
}
56+
4857

4958
/**
5059
* this method starts Appium server. Calls startAppiumServer method to start the session depending upon your OS.
@@ -143,7 +152,7 @@ public synchronized void androidDriver(String buildPath, Method methodName) thro
143152
capabilities.setCapability("app", app.getAbsolutePath());
144153
capabilities.setCapability(MobileCapabilityType.NO_RESET, false);
145154
capabilities.setCapability("automationName", "UiAutomator2");
146-
driver = new AndroidDriver( new URL("http://localhost:4723/wd/hub"), capabilities);
155+
driver = new AndroidDriver( new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
147156

148157
}
149158

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
package UITestFramework.retryLogic;
22

33

4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.text.SimpleDateFormat;
7+
import java.util.Date;
48
import java.util.Set;
9+
10+
import org.apache.commons.io.FileUtils;
11+
import org.openqa.selenium.OutputType;
12+
import org.openqa.selenium.TakesScreenshot;
13+
import org.openqa.selenium.WebDriver;
514
import org.testng.ITestContext;
615
import org.testng.ITestListener;
716
import org.testng.ITestNGMethod;
817
import org.testng.ITestResult;
18+
import UITestFramework.CreateSession;
919

10-
public class TestListener implements ITestListener {
11-
@Override
20+
public class TestListener extends CreateSession implements ITestListener {
21+
22+
1223
public void onFinish(ITestContext context) {
1324
Set<ITestResult> failedTests = context.getFailedTests().getAllResults();
1425
for (ITestResult temp : failedTests) {
@@ -23,20 +34,37 @@ public void onFinish(ITestContext context) {
2334
}
2435
}
2536

26-
public void onTestStart(ITestResult result) {
27-
28-
}
37+
public void onTestStart(ITestResult result) { }
2938

3039
public void onTestSuccess(ITestResult result) { }
31-
32-
public void onTestFailure(ITestResult result) { }
40+
41+
@Override
42+
public void onTestFailure(ITestResult result) {
43+
Object TestListener = result.getInstance();
44+
WebDriver webDriver = ((CreateSession) TestListener).getDriver();
45+
if (webDriver != null)
46+
{
47+
File scr = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
48+
String filename = new SimpleDateFormat("yyyyMMddhhmmss'.jpg'").format(new Date());
49+
File dest = new File("./Screenshots/" + filename); //Directory where Screenshot get saved.
50+
try
51+
{
52+
FileUtils.copyFile(scr, dest);
53+
}
54+
catch (IOException e) {
55+
// TODO Auto-generated catch block
56+
e.printStackTrace();
57+
}
58+
}
59+
}
3360

3461
public void onTestSkipped(ITestResult result) { }
3562

3663
public void onTestFailedButWithinSuccessPercentage(ITestResult result) { }
3764

38-
public void onStart(ITestContext context) {
39-
}
40-
}
65+
public void onStart(ITestContext context) { }
66+
67+
}
68+
4169

4270

testng.xml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
22
<suite name="mobile-automation-framework">
33
<parameter name="os" value="${os}" />
4-
4+
<listeners>
5+
<listener class-name="UITestFramework.retryLogic.TestListener"/>
6+
</listeners>
57
<test name="automation-tests">
68
<packages>
7-
<package name="tests.testngTests" />
9+
<package name="tests.testngTests" />
810
</packages>
911
</test>
10-
</suite>
12+
</suite>

0 commit comments

Comments
 (0)