Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix[TE-14994]: Image search actions community addon functionality fixed #38

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion image_search_actions/pom.xml
SivasaiPodugu marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SivasaiPodugu Please do update version in Kibbutz Bootstrap

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<testsigma.sdk.version>1.2.1_cloud</testsigma.sdk.version>
<testsigma.sdk.version>1.2.4_cloud</testsigma.sdk.version>
<junit.jupiter.version>5.8.0-M1</junit.jupiter.version>
<testsigma.addon.maven.plugin>1.0.0</testsigma.addon.maven.plugin>
<maven.source.plugin.version>3.2.1</maven.source.plugin.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
import com.testsigma.sdk.annotation.TestData;
import com.testsigma.sdk.annotation.TestStepResult;
import lombok.Data;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.interactions.Actions;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

@Data
Expand All @@ -32,22 +36,48 @@ public class ClickOnImage extends WebAction {

@Override
protected Result execute() throws NoSuchElementException {
TakesScreenshot scrShot =((TakesScreenshot)this.driver);
File baseImageFile=scrShot.getScreenshotAs(OutputType.FILE);
String url = testStepResult.getScreenshotUrl();
ocr.uploadFile(url, baseImageFile);
FindImageResponse response = ocr.findImage(testData1.getValue().toString());
int x1 = response.getX1();
int x2 = response.getX2();
int y1 = response.getY1();
int y2 = response.getY2();
Double clickLocationX = (double) ((x1 + x2) / 2);
Double clickLocationY = (double) ((y1 + y2) / 2);

Actions actions = new Actions(driver);
actions.moveByOffset(clickLocationX.intValue(), clickLocationY.intValue()).click().build().perform();
setSuccessMessage("Image Found :" + response.getIsFound() +
" Image coordinates :" + "x1-" + x1 + ", x2-" + x2 + ", y1-" + y1 + ", y2-" + y2);
return Result.SUCCESS;
try {
TakesScreenshot scrShot = ((TakesScreenshot) this.driver);
File baseImageFile = scrShot.getScreenshotAs(OutputType.FILE);
BufferedImage bufferedImage = ImageIO.read(baseImageFile);
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
logger.info("Width of image: " + width);
logger.info("Height of image: " + height);

long innerHeight = (Long) ((JavascriptExecutor) driver).executeScript("return window.innerHeight;");
long innerWidth = (Long) ((JavascriptExecutor) driver).executeScript("return window.innerWidth;");
int windowHeight = (int) innerHeight;
int windowWidth = (int) innerWidth;
logger.info("inner Width of window: " + windowWidth);
logger.info("inner Height of window: " + windowHeight);

String url = testStepResult.getScreenshotUrl();
ocr.uploadFile(url, baseImageFile);
FindImageResponse response = ocr.findImage(testData1.getValue().toString());
int clickLocationX = (response.getX1() + response.getX2()) / 2;
int clickLocationY = (response.getY1() + response.getY2()) / 2;

double xRelative = (double) clickLocationX / width;
double yRelative = (double) clickLocationY / height;

int xCoordinate = (int) (xRelative * windowWidth);
int yCoordinate = (int) (yRelative * windowHeight);
logger.info("Click Location X: " + xCoordinate);
logger.info("Click Location Y: " + yCoordinate);

Actions actions = new Actions(driver);
actions.moveByOffset(xCoordinate, yCoordinate).click().build().perform();
setSuccessMessage("Image Found :" + response.getIsFound() +
" Image coordinates :" + "x1-" + response.getX1() + ", x2-" + response.getX2() + ", y1-" + response.getY1() + ", y2-" + response.getY2());
Thread.sleep(1000);
return Result.SUCCESS;
}
catch (Exception e){
logger.info("Exception: "+ ExceptionUtils.getStackTrace(e));
setErrorMessage("Exception occurred while performing click action");
return Result.FAILED;
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
import com.testsigma.sdk.annotation.TestData;
import com.testsigma.sdk.annotation.TestStepResult;
import lombok.Data;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.interactions.Actions;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

@Data
Expand All @@ -35,22 +39,47 @@ public class ClickOnImageWithThreshold extends WebAction {

@Override
protected Result execute() throws NoSuchElementException {
TakesScreenshot scrShot =((TakesScreenshot)this.driver);
File baseImageFile=scrShot.getScreenshotAs(OutputType.FILE);
String url = testStepResult.getScreenshotUrl();
ocr.uploadFile(url, baseImageFile);
FindImageResponse response = ocr.findImage(testData1.getValue().toString(), Float.valueOf(testData2.getValue().toString()));
int x1 = response.getX1();
int x2 = response.getX2();
int y1 = response.getY1();
int y2 = response.getY2();
Double clickLocationX = (double) ((x1 + x2) / 2);
Double clickLocationY = (double) ((y1 + y2) / 2);

Actions actions = new Actions(driver);
actions.moveByOffset(clickLocationX.intValue(), clickLocationY.intValue()).click().build().perform();
setSuccessMessage("Image Found :" + response.getIsFound() +
" Image coordinates :" + "x1-" + x1 + ", x2-" + x2 + ", y1-" + y1 + ", y2-" + y2);
return Result.SUCCESS;
try {
TakesScreenshot scrShot = ((TakesScreenshot) this.driver);
File baseImageFile = scrShot.getScreenshotAs(OutputType.FILE);
BufferedImage bufferedImage = ImageIO.read(baseImageFile);
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
logger.info("Width of image: " + width);
logger.info("Height of image: " + height);

long innerHeight = (Long) ((JavascriptExecutor) driver).executeScript("return window.innerHeight;");
long innerWidth = (Long) ((JavascriptExecutor) driver).executeScript("return window.innerWidth;");
int windowHeight = (int) innerHeight;
int windowWidth = (int) innerWidth;
logger.info("inner Width of window: " + windowWidth);
logger.info("inner Height of window: " + windowHeight);

String url = testStepResult.getScreenshotUrl();
ocr.uploadFile(url, baseImageFile);
FindImageResponse response = ocr.findImage(testData1.getValue().toString(), Float.valueOf(testData2.getValue().toString()));
int clickLocationX = (response.getX1() + response.getX2()) / 2;
int clickLocationY = (response.getY1() + response.getY2()) / 2;

double xRelative = (double) clickLocationX / width;
double yRelative = (double) clickLocationY / height;

int xCoordinate = (int) (xRelative * windowWidth);
int yCoordinate = (int) (yRelative * windowHeight);
logger.info("Click Location X: " + xCoordinate);
logger.info("Click Location Y: " + yCoordinate);

Actions actions = new Actions(driver);
actions.moveByOffset(xCoordinate, yCoordinate).click().build().perform();
setSuccessMessage("Image Found :" + response.getIsFound() +
" Image coordinates :" + "x1-" + response.getX1() + ", x2-" + response.getX2() + ", y1-" + response.getY1() + ", y2-" + response.getY2());
Thread.sleep(1000);
return Result.SUCCESS;
}
catch (Exception e){
logger.info("Exception: "+ ExceptionUtils.getStackTrace(e));
setErrorMessage("Exception occurred while performing click action");
return Result.FAILED;
}
}
}