Skip to content

Commit

Permalink
1. fixed visual test implementation
Browse files Browse the repository at this point in the history
2. fixed issue with test status showing terminated
  • Loading branch information
anandbagmar committed Mar 15, 2021
1 parent ba9e7c5 commit 2ecb530
Show file tree
Hide file tree
Showing 17 changed files with 85 additions and 77 deletions.
2 changes: 0 additions & 2 deletions src/main/java/com/znsio/e2e/entities/TEST_CONTEXT.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@ public class TEST_CONTEXT {
public static final String CURRENT_DRIVER = "currentDriver";
public static final String ALL_DRIVERS = "allDrivers";
public static final String CURRENT_USER_PERSONA = "currentUserPersona";
public static final String APPLITOOLS_APPLICATION_NAME = "applitoolsApplicationName";
public static final String WEB_BASE_URL = "webBaseUrl";
}
98 changes: 55 additions & 43 deletions src/main/java/com/znsio/e2e/runner/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public class Runner {
public static final boolean IS_MAC = OS_NAME.toLowerCase().startsWith("mac");
public static final String USER_DIRECTORY = System.getProperty("user.dir");
public static final String USER_NAME = System.getProperty("user.name");
public static final String BASE_URL_FOR_WEB = "BASE_URL_FOR_WEB";
private static final String APP_NAME = "APP_NAME";
private static final String IS_VISUAL = "IS_VISUAL";
private static final String CHROME = "chrome";
private static final String PLUGIN = "--plugin";
private static final String tempDirectory = "temp";
Expand All @@ -50,19 +53,13 @@ public class Runner {
private static final String BRANCH_NAME = NOT_SET;
private static final String LOG_PROPERTIES_FILE = NOT_SET;
private static final String DEFAULT_LOG_DIR = "target";
public static Platform platform = Platform.android;
public static boolean isVisualTestingEnabled = false;
public static BatchInfo batchName;
private static final String APP_NAME = "APP_NAME";
private static final String APP_PATH = "APP_PATH";
private static final String BASE_URL_FOR_WEB = "BASE_URL_FOR_WEB";
private static final String BROWSER = "BROWSER";
private static final String CAPS = "CAPS";
private static final String CONFIG_FILE = "CONFIG_FILE";
private static final String DEVICE_LAB_URL = "DEVICE_LAB_URL";
private static final String ENVIRONMENT_CONFIG_FILE = "ENVIRONMENT_CONFIG_FILE";
private static final String EXECUTED_ON = NOT_SET;
private static final String IS_VISUAL = "IS_VISUAL";
private static final String INFERRED_TAGS = "INFERRED_TAGS";
private static final String LAUNCH_NAME = "LAUNCH_NAME";
private static final String LOG_DIR = "LOG_DIR";
Expand All @@ -72,11 +69,13 @@ public class Runner {
private static final String TAG = "TAG";
private static final String TARGET_ENVIRONMENT = "TARGET_ENVIRONMENT";
private static final String TEST_DATA_FILE = "TEST_DATA_FILE";
private static Map<String, Map> environmentConfiguration;
private static Map<String, Map> testDataForEnvironment;
private static final Map<String, String> configs = new HashMap();
private static final Map<String, Boolean> configsBoolean = new HashMap();
private static final Map<String, Integer> configsInteger = new HashMap();
public static Platform platform = Platform.android;
public static BatchInfo batchName;
private static Map<String, Map> environmentConfiguration;
private static Map<String, Map> testDataForEnvironment;
private final Properties properties;

public Runner () {
Expand Down Expand Up @@ -135,40 +134,8 @@ public Runner (String configFilePath, String stepDefDirName, String featuresDirN
run(cukeArgs, stepDefDirName, featuresDirName);
}

private void setBranchName () {
String[] listOfDevices = new String[]{"git", "rev-parse", "--abbrev-ref", "HEAD"};
CommandLineResponse response = CommandLineExecutor.execCommand(listOfDevices);
String branchName = response.getStdOut();
System.out.println("BRANCH_NAME: " + branchName);
configs.put(BRANCH_NAME, branchName);
}

private void loadAndUpdateConfigParameters (String configFilePath, String logPropertiesFile) {
configs.put(CONFIG_FILE, configFilePath);
configs.put(LOG_PROPERTIES_FILE, logPropertiesFile);
buildMapOfRequiredProperties();

configs.forEach((k, v) -> {
if (NOT_SET.equalsIgnoreCase(v) && properties.containsKey(k.toUpperCase())) {
configs.put(k, properties.getProperty(k.toUpperCase()));
}
});

configsBoolean.forEach((k, v) -> {
if (properties.containsKey(k.toUpperCase())) {
configsBoolean.put(k, Boolean.valueOf(properties.getProperty(k.toUpperCase())));
}
});

configsInteger.forEach((k, v) -> {
if (properties.containsKey(k.toUpperCase())) {
configsInteger.put(k, Integer.valueOf(properties.getProperty(k.toUpperCase())));
}
});

System.out.println("updated string values from property file for missing properties: \n" + configs);
System.out.println("updated boolean values from property file for missing properties: \n" + configsBoolean);
System.out.println("updated integer values from property file for missing properties: \n" + configsInteger);
public static boolean isVisualTestingEnabled () {
return configsBoolean.get(IS_VISUAL);
}

public static void remove (long threadId) {
Expand Down Expand Up @@ -253,14 +220,59 @@ private static Map<String, Map> loadTestDataForEnvironment (String environment)
return (NOT_SET.equalsIgnoreCase(testDataFile)) ? new HashMap<>() : JsonFile.getNodeValueAsMapFromJsonFile(environment, testDataFile);
}

public static String getTargetEnvironment () {
return configs.get(TARGET_ENVIRONMENT);
}

public static String getAppName () {
return configs.get(APP_NAME);
}

private void setBranchName () {
String[] listOfDevices = new String[]{"git", "rev-parse", "--abbrev-ref", "HEAD"};
CommandLineResponse response = CommandLineExecutor.execCommand(listOfDevices);
String branchName = response.getStdOut();
System.out.println("BRANCH_NAME: " + branchName);
configs.put(BRANCH_NAME, branchName);
}

private void loadAndUpdateConfigParameters (String configFilePath, String logPropertiesFile) {
configs.put(CONFIG_FILE, configFilePath);
configs.put(LOG_PROPERTIES_FILE, logPropertiesFile);
buildMapOfRequiredProperties();

configs.forEach((k, v) -> {
if (NOT_SET.equalsIgnoreCase(v) && properties.containsKey(k.toUpperCase())) {
configs.put(k, properties.getProperty(k.toUpperCase()));
}
});

configsBoolean.forEach((k, v) -> {
if (properties.containsKey(k.toUpperCase())) {
configsBoolean.put(k, Boolean.valueOf(properties.getProperty(k.toUpperCase())));
}
});

configsInteger.forEach((k, v) -> {
if (properties.containsKey(k.toUpperCase())) {
configsInteger.put(k, Integer.valueOf(properties.getProperty(k.toUpperCase())));
}
});

System.out.println("Updated string values from property file for missing properties: \n" + configs);
System.out.println("Updated boolean values from property file for missing properties: \n" + configsBoolean);
System.out.println("Updated integer values from property file for missing properties: \n" + configsInteger);
}

public void run (ArrayList<String> args, String stepDefsDir, String featuresDir) {
args.add("--glue");
args.add(stepDefsDir);
args.add(featuresDir);
System.out.println("Begin running tests...");
System.out.println("Args: " + args);
String[] array = args.stream().toArray(String[]::new);
Main.main(array);
byte exitStatus = Main.run(array);
System.out.println("Output of test run: " + exitStatus);
}

private void buildMapOfRequiredProperties () {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/znsio/e2e/steps/Hooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import com.znsio.e2e.runner.Runner;
import com.znsio.e2e.tools.Drivers;
import com.znsio.e2e.tools.ScreenShotManager;
import com.znsio.e2e.tools.cmd.CommandLineExecutor;
import com.znsio.e2e.tools.cmd.CommandLineResponse;
import io.cucumber.java.Scenario;
import org.assertj.core.api.SoftAssertions;

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/znsio/e2e/tools/Driver.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.znsio.e2e.tools;

import com.znsio.e2e.entities.TEST_CONTEXT;
import com.znsio.e2e.runner.Runner;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileBy;
Expand Down Expand Up @@ -33,10 +32,10 @@ public Driver (String testName, AppiumDriver<WebElement> appiumDriver) {
}

private void instantiateEyes (String testName, WebDriver innerDriver) {
String applicationName = System.getenv(TEST_CONTEXT.APPLITOOLS_APPLICATION_NAME) == null ? "unified-e2e" : System.getenv(TEST_CONTEXT.APPLITOOLS_APPLICATION_NAME);
String applicationName = Runner.getAppName();
System.out.println("applicationName: " + applicationName);
String appName = applicationName + "-" + Runner.platform;
this.visually = new Visual(this.type, innerDriver, appName, testName, Runner.isVisualTestingEnabled);
this.visually = new Visual(this.type, innerDriver, appName, testName, Runner.isVisualTestingEnabled());
}

public Driver (String testName, WebDriver webDriver) {
Expand Down Expand Up @@ -91,7 +90,7 @@ public void scroll (Point fromPoint, Point toPoint) {
}

public WebElement scrollToAnElementByText (String text) {
return ((AppiumDriver) driver).findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector())" +
return driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector())" +
".scrollIntoView(new UiSelector().text(\"" + text + "\"));"));
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/znsio/e2e/tools/Drivers.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private WebDriver createNewWebDriver (String forUserPersona, TestExecutionContex

WebDriver driver = new ChromeDriver(chromeOptions);

String providedBaseUrl = System.getenv(TEST_CONTEXT.WEB_BASE_URL);
String providedBaseUrl = Runner.BASE_URL_FOR_WEB;
if (null == providedBaseUrl) {
throw new InvalidTestDataException("baseUrl not provided as an environment variable");
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/znsio/e2e/tools/JsonFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import java.util.Map;

public class JsonFile {
private JsonFile() {}
private JsonFile () {
}

public static void saveJsonToFile (Map<String, Map> jsonMap, String fileName) {
System.out.printf("Save the following json to file: '%s'%n'%s'%n", fileName, jsonMap);
Expand All @@ -33,7 +34,7 @@ public static Map<String, Map> getNodeValueAsMapFromJsonFile (String node, Strin
Map<String, Map> envMap = map.get(node);
System.out.println("Loaded map: " + envMap);
if (null == envMap) {
throw new InvalidTestDataException(String.format("Node: '%s' not found in file: '%s'",node, fileName ));
throw new InvalidTestDataException(String.format("Node: '%s' not found in file: '%s'", node, fileName));
}
return envMap;
}
Expand All @@ -60,7 +61,7 @@ public static String getNodeValueAsStringFromJsonFile (String fileName, String[]
nodePath += nodeTree[nodeCount] + " -> ";
map = map.get(nodeTree[nodeCount]);
if (null == map) {
throw new InvalidTestDataException(String.format("Node: '%s' not found in file: '%s'",nodePath, fileName ));
throw new InvalidTestDataException(String.format("Node: '%s' not found in file: '%s'", nodePath, fileName));
}
}
String retValue = String.valueOf(map.get(nodeTree[nodeTree.length - 1]));
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/com/znsio/e2e/tools/Visual.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.znsio.e2e.tools;

import com.appium.utils.Variable;
import com.applitools.eyes.MatchLevel;
import com.applitools.eyes.RectangleSize;
import com.applitools.eyes.StdoutLogHandler;
Expand All @@ -24,6 +25,8 @@ public class Visual {
private final TestExecutionContext context;
private final ScreenShotManager screenShotManager;
private final RectangleSize viewportSize = new RectangleSize(1024, 800);
private final String applitoolsApiKey = Variable.getOverriddenStringValue("APPLITOOLS_API_KEY");
private final String targetEnvironment = Runner.getTargetEnvironment();

public Visual (String driverType, WebDriver innerDriver, String appName, String testName, boolean isVisualTestingEnabled) {
System.out.printf("Visual constructor: Driver type: '%s', appName: '%s', testName: '%s', isVisualTestingEnabled: '%s'%n", driverType, appName, testName, isVisualTestingEnabled);
Expand All @@ -39,14 +42,14 @@ private com.applitools.eyes.appium.Eyes instantiateAppiumEyes (String driverType
}
System.out.println("instantiateAppiumEyes: isVisualTestingEnabled: " + isVisualTestingEnabled);
com.applitools.eyes.appium.Eyes eyes = new com.applitools.eyes.appium.Eyes();
eyes.setApiKey(System.getenv("APPLITOOLS_API_KEY"));
eyes.setApiKey(applitoolsApiKey);
eyes.setBatch(Runner.batchName);
eyes.setLogHandler(new StdoutLogHandler(true));
// eyes.setEnvName(Runner.targetEnvironment);
eyes.setEnvName(targetEnvironment);
eyes.setIsDisabled(!isVisualTestingEnabled);
eyes.setMatchLevel(MatchLevel.STRICT);
if (isVisualTestingEnabled) {
eyes.open((AppiumDriver) innerDriver, appName, testName);
eyes.open(innerDriver, appName, testName);
}
System.out.println("instantiateAppiumEyes: eyes.getIsDisabled(): " + eyes.getIsDisabled());
return eyes;
Expand All @@ -58,14 +61,14 @@ private com.applitools.eyes.selenium.Eyes instantiateWebEyes (String driverType,
}
System.out.println("instantiateWebEyes: isVisualTestingEnabled: " + isVisualTestingEnabled);
com.applitools.eyes.selenium.Eyes eyes = new com.applitools.eyes.selenium.Eyes();
eyes.setApiKey(System.getenv("APPLITOOLS_API_KEY"));
eyes.setApiKey(applitoolsApiKey);
eyes.setBatch(Runner.batchName);
eyes.setLogHandler(new StdoutLogHandler(true));
// eyes.setEnvName(Runner.targetEnvironment);
eyes.setEnvName(targetEnvironment);
eyes.setIsDisabled(!isVisualTestingEnabled);
eyes.setMatchLevel(MatchLevel.STRICT);
if (isVisualTestingEnabled) {
eyes.open((WebDriver) innerDriver, appName, testName, viewportSize);
eyes.open(innerDriver, appName, testName, viewportSize);
}
System.out.println("instantiateWebEyes: eyes.getIsDisabled(): " + eyes.getIsDisabled());
return eyes;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/znsio/e2e/RunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

class RunnerTest {
private String stepDefDir = "com/znsio/e2e/steps";
private String logDir = "target";
private final String stepDefDir = "com/znsio/e2e/steps";
private final String logDir = "target";

@Test
void mainLocalDefault () {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/znsio/e2e/businessLayer/AppBL.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.znsio.e2e.businessLayer;

import com.context.TestExecutionContext;
import com.znsio.e2e.runner.Runner;
import com.znsio.e2e.entities.Platform;
import com.znsio.e2e.runner.Runner;
import com.znsio.e2e.screen.HomeScreen;
import com.znsio.e2e.screen.LoginScreen;
import org.assertj.core.api.SoftAssertions;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/znsio/e2e/businessLayer/LoginBL.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.znsio.e2e.businessLayer;

import com.context.TestExecutionContext;
import com.znsio.e2e.runner.Runner;
import com.znsio.e2e.entities.Platform;
import com.znsio.e2e.runner.Runner;
import org.assertj.core.api.SoftAssertions;

public class LoginBL {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

public class SAMPLE_TEST_CONTEXT extends TEST_CONTEXT {
public static final String ME = "me";
;
}
2 changes: 1 addition & 1 deletion src/test/java/com/znsio/e2e/screen/HomeScreen.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.znsio.e2e.screen;

import com.znsio.e2e.runner.Runner;
import com.znsio.e2e.entities.Platform;
import com.znsio.e2e.runner.Runner;
import com.znsio.e2e.screen.android.HomeScreenAndroid;
import com.znsio.e2e.tools.Driver;
import com.znsio.e2e.tools.Visual;
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/com/znsio/e2e/screen/LoginScreen.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.znsio.e2e.screen;

import com.znsio.e2e.runner.Runner;
import com.znsio.e2e.entities.Platform;
import com.znsio.e2e.screen.android.HomeScreenAndroid;
import com.znsio.e2e.runner.Runner;
import com.znsio.e2e.screen.android.LoginScreenAndroid;
import com.znsio.e2e.tools.Driver;
import com.znsio.e2e.tools.Visual;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.znsio.e2e.screen.LoginScreen;
import com.znsio.e2e.tools.Driver;
import com.znsio.e2e.tools.Visual;
import io.appium.java_client.MobileElement;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

Expand Down Expand Up @@ -32,6 +31,7 @@ public LoginScreen enterLoginDetails (String username, String password) {
driver.findElementByAccessibilityId(passwordId).sendKeys(password);
driver.waitForVisibilityOf(passwordId).sendKeys(username);
visually.takeScreenshot(screenName, "enterLoginDetails");
visually.checkWindow(screenName, "entered login details");
return this;
}

Expand All @@ -46,6 +46,7 @@ public LoginScreen login () {
public String getInvalidLoginError () {
WebElement alertText = driver.waitForVisibilityOf(errorMessageId);
visually.takeScreenshot(screenName, "Invalid Login alert");
visually.checkWindow(screenName, "Invalid Login alert");
return alertText.getText();
}

Expand Down
8 changes: 3 additions & 5 deletions src/test/java/com/znsio/e2e/steps/LoginSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@
import com.context.SessionContext;
import com.context.TestExecutionContext;
import com.znsio.e2e.businessLayer.AppBL;
import com.znsio.e2e.runner.Runner;
import com.znsio.e2e.entities.SAMPLE_TEST_CONTEXT;
import com.znsio.e2e.runner.Runner;
import com.znsio.e2e.tools.Drivers;
import io.cucumber.java.en.When;

import static com.znsio.e2e.tools.Wait.waitFor;

public class LoginSteps {
private final TestExecutionContext context;
private final Drivers allDrivers;
private String userPersona;

public LoginSteps() {
public LoginSteps () {
context = SessionContext.getTestExecutionContext(Thread.currentThread().getId());
System.out.println("context: " + context.getTestName());
allDrivers = (Drivers) context.getTestState(SAMPLE_TEST_CONTEXT.ALL_DRIVERS);
System.out.println("allDrivers: " + (null==allDrivers));
System.out.println("allDrivers: " + (null == allDrivers));
}

@When("I login with invalid credentials - {string}, {string}")
Expand Down
Loading

0 comments on commit 2ecb530

Please sign in to comment.