diff options
Diffstat (limited to 'vid-automation/src/main/java/vid/automation/test/infra')
10 files changed, 342 insertions, 18 deletions
diff --git a/vid-automation/src/main/java/vid/automation/test/infra/Click.java b/vid-automation/src/main/java/vid/automation/test/infra/Click.java index f2cbbef80..8c6bf4831 100644 --- a/vid-automation/src/main/java/vid/automation/test/infra/Click.java +++ b/vid-automation/src/main/java/vid/automation/test/infra/Click.java @@ -2,8 +2,13 @@ package vid.automation.test.infra; import org.junit.Assert; import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils; +import org.openqa.selenium.Alert; +import org.openqa.selenium.By; +import org.openqa.selenium.Keys; import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.Select; +import org.openqa.selenium.support.ui.WebDriverWait; import java.util.List; @@ -35,6 +40,13 @@ public class Click { elements.get(0).click(); } + public static void byXpath(String xpath) { + WebElement element = Get.byXpath(xpath); + Assert.assertNotNull(element); + element.click(); + } + + public static void onFirstSelectOptionById(String id) { Select selectlist = new Select(Get.byId(id)); if(selectlist.getOptions().size() > 1) { @@ -42,6 +54,13 @@ public class Click { } } + public static void onFirstSelectOptionByTestId(String dataTestId) { + Select selectList = new Select(Get.byTestId(dataTestId)); + if(selectList.getOptions().size() > 1) { + selectList.selectByIndex(1); + } + } + public static void onFirstSelectOptionByClass(String className) { final List<WebElement> webElements = Get.byClass(className); webElements.forEach(webElement -> { @@ -51,4 +70,17 @@ public class Click { } }); } + + public static void byClassAndVisibleText(String className, String text ) { + WebElement element = Get.byClassAndText(className, text); + element.click(); + } + + + + public static void acceptAlert() { + Alert alert = GeneralUIUtils.getDriver().switchTo().alert(); + Assert.assertTrue(alert != null); + alert.accept(); + } } diff --git a/vid-automation/src/main/java/vid/automation/test/infra/Exists.java b/vid-automation/src/main/java/vid/automation/test/infra/Exists.java index 14339d63f..63327f50c 100644 --- a/vid-automation/src/main/java/vid/automation/test/infra/Exists.java +++ b/vid-automation/src/main/java/vid/automation/test/infra/Exists.java @@ -1,6 +1,8 @@ package vid.automation.test.infra; +import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; +import org.openqa.selenium.WebElement; import vid.automation.test.Constants; public class Exists { @@ -31,4 +33,12 @@ public class Exists { return false; } } + + public static boolean tagNameInAnotherElement(WebElement parent, String tagName) { + try { + return parent.findElement(By.tagName(tagName)) != null; + } catch (NoSuchElementException exception) { + return false; + } + } } diff --git a/vid-automation/src/main/java/vid/automation/test/infra/FeatureTogglingTest.java b/vid-automation/src/main/java/vid/automation/test/infra/FeatureTogglingTest.java new file mode 100644 index 000000000..6dfb4f119 --- /dev/null +++ b/vid-automation/src/main/java/vid/automation/test/infra/FeatureTogglingTest.java @@ -0,0 +1,31 @@ +package vid.automation.test.infra; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.TYPE; + +/* +In order to skip test classes regards the state of feature flag please use this annotation +There are 2 ways to annotate that tests required featureFlags to be active : +In method level - with @FeatureTogglingTest on the test method and list of Required Feature flags on +In Class level - with @FeatureTogglingTest on the test class and list of Required Feature flags on + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({METHOD, TYPE}) +public @interface FeatureTogglingTest { + + /** + * @return list of feature flags relevant to the test + */ + Features[] value(); + + /** + * @return if all features shall be active. + * If true test would run if all features are active. + * If false test would run if all features are not active. + */ + boolean flagActive() default true; +} diff --git a/vid-automation/src/main/java/vid/automation/test/infra/FeatureTogglingTestngTransformer.java b/vid-automation/src/main/java/vid/automation/test/infra/FeatureTogglingTestngTransformer.java new file mode 100644 index 000000000..46794da10 --- /dev/null +++ b/vid-automation/src/main/java/vid/automation/test/infra/FeatureTogglingTestngTransformer.java @@ -0,0 +1,76 @@ +package vid.automation.test.infra; + +import org.testng.IAnnotationTransformer; +import org.testng.annotations.ITestAnnotation; +import org.togglz.core.context.StaticFeatureManagerProvider; + +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +/* +In order to skip test classes regards the state of feature flag we add this listener to our testng configuration +There are 2 ways to annotate that tests required featureFlags to be active : +In method level - with @FeatureTogglingTest on the test method and list of Required Feature flags on +In Class level - with @FeatureTogglingTest on the test class and list of Required Feature flags on +For each test annotation of method level, we check if the test shall whole class shall run regards the features flag test. +Pay attention that this listener shall be configured in the testng.xml (or command line) +It can't be used as Listener annotation of base class +*/ +public class FeatureTogglingTestngTransformer implements IAnnotationTransformer { + + @Override + public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) { + + if (testMethod!=null) { + try { + + if (!annotation.getEnabled()) { + return; + } + + if (isIgnoreTest(testMethod)) { + disableTest(annotation, testMethod.getDeclaringClass().getName()); + return; + } + + if (isIgnoreTest(testMethod.getDeclaringClass())) { + disableTest(annotation, testMethod.getDeclaringClass().getName()); + return; + } + + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + private boolean isIgnoreTest(AnnotatedElement annotatedElement) { + + return (annotatedElement.isAnnotationPresent(FeatureTogglingTest.class) && + shallDisableTest(annotatedElement.getAnnotation(FeatureTogglingTest.class))); + + } + + private boolean shallDisableTest(FeatureTogglingTest featureTogglingTest) { + if (featureTogglingTest.value().length==0) { + return false; + } + if (new StaticFeatureManagerProvider().getFeatureManager()==null) { + FeaturesTogglingConfiguration.initializeFeatureManager(); + } + for (Features feature : featureTogglingTest.value()) { + if (!(feature.isActive()==featureTogglingTest.flagActive())) { + return true; + } + } + return false; + } + + private void disableTest(ITestAnnotation annotation, String name) { + System.out.println("Ignore "+ name+" due to feature flags configuration"); + annotation.setEnabled(false); + } + +} + diff --git a/vid-automation/src/main/java/vid/automation/test/infra/Features.java b/vid-automation/src/main/java/vid/automation/test/infra/Features.java new file mode 100644 index 000000000..37698fe36 --- /dev/null +++ b/vid-automation/src/main/java/vid/automation/test/infra/Features.java @@ -0,0 +1,25 @@ +package vid.automation.test.infra; + +import org.togglz.core.Feature; +import org.togglz.core.context.FeatureContext; + +public enum Features implements Feature { + + CREATE_INSTANCE_TEST, + EMPTY_DRAWING_BOARD_TEST, + FLAG_REGION_ID_FROM_REMOTE, + FLAG_ASYNC_JOBS, + FLAG_ADD_MSO_TESTAPI_FIELD, + FLAG_ASYNC_INSTANTIATION, + FLAG_UNASSIGN_SERVICE, + FLAG_SERVICE_MODEL_CACHE, + FLAG_NETWORK_TO_ASYNC_INSTANTIATION, + FLAG_COLLECTION_RESOURCE_SUPPORT, + FLAG_SHOW_ASSIGNMENTS, + FLAG_SHOW_VERIFY_SERVICE, + FLAG_SETTING_DEFAULTS_IN_DRAWING_BOARD; + + public boolean isActive() { + return FeatureContext.getFeatureManager().isActive(this); + } +} diff --git a/vid-automation/src/main/java/vid/automation/test/infra/FeaturesTogglingConfiguration.java b/vid-automation/src/main/java/vid/automation/test/infra/FeaturesTogglingConfiguration.java new file mode 100644 index 000000000..a3e14539c --- /dev/null +++ b/vid-automation/src/main/java/vid/automation/test/infra/FeaturesTogglingConfiguration.java @@ -0,0 +1,52 @@ +package vid.automation.test.infra; + + +import org.apache.commons.io.FileUtils; +import org.togglz.core.context.StaticFeatureManagerProvider; +import org.togglz.core.manager.FeatureManager; +import org.togglz.core.manager.FeatureManagerBuilder; +import org.togglz.core.repository.StateRepository; +import org.togglz.core.repository.file.FileBasedStateRepository; + +import java.io.File; +import java.io.IOException; +import java.net.URL; + +import static org.apache.commons.codec.Charsets.UTF_8; + + +public class FeaturesTogglingConfiguration { + + private static FeatureManager createFeatureManager() { + return new FeatureManagerBuilder() + .featureEnum(Features.class) + .stateRepository(getStateRepository()) + .build(); + } + + public static void initializeFeatureManager(){ + StaticFeatureManagerProvider.setFeatureManager(createFeatureManager()); + for (Features feature : Features.values()) { + System.out.println("FeaturesTogglingConfiguration: " + feature.name() + ": " + feature.isActive()); + } + } + + private static StateRepository getStateRepository() { + + final URL propertiesAsResource = FeaturesTogglingConfiguration.class.getClassLoader().getResource("features.properties"); + + final String featuresFile = + System.getProperty( + "FEATURES_FILE", + propertiesAsResource != null ? propertiesAsResource.getFile() : null + ); + + System.out.println("features file: " + featuresFile); + try { + System.out.println(FileUtils.readFileToString(new File(featuresFile), UTF_8)); + } catch (IOException e) { + // YOLO + } + return new FileBasedStateRepository(new File(featuresFile)); + } +} diff --git a/vid-automation/src/main/java/vid/automation/test/infra/Get.java b/vid-automation/src/main/java/vid/automation/test/infra/Get.java index f9ce529f8..fc1d06070 100644 --- a/vid-automation/src/main/java/vid/automation/test/infra/Get.java +++ b/vid-automation/src/main/java/vid/automation/test/infra/Get.java @@ -1,11 +1,13 @@ package vid.automation.test.infra; +import org.junit.Assert; import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils; -import org.openqa.selenium.By; -import org.openqa.selenium.WebElement; +import org.openqa.selenium.*; +import org.openqa.selenium.support.ui.WebDriverWait; import java.util.ArrayList; import java.util.List; +import java.util.function.Function; public class Get { public static WebElement byId(String id) { @@ -24,11 +26,44 @@ public class Get { } } + public static WebElement byXpath(String xpath) { + try { + return GeneralUIUtils.getWebElementBy(By.xpath(xpath)); + } catch (Exception var2) { + return null; + } + } + public static WebElement byXpath(String xpath, int timeout) { + try { + return GeneralUIUtils.getWebElementBy(By.xpath(xpath), timeout); + } catch (Exception var2) { + return null; + } + } + + + public static List<WebElement> multipleElementsByTestId(String dataTestId) { + try { + return GeneralUIUtils.getWebElementsListByTestID(dataTestId); + } catch (Exception var2) { + return null; + } + } public static WebElement byClassAndText(String className, String text) { + return byClassAndText(className, text, null); + } + + public static WebElement byClassAndText(String className, String text, Integer timeoutInSeconds) { WebElement result = null; - List<WebElement> elements = GeneralUIUtils.getWebElementsListByContainsClassName(className); + List<WebElement> elements; + if (timeoutInSeconds!=null) { + elements = GeneralUIUtils.getWebElementsListByContainsClassName(className, timeoutInSeconds); + } + else { + elements = GeneralUIUtils.getWebElementsListByContainsClassName(className); + } for(WebElement element : elements) { if (element.getText().contains(text)) { @@ -54,6 +89,7 @@ public class Get { return GeneralUIUtils.getSelectedElementFromDropDown(dataTestId).getText(); } + public static List<WebElement> byClass(String className) { return GeneralUIUtils.getWebElementsListByContainsClassName(className); } @@ -77,7 +113,7 @@ public class Get { return null; } } - + private static List<List<String>> tableValuesById(String tableId, String section, String column) { List<WebElement> rows = rowsByTableId(tableId, section, column); if(rows != null) { @@ -92,4 +128,28 @@ public class Get { return null; } } + public static String alertText() { + WebDriverWait wait = new WebDriverWait(GeneralUIUtils.getDriver(), 2); + wait.until(alertIsPresent()); + Alert alert = GeneralUIUtils.getDriver().switchTo().alert(); + Assert.assertTrue(alert != null); + return alert.getText(); + } + + public static Function<WebDriver, Alert> alertIsPresent() { + return new Function<WebDriver, Alert>() { + public String toString() { + return "alert to be present"; + } + + @Override + public Alert apply(WebDriver driver) { + try { + return driver.switchTo().alert(); + } catch (NoAlertPresentException arg2) { + return null; + } + } + }; + } } diff --git a/vid-automation/src/main/java/vid/automation/test/infra/Input.java b/vid-automation/src/main/java/vid/automation/test/infra/Input.java index 15fa22549..896ceae56 100644 --- a/vid-automation/src/main/java/vid/automation/test/infra/Input.java +++ b/vid-automation/src/main/java/vid/automation/test/infra/Input.java @@ -1,9 +1,8 @@ package vid.automation.test.infra; -import org.apache.commons.io.FilenameUtils; -import org.apache.commons.lang3.SystemUtils; import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils; import org.openqa.selenium.WebElement; +import vid.automation.test.utils.ReadFile; /** * Created by itzikliderman on 11/09/2017. @@ -14,6 +13,13 @@ public class Input { inputElement.sendKeys(text); } + public static void replaceText(String text, String inputTestsId) { + WebElement inputElement = GeneralUIUtils.getWebElementByTestID(inputTestsId, 30); + inputElement.clear(); + inputElement.sendKeys(text); + } + + public static String getValueByTestId(String testId) { WebElement input = GeneralUIUtils.getInputElement(testId); return input.getAttribute("value"); @@ -25,13 +31,8 @@ public class Input { */ public static void file(String pathInResources, String inputId) { - String path = Input.class.getResource("../../../../"+pathInResources).getPath().toString(); - if (SystemUtils.IS_OS_WINDOWS) { - path = FilenameUtils.separatorsToSystem(path); - if (path.charAt(0)=='\\') { - path = path.substring(1); - } - } + // Copy files from resources upon file-input field, so files will be accessible from inside a jar + String path = ReadFile.copyOfFileFromResources(pathInResources); WebElement inputElement = Get.byId(inputId); inputElement.sendKeys(path); } diff --git a/vid-automation/src/main/java/vid/automation/test/infra/SelectOption.java b/vid-automation/src/main/java/vid/automation/test/infra/SelectOption.java index 048174e99..dc792cfc3 100644 --- a/vid-automation/src/main/java/vid/automation/test/infra/SelectOption.java +++ b/vid-automation/src/main/java/vid/automation/test/infra/SelectOption.java @@ -20,9 +20,13 @@ public class SelectOption { return select; } - public static void byIdAndVisibleText(String id, String text) { + public static Select byIdAndVisibleText(String id, String text) { Select selectlist = new Select(Get.byId(id)); - selectlist.selectByVisibleText(text); + if(text != null) { + selectlist.selectByVisibleText(text); + } + + return selectlist; } public static void byClassAndVisibleText(String className, String text) { @@ -32,7 +36,14 @@ public class SelectOption { byIdAndVisibleText(id, text); }); } - + public static List<WebElement> getList(String selectDataTestId) { + Select selectList = GeneralUIUtils.getSelectList(null, selectDataTestId); + return selectList.getOptions(); + } + public static String getSelectedOption(String selectDataTestId) { + Select selectList = GeneralUIUtils.getSelectList(null, selectDataTestId); + return selectList.getFirstSelectedOption().getText(); + } public static void byTestIdAndVisibleText(String displayName, String selectDataTestId) { GeneralUIUtils.getSelectList(displayName, selectDataTestId); } @@ -44,4 +55,16 @@ public class SelectOption { Click.byClass(Constants.MULTI_SELECT_UNSELECTED_CLASS); } + + public static void selectOptionsFromMultiselectById(String multiSelectId, List<String> options) { + Click.byId(multiSelectId); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + for(String option:options) { + Click.byClassAndVisibleText(Constants.MULTI_SELECT_UNSELECTED_CLASS, option); + } + } } diff --git a/vid-automation/src/main/java/vid/automation/test/infra/Wait.java b/vid-automation/src/main/java/vid/automation/test/infra/Wait.java index fa6ce4f38..848107c83 100644 --- a/vid-automation/src/main/java/vid/automation/test/infra/Wait.java +++ b/vid-automation/src/main/java/vid/automation/test/infra/Wait.java @@ -1,5 +1,6 @@ package vid.automation.test.infra; +import org.apache.commons.collections.CollectionUtils; import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.NoSuchElementException; @@ -21,7 +22,7 @@ public class Wait { } } catch (Throwable t) { - System.out.println("a retry failed duo to:" +t.getMessage()); + System.out.println(String.format("a retry failed due to: %s %s", t, t.getMessage())); } try { intervalUnit.sleep(interval); @@ -37,7 +38,20 @@ public class Wait { } public static boolean waitByClassAndText(String className, String text, int timeoutInSeconds) { - return waitFor((x->Get.byClassAndText(className,text)!=null),null, timeoutInSeconds, 1); + return waitFor((x->Get.byClassAndText(className, text, 1)!=null), null, timeoutInSeconds, 1); + } + + public static boolean waitByClassAndTextXpathOnly(String className, String text, int timeoutInSeconds) { + try { + return CollectionUtils.isNotEmpty( + GeneralUIUtils.getWebElementsListByContainsClassNameAndText(className, text, timeoutInSeconds)); + } + catch (RuntimeException exception) { + System.out.println( + String.format("Failed to waitByClassAndText, after %d seconds. Class name: %s, Text: %s. Cause: %s", + timeoutInSeconds, className, text, exception.toString())); + return false; + } } public static boolean waitByTestId(String dataTestId, int timeoutInSeconds) { |