Skip to content

Commit

Permalink
Update takeScreenshotScenario in Hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
vothaian committed May 24, 2024
1 parent 2e89d8e commit fbd2616
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 51 deletions.
29 changes: 18 additions & 11 deletions src/main/java/com/anhtester/helpers/CaptureHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
package com.anhtester.helpers;

import com.anhtester.constants.FrameworkConstants;
import com.anhtester.driver.DriverManager;
import com.anhtester.utils.LogUtils;
import io.cucumber.java.Scenario;
import org.apache.commons.io.FileUtils;
import org.monte.media.Format;
import org.monte.media.FormatKeys.MediaType;
Expand Down Expand Up @@ -40,13 +42,11 @@ public class CaptureHelpers extends ScreenRecorder {
private static String name;
private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss");

//Hàm xây dựng
public CaptureHelpers(GraphicsConfiguration cfg, Rectangle captureArea, Format fileFormat, Format screenFormat, Format mouseFormat, Format audioFormat, File movieFolder, String name) throws IOException, AWTException {
super(cfg, captureArea, fileFormat, screenFormat, mouseFormat, audioFormat, movieFolder);
this.name = name;
}

//Hàm này bắt buộc để ghi đè custom lại hàm trong thư viên viết sẵn

@Override
protected File createMovieFile(Format fileFormat) throws IOException {

Expand All @@ -57,17 +57,15 @@ protected File createMovieFile(Format fileFormat) throws IOException {
}
return new File(movieFolder, name + "_" + dateFormat.format(new Date()) + "." + Registry.getInstance().getExtension(fileFormat));
}

// Hàm Start record video

public static void startRecord(String methodName) {
//Tạo thư mục để lưu file video vào
File file = new File("./" + FrameworkConstants.EXPORT_VIDEO_PATH + File.separator + methodName + File.separator);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = screenSize.width;
int height = screenSize.height;

System.out.println("width" + width);
System.out.println("height" + height);
LogUtils.info("width: " + width);
LogUtils.info("height: " + height);

Rectangle captureSize = new Rectangle(0, 0, width, height);

Expand All @@ -80,8 +78,7 @@ public static void startRecord(String methodName) {
e.printStackTrace();
}
}

// Stop record video

public static void stopRecord() {
try {
screenRecorder.stop();
Expand Down Expand Up @@ -110,7 +107,7 @@ public static void captureScreenshot(WebDriver driver, String screenName) {
LogUtils.info("Screenshot taken: " + screenName);
LogUtils.info("Screenshot taken current URL: " + driver.getCurrentUrl());
} catch (Exception e) {
System.out.println("Exception while taking screenshot: " + e.getMessage());
LogUtils.info("Exception while taking screenshot: " + e.getMessage());
}
}

Expand Down Expand Up @@ -141,5 +138,15 @@ public static File getScreenshot(String screenshotName) {
return file;
}

public static void takeScreenshotScenario(Scenario scenario, String screenshotName) {
try {
TakesScreenshot ts = (TakesScreenshot) DriverManager.getDriver();
byte[] src = ts.getScreenshotAs(OutputType.BYTES);
scenario.attach(src, "image/png", screenshotName);
} catch (Exception e) {
LogUtils.error("Unable to take screenshot");
LogUtils.error(e);
}
}

}
13 changes: 5 additions & 8 deletions src/main/java/com/anhtester/keywords/WebUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -1330,20 +1330,17 @@ public static List<String> getListElementsText(By by) {
* @return true/false
*/
@Step("Verify element exists {0}")
public static boolean checkElementExists(By by) {
smartWait();

boolean res;
public static Boolean checkElementExists(By by) {
boolean result;
List<WebElement> elementList = getWebElements(by);
if (elementList.size() > 0) {
res = true;
result = true;
LogUtils.info("Element existing");
} else {
res = false;
result = false;
LogUtils.error("Element not exists");

}
return res;
return result;
}


Expand Down
1 change: 0 additions & 1 deletion src/test/java/com/anhtester/hooks/CucumberListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.io.File;
import java.io.IOException;

import static com.anhtester.constants.FrameworkConstants.DELETE_TEMP_FOLDER;
import static com.anhtester.constants.FrameworkConstants.YES;

public class CucumberListener implements EventListener {
Expand Down
15 changes: 4 additions & 11 deletions src/test/java/com/anhtester/hooks/Hooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ public static void before_all() {
LogUtils.info("Deleted directory target/allure-results");
FileUtils.deleteDirectory(new File("ExportData"));
LogUtils.info("Deleted directory ExportData");
FileUtils.deleteDirectory(new File("reports"));
LogUtils.info("Deleted directory reports");
}
} catch (IOException e) {
LogUtils.error(e.getMessage());
e.printStackTrace();
}
}
Expand Down Expand Up @@ -103,21 +102,15 @@ public void afterScenario(Scenario scenario) {
public void afterStep(Scenario scenario) {
if (scenario.getStatus().equals(Status.PASSED) && SCREENSHOT_PASSED_STEPS.equals(YES)) {
WebUI.waitForPageLoaded();

byte[] screenshot = ((TakesScreenshot) DriverManager.getDriver()).getScreenshotAs(OutputType.BYTES);
scenario.attach(screenshot, "image/png", "Screenshot passed step");
CaptureHelpers.takeScreenshotScenario(scenario,"Screenshot passed step");
}
if (scenario.getStatus().equals(Status.FAILED) && SCREENSHOT_FAILED_STEPS.equals(YES)) {
WebUI.waitForPageLoaded();

byte[] screenshot = ((TakesScreenshot) DriverManager.getDriver()).getScreenshotAs(OutputType.BYTES);
scenario.attach(screenshot, "image/png", "Screenshot failed step");
CaptureHelpers.takeScreenshotScenario(scenario,"Screenshot failed step");
}
if (SCREENSHOT_ALL_STEPS.equals(YES)) {
WebUI.waitForPageLoaded();

byte[] screenshot = ((TakesScreenshot) DriverManager.getDriver()).getScreenshotAs(OutputType.BYTES);
scenario.attach(screenshot, "image/png", "Screenshot step");
CaptureHelpers.takeScreenshotScenario(scenario,"Screenshot step");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class SignInPageCRM extends CommonPageCRM {

private String pageUrl = "/signin";
private String pageTitle = "Sign in | RISE - Ultimate Project Manager and CRM";
private String pageTitle = "Sign in | RISE CRM | Anh Tester Demo";

public By inputEmail = By.xpath("//input[@id='email']");
public By inputPassword = By.xpath("//input[@id='password']");
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/config/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ BROWSER = chrome
TARGET = local
# headless mode only for chrome or firefox and local execution
HEADLESS = false
URL_CRM = https://rise.fairsketch.com/signin
URL_CRM = https://rise.anhtester.com/signin
# grid url and port
REMOTE_URL = localhost
REMOTE_PORT = 4444
Expand Down Expand Up @@ -57,4 +57,4 @@ SCREENSHOT_FAILED_STEPS = yes
# All Steps Screenshot
SCREENSHOT_ALL_STEPS = no

DELETE_TEMP_FOLDER = yes
DELETE_TEMP_FOLDER = no
34 changes: 17 additions & 17 deletions src/test/resources/features/SigninCRM.feature
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
Feature: Sign in page CRM

@Regression @device_Window_10 @author_AnVo
Scenario Outline: Sign in CRM with an email valid
Given User navigate to url "<url>"
When User login with username "<username>" and password "<password>" valid
Then The user redirect to Dashboard page
Examples:
| url | username | password |
| https://rise.fairsketch.com/signin | admin@demo.com | riseDemo |
@Regression @device_Window_10 @author_AnVo
Scenario Outline: Sign in CRM with an email valid
Given User navigate to url "<url>"
When User login with username "<username>" and password "<password>" valid
Then The user redirect to Dashboard page
Examples:
| url | username | password |
| https://rise.anhtester.com/signin | admin@example.com | 123456 |

@Smoke @device_Window_10 @author_AnVo
Scenario Outline: Sign in CRM with an email invalid
Given User navigate to url "<url>"
When User login with username "<username>" and password "<password>" invalid
Then The user can not redirect to Dashboard page
And The error message is displays
Examples:
| url | username | password |
| https://rise.fairsketch.com/signin | admin123@demo.com | riseDemo |
@Smoke @device_Window_10 @author_AnVo
Scenario Outline: Sign in CRM with an email invalid
Given User navigate to url "<url>"
When User login with username "<username>" and password "<password>" invalid
Then The user can not redirect to Dashboard page
And The error message is displays
Examples:
| url | username | password |
| https://rise.anhtester.com/signin | demo@example.com | 123456 |

0 comments on commit fbd2616

Please sign in to comment.